powershell - Monitoring Services on an Azure VM using an Azure Runbook -


i have powershell script enumerates running services , current state using get-wmiobject win32_service. initial version based on this one , modified azure. when run script in powershell (without azure automation parts) on location machine works fine , can connect machines of interest, when port runbook following error: "get-wmiobject : rpc server unavailable."

q: problem permissions automation account? if so, account should add local machines resolve issue?

q: get-wmiobject not valid way initiate connection? if not, should try instead?

the code i'm using below:

[cmdletbinding(supportsshouldprocess = $true)] param(      # servers check     [parameter(mandatory=$true)][string[]]$serverlist,      # services check     [parameter(mandatory=$true)][string[]]$includeservice     )   # following modifies write-verbose behavior turn messages on globally session $verbosepreference = "continue"  $connectionname = "azurerunasconnection"  # retry $retry = 6 $syncok = $false  $serviceprincipalconnection = get-automationconnection -name $connectionname  {      try     {           add-azurermaccount -serviceprincipal -tenantid $serviceprincipalconnection.tenantid -applicationid $serviceprincipalconnection.applicationid -certificatethumbprint $serviceprincipalconnection.certificatethumbprint         $syncok = $true     }     catch     {         $errormessage = $_.exception.message         $stacktrace = $_.exception.stacktrace         write-warning "error during sync: $errormessage, stack: $stacktrace. retry attempts left: $retry"         $retry = $retry - 1                start-sleep -s 60             } } while (-not $syncok -and $retry -ge 0)  select-azurermsubscription -subscriptionid $subscriptionid -tenantid $serviceprincipalconnection.tenantid $currentsubscription = get-azurermsubscription -subscriptionid $subscriptionid -tenantid $serviceprincipalconnection.tenantid set-azurermcontext -subscriptionid $subscriptionid;  $props=@()  [system.collections.arraylist]$unreachableservers = @()  foreach($servername in ($serverlist))  {       try     {         $service = get-wmiobject win32_service -computername $servername     }     catch     {}      if ($service -ne $null)       {           foreach ($item in $service)           {                #$item.displayname               foreach($include in $includeservice)                {                                               #write-host $include                                                       if(($item.name).contains($include) -eq $true)                   {                      $props += [pscustomobject]@{                     servername = $servername                     name =  $item.name                     status = $item.status                      startmode = $item.startmode                      state = $item.state                     serviceaccount=$item.startname                     displayname =$item.displayname}                 }               }           }       }      else     {         write-host "failed contact server: "$servername         $unreachableservers.add($servername)     }  }    $props | format-table servername,name,startmode,state,serviceaccount,displayname  -autosize 

i assuming using azure automation hybrid worker functionality. default runs under system account. can use different account run runbook under. documented here: azure automation hybrid worker; under runas account section. use same account works when try directly.


Comments