如何在PowerShell中调用带有输出参数的方法?

发布于 2024-07-19 02:20:33 字数 875 浏览 7 评论 0原文

我正在编写一个脚本来开始使用 PowerShell。 我正在尝试转换工作 VBScript 脚本,该脚本枚举远程 Windows 上的映射网络驱动器电脑。

其中一项任务是使用远程 WMI 读取注册表并查找资源管理器的进程所有者。 exe 以确定谁已登录。这似乎很容易 本指南

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

如何调用带有输出参数的方法? 当我尝试给它两个字符串时,出现错误:找不到“GetOwner”的重载和参数计数:“2”。。 MSDN 页面说有两个参数,所以我不确定我做错了什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟火散人牵绊 2024-07-26 02:20:33
$explorer = gwmi Win32_Process -computerName computerName -filter "Name='explorer.exe' and SessionID=0"   
$explorer.GetOwner() | select user,domain
$explorer = gwmi Win32_Process -computerName computerName -filter "Name='explorer.exe' and SessionID=0"   
$explorer.GetOwner() | select user,domain
假面具 2024-07-26 02:20:33

在我看来,问题本质上是关于如何从函数返回多个变量(2 个输出参数)。 我知道这是一个老问题,但为了其他人来这里寻求答案,这里有几个选项:

$Script:num3 = 2
$Script:num4 = 3

# Return an array with multiple values
function MultiplyByTwo {param ($num1, $num2)
    $result1 = $num1 * 2
    $result2 = $num2 * 2

    return $result1, $result2
}

# Modify by reference
function ModifyByReference {param ([ref]$num1, [ref]$num2)
    $num1.Value = $num1.Value * 2
    $num2.Value = $num2.Value * 2
}

# Combination - return 1 value, modify the other by reference
function MultiplyAndModify {param ($num1, [ref]$num2)
    $result1 = $num1 * 2
    $num2.Value = $num2.Value * 2

    return $result1
}

# Modify script variables
function ModifyScriptVariables {
    $Script:num3 = $Script:num3 * 2
    $Script:num4 = $Script:num4 * 2
}


# How to call each function and assign the results
$results = MultiplyByTwo 2 3
Write-Host "Array result = $($results[0]) $($results[1])"

$num1 = 2
$num2 = 3
ModifyByReference ([ref]$num1) ([ref]$num2)
Write-Host "Reference results: $num1 $num2"

$num2 = 3
$result = MultiplyAndModify 2 ([ref]$num2)
Write-Host "Combo results: $result $num2"

ModifyScriptVariables
Write-Host "Script variables: $num3 $num4"

在每种情况下,输出都具有相同的值。

控制台输出results

您可能需要使用一个或另一个选项,具体取决于您调用的函数、是否可以修改该函数以及脚本的上下文。

It seems to me the question is essentially regarding how to return more than one variable from a function (2 output parameters). I know this is an old question, but for the sake of anyone else coming here for answers, here are several options:

$Script:num3 = 2
$Script:num4 = 3

# Return an array with multiple values
function MultiplyByTwo {param ($num1, $num2)
    $result1 = $num1 * 2
    $result2 = $num2 * 2

    return $result1, $result2
}

# Modify by reference
function ModifyByReference {param ([ref]$num1, [ref]$num2)
    $num1.Value = $num1.Value * 2
    $num2.Value = $num2.Value * 2
}

# Combination - return 1 value, modify the other by reference
function MultiplyAndModify {param ($num1, [ref]$num2)
    $result1 = $num1 * 2
    $num2.Value = $num2.Value * 2

    return $result1
}

# Modify script variables
function ModifyScriptVariables {
    $Script:num3 = $Script:num3 * 2
    $Script:num4 = $Script:num4 * 2
}


# How to call each function and assign the results
$results = MultiplyByTwo 2 3
Write-Host "Array result = $($results[0]) $($results[1])"

$num1 = 2
$num2 = 3
ModifyByReference ([ref]$num1) ([ref]$num2)
Write-Host "Reference results: $num1 $num2"

$num2 = 3
$result = MultiplyAndModify 2 ([ref]$num2)
Write-Host "Combo results: $result $num2"

ModifyScriptVariables
Write-Host "Script variables: $num3 $num4"

In each case the output has the same values.

console output results

You may need to use one option or another depending on the function you're calling, whether or not you can modify the function, and based on the context of your script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文