gpt4 book ai didi

powershell - Powershell函数返回字符串,但客户端获取数组

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

下面的函数似乎返回一个字符串。诊断输出(在我的测试中)是

返回值应该是字符串。长度是30

但是,函数的用户会收到一个长度为2的对象数组。第一个元素为null,第二个元素为预期的字符串结果。

可以解释吗?您可以通过对任何.MSI文件运行它来进行测试。尝试一些简单的操作,例如(GetproductNameFromMsi c:\ temp \ myproduct.msi).length。在repro中,这将返回2,而诊断行显示不同的结果。 (除非您有两个字符的产品名称!)

function GetProductNameFromMsi
{
param([string]$msiPathAndFile)
$windowsInst = New-Object -com WindowsInstaller.Installer
$database = $windowsInst.GetType().InvokeMember('OpenDatabase', 'InvokeMethod', $Null, $windowsInst, @($msiPathAndFile, 0))
$view = $database.GetType().InvokeMember('OpenView', 'InvokeMethod', $Null, $database, ("SELECT * FROM Property WHERE Property ='ProductName'"))
$view.GetType().InvokeMember('Execute', 'InvokeMethod', $Null, $view, $Null)
$record = $view.GetType().InvokeMember('Fetch', 'InvokeMethod', $Null, $view, $Null)
$productName = $record.GetType().InvokeMember('StringData', 'GetProperty', $null, $record, 2)
write-host "return value should be $($productName.GetType()). Length is $($productName.Length)"
return $productName
}

最佳答案

问题是$ view.GetType()。InvokeMember('Execute','InvokeMethod',$ Null,$ view,$ Null)返回值。

试试这个:

[void] $ view.GetType()。InvokeMember('Execute','InvokeMethod',$ Null,$ view,$ Null)

关于powershell - Powershell函数返回字符串,但客户端获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26852790/

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