0

I'm trying to do a runbook/webhook that return a status of machine. What I finally notices is that

Get-AzureRmVM

is only returning 2 resource groups. Where

Get-AzureRmResource

Does return many more but not all of them again! I'm sure about my Resource Group Name but still it says resource group 'groupName' could not be found. when I try to run with specific name

 Get-AzureRmVM -ResourceGroupName groupName

While my other start and stop runbook works just fine, so i'm confused I can't see the difference between the groups.

PS: I'm using Azure Run As Connection

param
(
    [Parameter (Mandatory = $false)]
    [object] $WebhookData
)
if ($WebhookData) {

    $vms = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)

    Write-Output "Authenticating to Azure with service principal and certificate"
    $ConnectionAssetName = "AzureRunAsConnection"
    Write-Output "Get connection asset: $ConnectionAssetName" 

    $Conn = Get-AutomationConnection -Name $ConnectionAssetName
            if ($Conn -eq $null)
            {
                throw "Could not retrieve connection asset: $ConnectionAssetName. Check that this asset exists in the Automation account."
            }
            Write-Output "Authenticating to Azure with service principal." 
            Add-AzureRmAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint | Write-Output

        # Start each virtual machine
        foreach ($vm in $vms)
        {
            $vmName = $vm.Name
            Write-Output "Checking $vmName"

            $vmstatus = Get-AzureRmVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroup -Status# | Select-Object -ExpandProperty StatusesText | ConvertFrom-Json

            #select code index and convert to $vmpowerstate
            $vmPowerState = $vmstatus#[1].Code
            #Write-Output "Resource Group: $vmResourceGroupName", ("VM Name: " + $VM.Name), "Status: $VMStatusDetail" `n
            Write-Output ("VM Name: " + $vmName), "Status: $vmPowerState" `n
        }
}
else {
    write-Error "This is webhook only."
}
et3rnal
  • 322
  • 4
  • 17
  • Are the groups in the same subscription? – Joy Wang Nov 09 '18 at 07:27
  • 1
    Thanks for pointing that out, I have 2 subscriptions and Get-AzureRmVM returning 2 vms only because the rest are classics. The automation account I created this runbook at is the other one and I can see that in the overview? its the same one I use to start/stop machines in the other subscription! – et3rnal Nov 09 '18 at 13:02
  • Sorry, but its not solved yet. Thanks for pointing out the different subscriptions which I completely missed. I still can't get the hook to return the status :\ – et3rnal Nov 13 '18 at 03:45
  • I suppose you want to get the `PowerState` eventually? If so, you could check the update in my answer, if I misunderstood, also let me know. – Joy Wang Nov 13 '18 at 05:52

1 Answers1

0

Because your resource groups are in the different subscriptions.

As @et3rnal mentioned,

I have 2 subscriptions and Get-AzureRmVM returning 2 vms only because the rest are classics. The automation account I created this runbook at is the other one and I can see that in the overview? its the same one I use to start/stop machines in the other subscription!

Update:

If you want to get the PowerState of the VM, you could try the command below, in your case, just put it in the loop, the $PowerState is the PowerState.

$status = Get-AzureRmVM -Name <VM Name> -ResourceGroupName <Resource Group Name> -Status
$PowerState = ($status.Statuses | Where-Object {$_.Code -like "PowerState/*"}).DisplayStatus

enter image description here

Update2:

1.For the classic VM, we need to use azure ASM powershell module command, you could try Get-AzureVM, may be the Example 3: Display a table of virtual machine statuses in that link will help.

2.Another option is to migrate the classic VM(ASM) to ARM, then use the ARM command Get-AzureRmVM.

References:

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • This will not work for classic VM nor if the VM is in a different subscription. The idea is to have a webhook that can return the status by taking the VM name only. I don't mind passing more details but I can't seem to find a command to do that. I think I better forget the webhook idea and just use the APIs instead. – et3rnal Nov 14 '18 at 00:03
  • @et3rnal See my Update2, hope it can help you. – Joy Wang Nov 14 '18 at 01:16