2015-07-22 43 views
0

我正在处理一个脚本,我从库中下载了一个脚本来停止所有虚拟机在订阅中运行。这直到上周才完全运行,但是当我昨天运行它时,它无法在我的订阅中找到任何虚拟机(虚拟机在我的子虚拟机中并正在运行)。脚本如下:Get-AzureVM尽管有虚拟机在订阅下返回null

workflow Stop-AllAzureVM 
{ 
    # Add the credential used to authenticate to Azure. 
    # TODO: Fill in the -Name parameter with the Name of the Automation  PSCredential asset 
    # that has access to your Azure subscription. "myPScredName" is your asset name that reflects an OrgID user 
    # like "[email protected]" that has Co-Admin rights to your subscription. 
    $Cred = Get-AutomationPSCredential -Name "myPsCred" 

    # Connect to Azure 
    Add-AzureAccount -Credential $Cred 

    # Select the Azure subscription you want to work against 
    # TODO: Fill in the -SubscriptionName parameter with the name of your Azure subscription 
    Select-AzureSubscription -SubscriptionName "MySubs" 


    # Get all Azure VMs in the subscription that are not stopped and deallocated, and shut them down 
    # all at once. 
    $VMs = Get-AzureVM | where-object -FilterScript {$_.status -ne 'StoppedDeallocated'} 
    if (!$VMs) 
    { 
     Write-Output "No VM running at the moment" 
    } 
    else 
    { 
     Write-Output "VM(s) found running, proceeding for shutdown" 
    } 
    foreach -parallel ($vm in $VMs) 
    {  
     $stopRtn = Stop-AzureVM -Name $VM.Name -ServiceName $VM.ServiceName -force -ea SilentlyContinue 
     $count=1 
     if(($stopRtn.OperationStatus) -ne 'Succeeded') 
     { 
      do{ 
        Write-Output "Failed to stop $($VM.Name). Retrying in 60 seconds..." 
        sleep 60 
        $stopRtn = Stop-AzureVM -Name $VM.Name -ServiceName $VM.ServiceName -force -ea SilentlyContinue 
        $count++ 
       } 
       while(($stopRtn.OperationStatus) -ne 'Succeeded' -and $count -lt 5) 

     } 

     if($stopRtn){Write-Output "Stop-AzureVM cmdlet for $($VM.Name) $($stopRtn.OperationStatus) on attempt number $count of 5."} 
    } 

}

这个脚本始终打印“没有虚拟机上运行的时刻”我试图获取-AzureVM没有任何条件也

但结果却是一样的。

我不确定在过去的几周里是否有什么变化导致了这个问题。

更新-1: 我试图在脚本中添加下面的命令:

$VMs = Get-AzureVM 
$subscriptions = Get-AzureSubscription 
Write-Output "Found [$($subscriptions.Count)] subscriptions accessible to user" 
Write-Output "Found [$($VMs.Count)] VMs in Subscription" 

和我以下输出:

实测值[]订阅用户 访问实测值[0]的VM中订阅 目前没有虚拟机运行

所以它看起来像在我的自动化帐户中发生了奇怪的事情,我不会他们有任何线索!

+0

尝试使用select-azuresubscription cmdlet选择您的订阅 –

+0

@jisaaj它已经存在 - 选择“MySubs” – astaykov

+1

@ user2006769,当您剥离“工作流”部分并在本地运行脚本时会发生什么? PowerShell模块安装在您的机器上? – astaykov

回答

-1

对于所有谁面临这个问题,不知道这是否正确的方法来解决这个问题,但至少作为一种解决方法,我试着创建一个新的自动化帐户,并复制粘贴相同的Runbook,它工作正常我。

相关问题