通过Invoke-Command运行时,属性为空

时间:2015-08-05 12:30:27

标签: powershell scripting monitoring invoke-command

我试图通过过滤掉进程“w3wp.exe属性中找到的字符串来单独监视具有多个自身实例的进程(CommandLine)的内存使用情况。

当我在本地运行此脚本时,它可以正常工作:

$proc = (WmiObject Win32_Process -Filter "Name = 'w3wp.exe'" | Where-Object {$_.CommandLine -like "*SomeTextFromCl*"})
$id = $proc.ProcessId 
$ws = [math]::round((Get-Process -Id $id).WS/1MB)
Write-Host $ws

但是,当我尝试通过Invoke-Command远程运行​​它时,我收到错误,告知Id属性的值为null:

Cannot bind argument to parameter 'Id' because it is null.
+ CategoryInfo          : InvalidData: (:) [Get-Process], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetProcessCommand
+ PSComputerName        : RemoteServerName

我的Invoke-Command语法是:

Invoke-Command -ComputerName RemoteServerName -FilePath script.ps1 -Credential $mycredential

我确信这很简单,但是我很久没有回到PS了,我环顾四周但找不到任何有用的东西。

1 个答案:

答案 0 :(得分:0)

您正在编写控制台的答案。您将ps1用作函数,因此您应该使用:

return $ws

而不是

write-host $ws
相关问题