2016-01-13 71 views
0

對於錯誤提前道歉。我還在學習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格式,但至少,我可以得到一些信息,其中服務器的服務故障重啓未進行相應設置

預先感謝

乾杯

ģ

回答

0

由於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抱怨未初始化的變量,這通常是由錯誤的變量名造成的。

0

得到它的工作。應該早些迴應。多謝你們。

# 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}} 
     }