Powershell getowner

Hi Guys,

I am operating in a Mirosoft RDS Environment and when I run this command to get the owners of a process it doesn't work:

 C:\PS>$p = get-wmiobject win32_process -filter "name='outlook.exe'"

 C:\PS> $p.getowner()

There are quite a few outlook processes ran by serveral users in our organisation.

Thank you in advance.

Regards,
K
chglAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Barry CunneyCommented:
Hi K

do the following to verify that the a collection of processes are actually being returned

$p = get-wmiobject win32_process -filter "name='outlook.exe'"

foreach ($p1 in $p) 

{

      Write-Host $p1.name

}

Open in new window

0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
What is your output when you run your command?

Try:
$p = get-wmiobject win32_process -filter "name='outlook.exe'"
$p.getowner().user
$p.getowner().domain
0
chglAuthor Commented:
I get this error message when I do getowner().user:

Method invocation failed because [System.Object[]] doesn't contain a method named 'getowner'.
At line:1 char:12
+ $p.getowner <<<< ().user
    + CategoryInfo          : InvalidOperation: (getowner:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Try:
$ps = get-wmiobject win32_process -filter "name='outlook.exe'"
foreach ($p in $ps)
{
$p.getowner().user
# $p.getowner().domain
}
0
chglAuthor Commented:
Brilliant marahman,

thats working, now if i want to specifically end a process for a user how do this?
0
chglAuthor Commented:
basically i want to find a specific user using outlook.exe and kill it
0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
if ($p.getowner().user -eq "username")
{
   $p | invoke-wmimethod -name Terminate
}
0
SubsunCommented:
Try..
$p = get-wmiobject win32_process -filter "name='outlook.exe'"
$p | %{$_.getowner() | Select User}

Open in new window

0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
$ps = get-wmiobject win32_process -filter "name='outlook.exe'"
foreach ($p in $ps)
{
    if ($p.getowner().user -eq "username")
      {
         $p | invoke-wmimethod -name Terminate
      }

# $p.getowner().user
# $p.getowner().domain
}
0
chglAuthor Commented:
Marahman and everyone else,

thank you very much for your help.

Are you sure this will terminate the outlook process only for that user and not the entire ourlook processes being run by other users
0
chglAuthor Commented:
Thanks Marahman - it works!

Is it possible for me enter the username and the process in a prompt and then it will search through our rds servers which are rds01, rds02, rds03 and then terminate it?
0
chglAuthor Commented:
so far this is the script:



$ps = get-wmiobject win32_process -computername cieh-rds01,cieh-rds02,cieh-rds03 -filter "name='outlook.exe'"
foreach ($p in $ps)
{
    if ($p.getowner().user -eq "k.uddin")
      { 
        
	$p | invoke-wmimethod -name Terminate

      } 


} 

Open in new window

0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Yes it should kill the user's process since we are using $p in foreach cmdlet for each individual process

you can test by killing notepad asking few users to open notepad from their logins.
0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
$employee = Read-Host "Enter Username: "

$ps = get-wmiobject win32_process -computername cieh-rds01,cieh-rds02,cieh-rds03 -filter "name='outlook.exe'"
foreach ($p in $ps)
{
    if ($p.getowner().user -eq $employee )
      {
       
      $p | invoke-wmimethod -name Terminate

      }


}
0
chglAuthor Commented:
Yes thank you it works:)
0
chglAuthor Commented:
sorry it doesnt work, i copied and pasted the code:

$employee = Read-Host "Enter Username: "
$ps = get-wmiobject win32_process -computername cieh-rds01,cieh-rds02,cieh-rds03 -filter "name='outlook.exe'"
foreach ($p in $ps)
{
    if ($p.getowner().user -eq $employee)
      { 
        
	$p | invoke-wmimethod -name Terminate

      } 


} 

Open in new window


However the read-host doesn't work
0
chglAuthor Commented:
It doesn't prompt for the $employee
0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
$employee = Read-Host "Enter Username: "
$p_name = Read-Host "Enter Process Name: "

$ps = get-wmiobject win32_process -computername cieh-rds01,cieh-rds02,cieh-rds03 -filter "name='$p_name'"

foreach ($p in $ps)
{
    if ($p.getowner().user -eq $employee )
      {
       
      $p | invoke-wmimethod -name Terminate

      }


}
0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
what does it display when you enter the following in powershell window:

$employee = Read-Host "Enter Username: "
0
chglAuthor Commented:
by itself it works fine but when i paste the entire code i get this error:

 
Exception calling "GetOwner" : "Not found "
At line:3 char:20
+     if ($p.getowner <<<< ().user -eq $employee)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

Exception calling "GetOwner" : "Not found "
At line:3 char:20
+     if ($p.getowner <<<< ().user -eq $employee)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

Exception calling "GetOwner" : "Not found "
At line:3 char:20
+     if ($p.getowner <<<< ().user -eq $employee)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

Exception calling "GetOwner" : "Not found "
At line:3 char:20
+     if ($p.getowner <<<< ().user -eq $employee)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

Exception calling "GetOwner" : "Not found "
At line:3 char:20
+     if ($p.getowner <<<< ().user -eq $employee)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

Exception calling "GetOwner" : "Not found "
At line:3 char:20
+     if ($p.getowner <<<< ().user -eq $employee)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

Exception calling "GetOwner" : "Not found "
At line:3 char:20
+     if ($p.getowner <<<< ().user -eq $employee)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

Open in new window

0
chglAuthor Commented:
is there an alternative to read-host - ideally i wanted a pop up where I enter the username and the process
0
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
function CustomInputBox([string] $title, [string] $message, [string] $defaultText)
{
    $inputObject = new-object -comobject MSScriptControl.ScriptControl
    $inputObject.language = "vbscript"
    $inputObject.addcode("function getInput() getInput = inputbox(`"$message`",`"$title`" , `"$defaultText`") end function" )
    $_userInput = $inputObject.eval("getInput")
   
    return $_userInput
}


$employee = CustomInputBox "User Name" "Enter Username" ""
$p_name = CustomInputBox "Process Name" "Enter Process name" ""


$ps = get-wmiobject win32_process -computername cieh-rds01,cieh-rds02,cieh-rds03 -filter "name='$p_name'"

  foreach ($p in $ps)
{
    if ($p.getowner().user -eq $employee )
      {
       
      $p | invoke-wmimethod -name Terminate

      }


}
0
SubsunCommented:
It's not an issue with Read-Host.. If you copy past the code then it won’t work as read host will take the next line as the input and continue with the script and result in error.. you need to save the code in .ps1 and run it from PowerShell console as a script.. or run it from ISE or any other PS Script editor then it will work..
0
chglAuthor Commented:
Brilliant it works!
0
chglAuthor Commented:
what is the best PSscript editor?
0
SubsunCommented:
I am using PowerGUI.. it's free.. There are some paid tools like primal and PS Studio..

PowerShell Script Editors
http://social.technet.microsoft.com/wiki/contents/articles/4109.powershell-script-editors.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.