我有一个脚本可以从多台远程计算机获取进程信息。在尝试获取信息之前,它会检查计算机是否可以访问。如果不是,我想在结果中插入文字。这是我的剧本:
$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
提前致谢
标记
答案 0 :(得分:1)
为每台离线计算机创建一个pscustomobject,例如:
$ProcessNames | Foreach {[pscustomobject]@{CSName=$CurrHost;Name=$_;WorkingSetSize=-1;VirtualSize=-1}