I think this will do what you are looking for in one line. the CIMV2 namespace is there by default but if you wanted to select a different one, you can use gwmi -namesapce.
The "trick" is nesting a where-object in side the foreach-object
gwmi -list | % {$_.Properties | ? {$_.Name.Contains('Memory')}} | select Origin,Name
I came back to this after a brief hiatus to look again with fresh eyes (both mine and a co-worker) and decided to go back to the basics again:
On the client I executed (in administrator shell):
enable-wsmancredssp -role client -delegatecomputer devremvm03 -force
On the server I executed (in administrator shell):
enable-wsmancredssp -role server -force
Both returned normal output indicating CredSSP was now "true".
I then used the following exerciser code to walk through increasing levels of complexity:
$block = {
Write-Host ("hello, world: {0}, {1}" -f $env:USERNAME, (hostname))
}
$username = "test_user"
$password = "..."
$adjPwd = $password | ConvertTo-SecureString -asPlainText -Force
$testCred = (New-Object System.Management.Automation.PSCredential($username,$adjPwd))
switch ($choice)
{
"basic" { Invoke-Command $block }
"remote" { Invoke-Command $block -computername $serverName }
"credentialA" { Invoke-Command $block -computername $serverName -credential $testCred }
"credentialB" { Invoke-Command $block -computername $serverName -credential $testCred -Authentication Credssp}
"session" {
$testSession = New-PSSession -computername $serverName -credential $testCred -Authentication Credssp
if ($testSession) { Invoke-Command $block -Session $testSession; Remove-PSSession $testSession }
}
}
All of that is in my run.ps1 script so the transcript went as follows (and this ran in a non-administrator shell):
PS C:\> .\run.ps1 basic
hello, world: msorens, MyLocalMachine
PS C:\> .\run.ps1 remote MyRemoteServer
hello, world: msorens, MyRemoteServer
PS C:\> .\run.ps1 credentialA MyRemoteServer
hello, world: test_user, MyRemoteServer
PS C:\> .\run.ps1 credentialB MyRemoteServer
hello, world: test_user, MyRemoteServer
PS C:\> .\run.ps1 session MyRemoteServer
hello, world: test_user, MyRemoteServer
Previously, only basic, remote, and credentialA worked. Now all 5 work. Whew!
Best Answer
PowerShell evaluates the content within double quotes to perform any variable expansion, sub-expressions, etc, then it discards those double quotes. What PowerShell returns from
"443"
is literally443
(note the missing quotes).FIND.EXE
requires the search string enclosed with double quotes.If you want to prevent PowerShell from stripping the double quotes use the grave accent (`) to escape them.
You may also use the
--%
parameter to perform the escape. Requires PowerShell 3+.