44

I'm working on a script to get started in PowerShell. I'm trying to convert a working VBScript script that enumerates mapped network drives on a remote Windows computer.

One of the tasks is to use remote WMI to read the registry and find the process owner of explorer.exe in order to determine who is logged in. This seems easy enough going by this guide.

However, the WMI method I need to call is GetOwner() from Win32_Process, which requires two output parameters to store its return value.

How can I call a method with output parameters? When I try to give it two strings, I get the error: Cannot find an overload for "GetOwner" and the argument count: "2".. The MSDN page says there are two parameters, so I'm not sure what I'm doing wrong.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
spoulson
  • 21,335
  • 15
  • 77
  • 102

2 Answers2

61

Using the [ref] modifier:

SomeMethod( [ref] $a );

Notable blog entries

karel
  • 5,489
  • 46
  • 45
  • 50
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
12
$explorer = gwmi Win32_Process -computerName computerName -filter "Name='explorer.exe' and SessionID=0"   
$explorer.GetOwner() | select user,domain
bahrep
  • 29,961
  • 12
  • 103
  • 150
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • 1
    I'd give you doublevotes if I could. This is a better approach, but unfortunately doesn't match the main question so I kinda can't assign this as the accepted answer. – spoulson May 05 '09 at 20:02
  • I may be missing something but the method signature doesn't show the overload you mentioned: PS > $explorer.GetOwner.OverloadDefinitions System.Management.ManagementBaseObject GetOwner() – Shay Levy May 06 '09 at 08:45
  • You're using a different method. I don't really see why, or how I would've known, that the Win32_Process doc shows two output parameters, but via Powershell it accepts none. Does Powershell simply redirect output parameters as output sets? – spoulson May 07 '09 at 13:26