1

I am trying to get the OWNER of a process, code :

(Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'}).getowner() | Foreach-Object user | out-string**

This works great under win8 but in win7 I get this msg :

ForEach-Object : Cannot bind parameter 'Process'. Cannot convert the "user" val
ue of type "System.String" to type "System.Management.Automation.ScriptBlock".
At C:\Program Files (x86)\Advanced Monitoring Agent GP\scripts\9660.ps1:1 char:
108
+ (Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'
}).getowner() | Foreach-Object <<<<  user | out-string
    + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], Parameter 
   BindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerSh 
   ell.Commands.ForEachObjectCommand

Help please! Thank you for the time.

4 Answers 4

1

Instead of foreach-object user, use select -expand user. This is equivalent to doing foreach-object { $_.user } which is probably what you meant to do. Improvements to flexibility in the grammar allow your first attempt in later versions of powershell.

Sign up to request clarification or add additional context in comments.

Comments

1

The older version of Powershell won't work with the simplified syntax. This should work on either one:

(Get-WmiObject -class win32_process | 
 where{$_.ProcessName -eq 'explorer.exe'}).getowner() |
 Foreach-Object { $_.user | out-string }

Comments

1
(Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'}).getowner() | select user

Comments

0

I had a similar problem but in my case, there was a non-printable character in my script that appeared after one of the }'s. ASCII code 03. I found that by opening the script in a binary editor (Textpad8). I deleted this character and it fixed the problem for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.