Powershell-将文字插入结果

Powershell-将文字插入结果,powershell,literals,Powershell,Literals,我有一个脚本,可以从几个远程计算机获取进程信息。在尝试获取信息之前,它会检查计算机是否可以访问。如果不是,我想在结果中插入文字。这是我的剧本: $Pass = ConvertTo-SecureString -string "SECRET" -AsPlainText –Force $User = "User" $Cred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $Pa

我有一个脚本,可以从几个远程计算机获取进程信息。在尝试获取信息之前,它会检查计算机是否可以访问。如果不是,我想在结果中插入文字。这是我的剧本:

$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"
  }
}
else子句只是我输入的东西,试图显示我想要的东西

如果Computer2处于脱机状态,结果应如下所示:

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
提前谢谢


标记

为每台脱机计算机创建pscustomobject,例如:

$ProcessNames | Foreach {[pscustomobject]@{CSName=$CurrHost;Name=$_;WorkingSetSize=-1;VirtualSize=-1}

谢谢Keith,我得到了我想要的数据,当它在else子句中时,我得到了太多的数据,无法评论。我在页面上看到的列是活着的主机,而在页面下看到的是死去的主机