Start-AzVM:您的Azure凭据尚未设置或已过期错误

我在Azure的自动化帐户中创建了一个Runbook,它将启动和停止我的VM,但是在设置Runbook中的凭据时,我一直收到凭据错误。

错误:

Start-AzVM : Your Azure credentials have not been set up or have expired,please run Connect-Azaccount to set up your Azure credentials. At Start-Stopvm-Workflow:59 char:59 + + CategoryInfo : CloseError: (:) [Start-AzVM],ArgumentException + FullyQualifiedErrorId : microsoft.Azure.Commands.Compute.StartAzureVMCommand

Runbook代码:

{
    Param 
    (    
        [String] $action
    ) 

################################################################     
    $connectionName = "AzureRunAsConnection"
    try
    {
        # Get the connection "AzureRunAsConnection "
        $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

        "Logging in to Azure..."
        Connect-Azaccount `
            -ServicePrincipal `
            -TenantId $servicePrincipalConnection.TenantId `
            -applicationid $servicePrincipalConnection.applicationid `
            -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
    }
    catch {
        if (!$servicePrincipalConnection)
        {
            $ErrorMessage = "Connection $connectionName not found."
            throw $ErrorMessage
        } else{
            write-error -Message $_.Exception
            throw $_.Exception
        }
    }
################################################################

    if($action -like "Stop") 
    { 
        $AzureVMs = Get-AzVM | ? {$_.resourcegroupname -notlike 'lab*'}
        Write-Output $AzureVMs.count
        #$AzureVMs = Get-AzVM | ? {$_.resourcegroupname -notlike 'lab*' -And $_.Tags["DontShut"] -notlike "Yes"}

        #Write-Output "Stopping VMs"
        foreach -parallel ($AzureVM in $Azurevms)
        { 
            $temp = "Stop VM: " + $AzureVM.name
            Write-Output $temp
            $AzureVM | Stop-AzVM -Force
            $count = $count+1
        } 
    } 
    else 
    { 
        $AzureVMs = Get-AzVM | ?{$_.Tags["AutomaticStart"] -eq "Yes"}
        Write-Output $AzureVMs.count
        #Write-Output "Starting VMs";

        foreach -parallel ($AzureVM in $AzureVMs) 
        { 
            $temp = "Start VM: " + $AzureVM.name
            Write-Output $temp
            $AzureVM | Start-AzVM 
        }
    } 
}

我尝试使用Connect-AzRMaccount命令,但没有用。

如果有人知道解决方法,我将非常感激。

编辑:尝试更新Az.accounts和Az.Compute模块后,我仍然遇到相同的错误以及四个新错误:

Install-module : NuGet provider is required to interact with NuGet-based repositories. Please ensure that '2.8.5.201' or newer version of NuGet provider is installed. At Start-Stopvm-Workflow:13 char:13 + + CategoryInfo : InvalidOperation: (:) [Install-Module],InvalidOperationException + FullyQualifiedErrorId : CouldNotInstallNuGetProvider,Install-Module
Exception calling "ShouldContinue" with "2" argument(s): "A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\Users\Client\AppData\Roaming\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now?" At Start-Stopvm-Workflow:13 char:13 + + CategoryInfo : NotSpecified: (:) [],MethodinvocationException + FullyQualifiedErrorId : HostException
Install-module : NuGet provider is required to interact with NuGet-based repositories. Please ensure that '2.8.5.201' or newer version of NuGet provider is installed. At Start-Stopvm-Workflow:12 char:12 + + CategoryInfo : InvalidOperation: (:) [Install-Module],Install-Module
Exception calling "ShouldContinue" with "2" argument(s): "A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\Users\Client\AppData\Roaming\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now?" At Start-Stopvm-Workflow:12 char:12 + + CategoryInfo : NotSpecified: (:) [],MethodinvocationException + FullyQualifiedErrorId : HostException

我当前的Runbook脚本如下所示(更改在第12-14行中):

workflow Start-Stopvm-Workflow
{
    Param 
    (    
        [String] $action
    ) 

################################################################     
    $connectionName = "AzureRunAsConnection"
    try
    {
        Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
        Install-module -Force -name Az.accounts
        Install-module -Force -name Az.Compute
        # Get the connection "AzureRunAsConnection "
        $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

        "Logging in to Azure..."
        Connect-Azaccount `
            -ServicePrincipal `
            -TenantId $servicePrincipalConnection.TenantId `
            -applicationid $servicePrincipalConnection.applicationid `
            -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
    }
    catch {
        if (!$servicePrincipalConnection)
        {
            $ErrorMessage = "Connection $connectionName not found."
            throw $ErrorMessage
        } else{
            write-error -Message $_.Exception
            throw $_.Exception
        }
    }
################################################################

    if($action -like "Stop") 
    { 
        $AzureVMs = Get-AzVM | ? {$_.resourcegroupname -notlike 'lab*'}
        Write-Output $AzureVMs.count
        #$AzureVMs = Get-AzVM | ? {$_.resourcegroupname -notlike 'lab*' -And $_.Tags["DontShut"] -notlike "Yes"}

        #Write-Output "Stopping VMs"
        foreach -parallel ($AzureVM in $Azurevms)
        { 
            $temp = "Stop VM: " + $AzureVM.name
            Write-Output $temp
            $AzureVM | Stop-AzVM -Force
            $count = $count+1
        } 
    } 
    else 
    { 
        $AzureVMs = Get-AzVM | ?{$_.Tags["AutomaticStart"] -eq "Yes"}
        Write-Output $AzureVMs.count
        #Write-Output "Starting VMs";

        foreach -parallel ($AzureVM in $AzureVMs) 
        { 
            $temp = "Start VM: " + $AzureVM.name
            Write-Output $temp
            $AzureVM | Start-AzVM 
        }
    } 
}
Sunny_Frankk 回答:Start-AzVM:您的Azure凭据尚未设置或已过期错误

理想情况下,您的Runbook应该仅与Az.AccountsAz.Compute模块一起使用。我能够使用我拥有的帐户测试并执行您的代码段。

我建议您交叉检查当前的自动化帐户/ Runbook配置,或创建一个新的配置,如下所示:

如果这样做仍然存在问题,请告诉我,我们可以进行进一步调查。

有关在Azure自动化here中使用Az模块的更多信息。

本文链接:https://www.f2er.com/3067693.html

大家都在问