2017-04-05 110 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