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