下面的函数似乎会返回一个字符串。诊断输出(在我的测试中)是
返回值应该是字符串。长度是30
但是,函数的用户会收到长度为2的对象数组。第一个元素为null,第二个元素是预期的字符串结果。
可以解释一下吗?您可以通过针对任何.MSI文件运行它来测试它。尝试一些简单的事情(GetproductNameFromMsi c:\ temp \ myproduct.msi).length。在repro中,这将返回2,而诊断线显示不同的结果。 (除非你有一个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
}
答案 0 :(得分:1)
问题是$ view.GetType()。InvokeMember('Execute','InvokeMethod',$ Null,$ view,$ Null)返回一些内容。
试试这个:
[void] $ view.GetType()。InvokeMember('Execute','InvokeMethod',$ Null,$ view,$ Null)