我在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
}
}
}