I am trying to connect to a Windows machine using SSH (it has OpenSSH installed) and I am trying to get the CPU usage of all the processes, by using this command (it's pretty much an answer to another question, that I was trying out):
.\ssh [email protected] powershell Get-Counter "'\Process(*)\% Processor Time'^| Select-Object -ExpandProperty countersamples^| Sort-Object -Property cookedvalue -Descending^| Select-Object -First 25^| ft InstanceName,CookedValue"
After looking at a couple of questions, I learned that by over remote I needed to use escaped pipes, that was one problem cleared, but now I am trying to see if there is a way to add this:
ft InstanceName,@{L='CPU';E={($_.Cookedvalue/100/$env:NUMBER_OF_PROCESSORS).toString('P')}} -AutoSize
Now if I run :
Get-Counter '\Process(*)\% Processor Time'| Select-Object -ExpandProperty countersamples| Sort-Object -Property cookedvalue -Descending| Select-Object -First 25| ft InstanceName,@{L='CPU';E={($_.Cookedvalue/100/$env:NUMBER_OF_PROCESSORS).toString('P')}} -AutoSize
I get all the values neatly represented locally. But if I try to run something like this over an SSH using public key auth:
.\ssh [email protected] powershell Get-Counter "'\Process(*)\% Processor Time'^| Select-Object -ExpandProperty countersamples^| Sort-Object -Property cookedvalue -Descending^| Select-Object -First 25^| ft InstanceName,@{L='CPU';E={($_.Cookedvalue/100/$env:NUMBER_OF_PROCESSORS).toString('P')}} -AutoSize"
The values related to the CPU don't appear at all (Edit: The command doesn't fail, just the column that is suppose to show the CPU values returns blank, if I run the very first command, I get the values still but not converted), and I am guessing that what I am doing is having issues using variables, as it's the only part of the command that doesn't seem to work properly. I am thinking it may be the $_.Cookedvalue and $env:NUMBER_OF_PROCESSORS , and was wondering if there is a way around this? I tried escaping the $ as well as quoting it but the values just seem to get lost.
Basically, I went piece by piece of the command testing it via the remote connection, as it has a slightly different format requirements as it starts on a CMD prompt (was unable to pipe, needed escaped pipes), I tried simply connecting first, then executing the command, but given it's being done via an automated script, it establishes the connection but instantly closes it, forcing me to use it as part of the connection command. Simpler stuff works flawlessly, like creating/deleting files and moving around.