0

私が書いたスクリプトに問題があり、助けてもらいたいです。私はpowershellに非常に慣れていないことに注意してください。

ドメイン上のリモート コンピューターを含む txt ファイルを使用するスクリプトを作成しました。ある程度は機能しているように見えますが、マシンがオフラインになるとエラーが発生し、スクリプトがループします。

$machines
$pcname
Name = 'Machine'
Expression = { $_.PsComputerName }
}

ForEach ($System in $Machines)
{
    #Pings machine's found in text file
    if (!(test-Connection -ComputerName $System -BufferSize 16 -Count 1 -ea 0 -Quiet))
    {
        Write-Output "$System Offline"
    }

Else
   {
     #Providing the machine is reachable 
     #Checks installed programs for products that contain Kaspersky in the name
     gwmi win32_product -Filter {Name like "%Kaspersky%"} -ComputerName $Machines | Select-Object -Property $pcname,Name,Version
   }
}

現在、これは実行され、出力は次のようになります。

Machine          Name                                        Version
UKTEST01         Kaspersky Security Center Network Agent     10.1.249 
UKTEST02         Kaspersky Endpoint Security 10 for Windows  10.2.1.23

ただし、マシンに到達できない場合は、次のエラーが表示されます。

gwmi : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Scripts\Powershell\Kaspersky Endpoint Security 10\Script\New folder\Kaspersky Checker working v2.ps1:15 char:9
+         gwmi win32_product -Filter {Name like "%Kaspersky%"} -ComputerName   $Mach ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

その後、リスト内の次のマシンに移動し、最初からやり直します。

これを単純に次のように表示したい:

UKTEST03 Offline

そして、txt ファイルの最後のマシンが完了したら停止します。

どんな助けやアドバイスも大歓迎です。

4

2 に答える 2

1

これは、Try/Catch/Finally ブロックを使用する絶好の機会です。フローは次のとおりです。ここでコードのブロックを試してください。エラーが発生した場合は、メッセージを抑制し、代わりに Catch ブロックにあることを実行してください。

コードを少し変更したので、このコード ブロック全体をコピーしてドロップし、元のコードの Else {scriptblock} を置き換えます。

Else
{
 #Providing the machine is reachable 
 #Checks installed programs for products that contain Kaspersky in the name
 Try {Get-WMIObject -Class win32_product -Filter {Name like "%Kaspersky%"} `
   -ComputerName $Machines -ErrorAction STOP | 
      Select-Object -Property $pcname,Name,Version }
 Catch {#If an error, do this instead
        Write-Output "$system Offline }
  }
}

完成した回答

あなたが意図したように、$system ではなく $machines 内のすべてのマシンでスクリプトが実行されないようにするために、あなたが要求した変更を折り込みました。

ForEach ($System in $Machines){
    #Pings machine's found in text file
    if (!(test-Connection -ComputerName $System -BufferSize 16 -Count 1 -ea 0 -Quiet))
    {
        Write-Output "$System Offline"
    }
    Else
    {
     #Providing the machine is reachable 
     #Checks installed programs for products that contain Kaspersky in the name
     Try {Get-WMIObject -Class win32_product -Filter {Name like "%Kaspersky%"} `
       -ComputerName $System -ErrorAction STOP | 
          Select-Object -Property $pcname,Name,Version }
     Catch {#If an error, do this instead
            Write-Output "$system Offline "}
     #EndofElse
     }
#EndofForEach
}
于 2015-03-30T15:12:04.567 に答える