【问题标题】:How to call a method with output parameters in PowerShell?如何在 PowerShell 中调用带有输出参数的方法?
【发布时间】:2023-04-01 15:00:01
【问题描述】:

我正在编写一个脚本,以便在 PowerShell 开始。我正在尝试转换一个working VBScript script,它枚举远程 Windows 计算机上的映射网络驱动器。

其中一项任务是使用远程WMI 读取注册表并找到explorer.exe 的进程所有者以确定谁已登录。this guide 似乎很容易。

但是,我需要调用的WMI方法是GetOwner() from Win32_Process,它需要两个输出参数来存储它的返回值。

如何调用带有输出参数的方法?当我尝试给它两个字符串时,我收到错误:Cannot find an overload for "GetOwner" and the argument count: "2".。 MSDN页面说有两个参数,所以我不确定我做错了什么。

【问题讨论】:

    标签: powershell wmi remote-registry


    【解决方案1】:

    【讨论】:

    • 这样称呼它$YouWillNeedTheParens = SomeMethod -a:([ref]$newVar)
    【解决方案2】:
    $explorer = gwmi Win32_Process -computerName computerName -filter "Name='explorer.exe' and SessionID=0"   
    $explorer.GetOwner() | select user,domain
    

    【讨论】:

    • 如果可以的话,我会给你双票。这是一种更好的方法,但不幸的是与主要问题不匹配,因此我无法将其指定为已接受的答案。
    • 我可能遗漏了一些东西,但方法签名没有显示您提到的重载:PS > $explorer.GetOwner.OverloadDefinitions System.Management.ManagementBaseObject GetOwner()
    • 您使用的是不同的方法。我真的不明白为什么,或者我怎么知道,Win32_Process 文档显示了两个输出参数,但是通过 Powershell 它不接受任何输出参数。 Powershell 是否只是将输出参数重定向为输出集?
    猜你喜欢
    • 1970-01-01
    • 2013-07-03
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2011-07-13
    相关资源
    最近更新 更多