我有一个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