gpt4 book ai didi

powershell - 无法在PowerShell函数中运行[System .__ ComObject] .InvokeMember

转载 作者:行者123 更新时间:2023-12-03 01:18:27 26 4
gpt4 key购买 nike

我一直试图编写一个程序来访问第三方OLE DLL的属性和方法。

下面的代码运行正常。

[System.__ComObject].InvokeMember("AppName", [System.Reflection.BindingFlags]::GetProperty, $null, $appObj, $null)

由于调用将是重复的,因此我想像下面这样调用包装器。
function Get-Property {
param(
$objOLE,
[String] $propertyName
)
[System.__ComObject].InvokeMember($propertyName,[System.Reflection.BindingFlags]::GetProperty,$null,$objOLE,$null)
}

脚本何时运行
Get-Property($appObj, "AppName")

我收到此错误:
Exception calling "InvokeMember" with "5" argument(s): "Method 'System.__ComObject.ToString' not found."
At F:\Scripts\test.ps1:21 char:36
+ [System.__ComObject].InvokeMember <<<< ($propertyName,[System.Reflection.BindingFlags]::GetProperty,$null,$objOLE,$null)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

这很令人困惑。有人有见识吗?先感谢您。

最佳答案

请记住,您的函数是PowerShell函数/命令,而不是.NET方法,即不要使用括号并且不要逗号分隔参数:

Get-Property $appObj AppName

有了它,函数将获得一个参数,该参数是一个包含两个元素的数组。

关于powershell - 无法在PowerShell函数中运行[System .__ ComObject] .InvokeMember,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22658748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com