1
我試圖從一個MSI的一些信息文件中像ProductCode
或CustomAction
。我使用了來自互聯網的其他腳本並對它們進行了編輯,但我的腳本存在一些問題。PowerShell中獲取MSI信息
這僅僅是一小片的原代碼,但如果我執行此,我收到此錯誤信息:
警告:異常調用「InvokeMember」和「5」的說法(S):「的OpenView,SQL」的
function Get-MSIFileInformationList {
[CmdletBinding()]
[OutputType([string])]
param(
[parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]$Path,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$FROM = "Property",
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$LIKE = "Property",
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$WHERE = "Property",
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$SELECT = "Property"
)
Begin
{
}
Process {
try {
# Read property from MSI database
$WindowsInstaller = New-Object -ComObject WindowsInstaller.Installer
$MSIDatabase = $WindowsInstaller.GetType().InvokeMember("OpenDatabase", "InvokeMethod", $null, $WindowsInstaller, @($Path.FullName, 0))
$Query = "SELECT $SELECT FROM $FROM WHERE $WHERE LIKE '%$($LIKE)%'"
# $Query = "SELECT Value FROM CustomAction WHERE Action = '$($CustomAction)'"
Write-Host 1
$View = $MSIDatabase.GetType().InvokeMember("OpenView", "InvokeMethod", $null, $MSIDatabase, ($Query))
Write-Host 2
$View.GetType().InvokeMember("Execute", "InvokeMethod", $null, $View, $null) | Out-Null
Write-Host 3
$Record = $View.GetType().InvokeMember("Fetch", "InvokeMethod", $null, $View, $null)
try {
Write-Host 4
$Value = $Record.GetType().InvokeMember("StringData", "GetProperty", $null, $Record, 1)
Write-Host 5
# Commit database and close view
$MSIDatabase.GetType().InvokeMember("Commit", "InvokeMethod", $null, $MSIDatabase, $null) | Out-Null
Write-Host 6
$View.GetType().InvokeMember("Close", "InvokeMethod", $null, $View, $null) | Out-Null
Write-Host 7
$MSIDatabase = $null
$View = $null
} catch {
$Value = "-"
}
# Return the value
return $Value
}
catch {
Write-Warning -Message $_.Exception.Message ; break
return ""
}
}
End {
# Run garbage collection and release ComObject [System.Runtime.Interopservices.Marshal]::ReleaseComObject($WindowsInstaller) | Out-Null
[System.GC]::Collect()
}
}
使用什麼參數執行命令?它看起來像這條線是失敗的:'$ View = $ MSIDatabase.GetType()。InvokeMember(「OpenView」,「InvokeMethod」,$ null,$ MSIDatabase,($ Query))''但這可能是因爲' $ MSIDatabase'或'$ Query'未正確填充。 –
呼叫['Installer.LastErrorRecord()'](https://msdn.microsoft.com/en-us/library/windows/desktop/aa369430(V = vs.85)的.aspx),以獲得詳細的錯誤信息。 – zett42