使用 Azure Runbook 监视 Azure VM 上的服务

Monitoring Services on an Azure VM using an Azure Runbook

我有一个 Powershell 脚本,它使用 Get-WmiObject Win32_Service 枚举 运行ning 服务及其当前状态。初始版本基于 this one,然后针对 Azure 进行了修改。当我 运行 我的位置机器上的 Powershell 中的脚本(没有 azure 自动化部件)它工作正常并且我可以连接到所有感兴趣的机器,但是当我将它移植到 运行book 时我得到以下错误:"Get-WmiObject : The RPC server is unavailable."

问:是自动化账号的权限问题吗?如果是这样,我应该在本地机器上添加什么帐户来解决这个问题?

问: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

您考虑过使用 OMS 吗?这听起来是个更好的选择。

无论如何,为了回答你的问题,我可能会创建一个本地用户并创建一个 PS 配置端点供该用户连接,并从自动化帐户模拟该用户进行连接,但我还是甚至不会走这条路,我宁愿使用 OMS.

我假设你正在使用 Azure Automation Hybrid Worker 功能。默认情况下它 运行s 在系统帐户下。但是,您可以使用不同的帐户 运行 运行 下的书。这记录在这里:Azure Automation Hybrid Worker;查看 RunAs 帐户部分。使用与您直接尝试时相同的帐户。