How to call a method with output parameters in PowerShell?

假装没事ソ 提交于 2019-12-18 18:48:50

问题


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.


回答1:


Using the [ref] modifier

SomeMethod( [ref] $a );

Notable blog entries

  • http://geekswithblogs.net/Lance/archive/2009/01/14/pass-by-reference-parameters-in-powershell.aspx
  • http://weblogs.asp.net/soever/archive/2009/03/26/powershell-return-values-from-a-function-through-reference-parameters.aspx



回答2:


$explorer = gwmi Win32_Process -computerName computerName -filter "Name='explorer.exe' and SessionID=0"   
$explorer.GetOwner() | select user,domain


来源:https://stackoverflow.com/questions/821744/how-to-call-a-method-with-output-parameters-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!