将powershell变量插入msiexec命令

时间:2016-09-08 20:03:02

标签: powershell variables command msiexec

我们在这里需要一个快速脚本将变量名称传递给命令行。很容易,但我还是难倒了。我需要使用msiexec / f来修复C:\ windows \ installer目录中的msi。但是,MSI具有变量名称。我需要获取本地名称,然后将该名称和路径放入脚本中。然后它应该使用正确的路径和名称运行msiexec / f到本地MSI。这可能是一个简单的引用/格式的事情或者我可能完全在错误的门口吠叫。我对此比较陌生。有任何想法吗?

$localpackageName = 'localPackageName'

get-wmiobject -class win32_product -filter "name = 'smart ink'" | select-object localpackage -outvariable localPackageName

Invoke-Expression -command C:\windows\system32\MSIExec.exe /f $localPackageName

2 个答案:

答案 0 :(得分:1)

如果您只是尝试触发智能墨水的修复,则有更简单的方法。 Msiexec允许您pass it the guid for the ProductId而不是msi的路径。您需要做的就是获得相同版本软件的所有安装应该保持相同的guid。从注册表获取GUID而不是调用Win32_Product的简单方法是:

##Read in installed packages, check for a displayname, split the key to get the GUID
$prodId = Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
 Where-Object {$_.GetValue('DisplayName') -match 'smart ink'} |
 Foreach-Object { $_.Name -split '\\'} | Select-Object -last 1

在我的情况下,这将返回{5ABC49B5-D0DC-428D-A082-4AEFF6490F04}作为智能墨水产品ID。然后,您可以将其传递给msiexec。

msiexec /fa $prodId

答案 1 :(得分:-1)

试试这个......

$localPackageName = 'localPackageName'
$command = 'C:\windows\system32\MSIExec.exe /f'

get-wmiobject -class win32_product -filter "name = 'smart ink'" | select-object localpackage -outvariable localPackageName

$packageName = $localPackageName.localpackage

$string = '{0} {1}' -f $command, $packageName
Invoke-Expression $string