Property is empty when Invoke-Command is run.

I am trying to individually control the memory usage of a process ( w3wp.exe ) that has multiple instances of itself, filtering out the string found in the CommandLine process CommandLine .

It works when I run this script locally:

 $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 

However, when I try to run it remotely via Invoke-Command , I get an error message indicating that the value of the Id property is null:

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

My Invoke-Command syntax:

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

I am sure it is simple, but I returned to PS after a long absence, and I looked around, but did not find anything useful.

+4
source share
1 answer

You are writing a response to the console. You are using ps1 as a function, so you should use:

 return $ws 

instead

 write-host $ws 
0
source

All Articles