【问题标题】:Getting a specific service recovery option for a list of servers获取服务器列表的特定服务恢复选项
【发布时间】:2016-01-13 13:00:45
【问题描述】:

对于错误提前道歉。我还在学习 Powershell。

我正在尝试检查 AD 中服务器列表的特定服务恢复选项

$credential = Get-Credential
$servers = Get-ADComputer -Filter * -properties * | ?{$_.OperatingSystem -match "server"} | ft name -hidetableheaders | out-string
$Results = @()
    foreach ($Server in $Servers)
    {
      Invoke-command -cn $server -credential $credential -ScriptBlock {Get-WMIObject win32_service | 
    Where-Object {$_.description -imatch "nscli" -and $_.startmode -eq "Auto"}; foreach ($service in $services){sc.exe qfailure $service.name}}
    }

我收到以下错误

Invoke-command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects
instead of strings.
At line:1 char:32
+ foreach ($Server in $Servers) {Invoke-command -cn $server -credential $credentia ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
+ FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand

如果我直接在每台服务器上运行命令,我没有任何问题。

Invoke-command -cn EXCHANGE -credential $credential -ScriptBlock {$services = Get-WMIObject win32_service | Where-Object {$_.description -imatch "nscli" -and $_.startmode -eq "Auto"}; foreach ($service in $services){sc.exe qfailure $service.name}}
[SC] QueryServiceConfig2 SUCCESS

SERVICE_NAME: NSClientpp
    RESET_PERIOD (in seconds)    : 0
    REBOOT_MESSAGE               :
    COMMAND_LINE                 :
    FAILURE_ACTIONS              : RESTART -- Delay = 120000 milliseconds.
                                   RESTART -- Delay = 120000 milliseconds.

因为我使用的是 sc.exe,我不确定如何将列表输出为 csv 格式,但至少我可以获得一些关于哪些服务器没有相应设置服务失败重启的信息

提前致谢

干杯

G

【问题讨论】:

    标签: powershell


    【解决方案1】:

    由于Invoke-Command 不支持-WhatIf 开关,最好的办法是在控制台上回显服务器名称以查看导致失败的服务器名称。在调试语句中,服务器名称用单引号 ' 括起来,例如,额外的空格更容易看到。像这样,

    foreach ($Server in $Servers)
    {
      Write-host $("Now processing server '{0}' " -f $Server)
      Invoke-command ...
    }
    

    您还可以尝试将Invoke-Command 语句写入控制台并将这些语句复制粘贴到另一个 Powershell 会话,以查看哪个(如果有)是失败的。

    对于它的价值,您似乎使用了变量$Server$server(注意大写/小写的区别)。尽管在 Powershell 中变量名不区分大小写,但您可能会因为简单的拼写错误而产生未初始化的变量。为了防止这种情况,请使用Set-StrictMode。这将导致 Powershell 抱怨未初始化的变量,这通常是由错误输入变量名引起的。

    【讨论】:

      【解决方案2】:

      搞定了。应该早点回复。谢谢大家。

      # Get the credential
      $Credential=Get-Credential
      
      # Collect the server list
      $Servers = Get-ADComputer -Filter * -Properties operatingsystem | Where operatingsystem -match 'server'
      
      $Results = @()    
      
      ## Check service status 
      $Results = foreach ($Server in $Servers)
              {Invoke-Command -ComputerName $Server.Name –Ea 0 -Credential $Credential {Get-Service |?{$_.DisplayName –match “nscli”} }}
      
      ## Create a csv report
      $Results| Sort PSComputerName | select PSComputerName, DisplayName, Name, Status |Export-Csv nsclient_service.csv -NoTypeInformation -UseCulture
      
      # Check the recovery options for NSclient service on each Server
      foreach ($Server in $Servers)
              { write-host "Checking " $Server.Name;
              Invoke-Command -ComputerName $Server.Name –Ea 0 -Credential $Credential `
                    {$services = Get-WMIObject win32_service | Where-Object {$_.name -imatch "nscli" -and $_.startmode -eq "Auto"}; `
                          foreach ($service in $services){sc.exe qfailure $service.name}}
              }
      

      【讨论】:

        猜你喜欢
        • 2023-03-21
        • 1970-01-01
        • 2019-08-15
        • 2018-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-25
        • 1970-01-01
        相关资源
        最近更新 更多