0

I have a script that gets process info from several remote computers. Before trying to get the info, it checks if the computer is reachable. If it isn't I want to insert literals into the results. Here's my script:

$Pass = ConvertTo-SecureString -string "SECRET" -AsPlainText –Force
$User = "User"
$Cred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $Pass 
$ProcessNames = @('App1.exe', 'App2.exe')
$HostList =@('Computer1','Computer2', 'Computer3')

foreach ($CurrHost in $HostList)
{
    # check if it's alive
    if((Test-Connection -Cn $CurrHost -BufferSize 16 -Count 1 -ea 0 -quiet))
    {
        gwmi win32_process -computername $CurrHost -Credential  $Cred |
            Where-Object  {$ProcessNames -contains $_.Name } |
            Select-Object CSName, Name, WorkingSetSize, VirtualSize #|
            #Format-Table -AutoSize
  }
    Else
  {
    # This will be replaced with a foreach $App
    Select-Object $CurrHost, "App1.exe", "-1", "-1"
    Select-Object $CurrHost, "App2.exe", "-1", "-1"
  }
}

The else clause is just something I put in to try and show what I want

Results should be like this if Computer2 is offline:

CSName          Name           WorkingSetSize    VirtualSize
------          ----           --------------    -----------
Computer1     App1.exe                6516736       82006016
Computer1     App2.exe              156880896      338481152
Computer2     App1.exe                     -1             -1
Computer2     App2.exe                     -1             -1
Computer3     App1.exe                9981952       78761984
Computer3     App2.exe              219643904      357588992

Thanks in advance

Mark

4

1 回答 1

1

为每台离线计算机创建一个 pscustomobject,例如:

$ProcessNames | Foreach {[pscustomobject]@{CSName=$CurrHost;Name=$_;WorkingSetSize=-1;VirtualSize=-1}
于 2013-10-04T19:48:51.317 回答