2017-04-05 137 views
1

我有一個Powershell腳本,它使用Get-WmiObject Win32_Service枚舉運行服務及其當前狀態。初始版本基於this one,然後針對Azure進行了修改。當我在我的位置機器上運行Powershell中的腳本(沒有天藍色的自動化部件)時,它工作正常,我可以連接到所有感興趣的機器,但是當我將它移植到Runbook時,出現以下錯誤:「Get-WmiObject :RPC服務器不可用。「使用Azure Runbook監測Azure虛擬機上的服務

問:自動化帳戶的權限問題?如果是這樣,我應該添加到本地機器來解決問題?

問:Get-WmiObject不是啓動連接的有效方式嗎?如果不是,我該怎麼試試?

我正在使用的代碼如下:

[CmdletBinding(SupportsShouldProcess = $true)] 
param(

    # Servers to check 
    [Parameter(Mandatory=$true)][string[]]$ServerList, 

    # Services to check for 
    [Parameter(Mandatory=$true)][string[]]$includeService 
    ) 

# Following modifies the Write-Verbose behavior to turn the messages on globally for this session 
$VerbosePreference = "Continue" 

$connectionName = "AzureRunAsConnection" 

# retry 
$retry = 6 
$syncOk = $false 

$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName 
do 
{ 
    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; 

[email protected]() 

[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 to contact server: "$ServerName 
     $unreachableServers.Add($ServerName) 
    } 
} 

$props | Format-Table Servername,Name,startmode,state,serviceaccount,displayname -AutoSize 

回答

0

我假設您正在使用Azure自動化混合工作者功能。默認情況下,它在系統帳戶下運行。但是,您可以使用其他帳戶在下運行該Runbook。這裏記錄在這裏:Azure Automation Hybrid Worker;查看RunAs帳戶部分。使用直接嘗試時使用的相同帳戶。

+0

是的,這是我最終選擇的解決方案。總之:1)我創建了一個自定義混合工作組2)我將該組與AD憑證相關聯,以對有問題的計算機擁有權限。 3)我使用「RunAs」參數在該混合工作組下運行Runbook – user2766185

0

你有沒有考慮過使用OMS?這聽起來像是一件更好的事情。

無論如何,爲了回答你的問題,我可能會創建一個本地用戶,併爲該用戶創建一個PS配置終端來連接,並從該自動化帳戶連接模擬該用戶,但是我再也不會去這條路線,我寧願使用OMS

+0

如果窗口服務停止或崩潰,是否總是寫入事件日誌? – user2766185

+0

取決於服務,顯然 – 4c74356b41

+0

OMS是否有辦法直接詢問服務以檢查其狀態?沒有這一點,如果服務沒有記錄所有的狀態變化,那麼OMS可能會錯過服務崩潰,而更直接的方法似乎更可靠地工作。 – user2766185