我有一个Powershell脚本,它使用Get-WmiObject Win32_Service枚举正在运行的服务及其当前状态。初始版本基于this one,然后针对Azure进行了修改。当我在我的位置机器上运行Powershell中的脚本(没有azure自动化部件)时,它工作正常,我可以连接到所有感兴趣的机器,但是当我将它移植到Runbook时,我得到以下错误:" Get-WmiObject:RPC服务器不可用。"
问:自动化帐户的权限是否有问题?如果是这样,我应该在本地计算机上添加哪个帐户来解决问题?
问: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;
$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 to contact server: "$ServerName
$unreachableServers.Add($ServerName)
}
}
$props | Format-Table Servername,Name,startmode,state,serviceaccount,displayname -AutoSize
答案 0 :(得分:0)
无论如何,为了回答你的问题,我可能会创建一个本地用户并为该用户创建一个PS配置端点来连接,并连接模拟该用户从自动化帐户,但是,我再也不会这样做路线,我宁愿使用OMS。
答案 1 :(得分:0)
我假设您使用的是Azure Automation Hybrid Worker功能。默认情况下,它在系统帐户下运行。但是,您可以使用其他帐户运行Runbook。这在此处记录:Azure Automation Hybrid Worker;查看RunAs帐户部分。使用直接尝试时使用的帐户。