首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带"5“参数错误的"InvokeMember”

带"5“参数错误的"InvokeMember”
EN

Stack Overflow用户
提问于 2022-09-07 02:09:31
回答 2查看 83关注 0票数 0

我经常遇到一个带有5个参数错误的InvokeMember,但我相信这与我的路径有关。我想从脚本文件夹所在的任何位置从任何计算机运行此命令。

代码语言:javascript
复制
$ScriptDir = Split-Path $MyInvocation.MyCommand.Path
$ScriptName = $MyInvocation.MyCommand.Name

Function Get-MsiDBVersion {
param (
    [string] $fn
)
try {
    $FullPath = (Resolve-Path $fn).Path 
    $windowsInstaller = New-Object -com WindowsInstaller.Installer
    $database = $windowsInstaller.GetType().InvokeMember(
            "OpenDatabase","InvokeMethod", $Null, 
            $windowsInstaller, @($Fullpath, 0)
        )
    $q = "SELECT Value FROM Property WHERE Property = 'ProductVersion'"
    $View = $database.GetType().InvokeMember(
            "OpenView", "InvokeMethod", $Null, $database, ($q)
        )
    $View.GetType().InvokeMember("Execute", "InvokeMethod", $Null, $View, $Null) | Out-Null
    $record = $View.GetType().InvokeMember(
            "Fetch", "InvokeMethod", $Null, $View, $Null
        )
    $productVersion = $record.GetType().InvokeMember(
            "StringData", "GetProperty", $Null, $record, 1
        )
    $View.GetType().InvokeMember("Close", "InvokeMethod", $Null, $View, $Null) | Out-Null
    return $productVersion
} catch {
    throw "Failed to get MSI file version the error was: {0}." -f $_
}
}

$Program = @{
Installer = "$ScriptDir\Program.exe"
Version = (get-item "$ScriptDir\Program.exe").VersionInfo.FileVersion.TrimEnd()
Arguments = "/SILENT /ALLUSERS /NORESTART"
Exe = "C:\Program Files (x86)\Program.exe"
ExeVersion = (Get-ChildItem $RegUninstall32 -ErrorAction SilentlyContinue | where name -like "*program*" | Get-ItemProperty ).DisplayVersion
}
EN

回答 2

Stack Overflow用户

发布于 2022-09-07 14:55:15

  • 您需要将完整的文件系统本机路径传递到.msi文件到WindowsInstaller.Installer COM对象(Windows安装程序COM自动化接口 ),因为PowerShell的当前目录(位置)通常不同于其他进程内环境,即.NET和COM使用的非托管进程。
代码语言:javascript
复制
- While `$FullPath = (Resolve-Path $fn).Path` is an _attempt_ to get the full path from a potentially relative one, it isn't _robust_, because the resolved path may be based on a _PowerShell-only_ drive (created with `New-PSDrive`), which the outside world knows nothing about.
代码语言:javascript
复制
- Instead, use [`Convert-Path`](https://learn.microsoft.com/powershell/module/microsoft.powershell.management/convert-path) `-LiteralPath $fn`, which returns a _file-system-native_ path, as known to all environments.
代码语言:javascript
复制
- In either case, the resolving is based on the _current location_, as reflected in `$PWD` / `Get-Location`.
  • 没有必要通过.GetType().InvokeMember()使用反射来调用方法并访问Windows对象公开的类型的实例的属性--照例直接调用/访问它们。(实际上,我无法让基于反射的代码工作。)

因此,下列各项应能发挥作用:

代码语言:javascript
复制
Function Get-MsiDBVersion {
  param (
    [string] $LiteralPath
  )
  try {
    # Convert to a full, native path.
    $fullPath = Convert-Path -ErrorAction Stop -LiteralPath $LiteralPath
    $windowsInstaller = New-Object -ComObject WindowsInstaller.Installer
    $database = $windowsInstaller.Opendatabase($fullPath, 0)
    $q = "SELECT Value FROM Property WHERE Property = 'ProductVersion'"
    $view = $database.OpenView($q)
    $null = $View.Execute()
    $record = $View.Fetch()
    $productVersion = $record.StringData(1)
    $null = $View.Close()
    return $productVersion
  }
  catch {
    throw "Failed to get MSI file version; the error was: {0}." -f $_
  }
}
票数 1
EN

Stack Overflow用户

发布于 2022-09-07 06:35:01

我不知道您目前的目标是什么,但是如果您想获得有关MSIpackages的信息,可以这样做:

代码语言:javascript
复制
Get-CimInstance -query "select * from win32_product"

您的查询不起作用--您的代码:

代码语言:javascript
复制
$q = "SELECT Value FROM Property WHERE Property = 'ProductVersion'"

语法必须是:

代码语言:javascript
复制
"Select [Property] from [class] where [property] = [propertyvalue]"

例如:

代码语言:javascript
复制
get-ciminstance -query "select version,installState,Description,version,IdentifyingNumber from win32_product where Name = 'Blender'"

产出:

代码语言:javascript
复制
Name                  : Blender
Version               : 2.82.1
InstallState          : 5
Caption               :
Description           : Blender
IdentifyingNumber     : {EDFAE2A8-E73B-4CD1-9648-46A7E4434BDA}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73629365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档