繁体   English   中英

使用 Start-Process 安装 MSI 时将参数作为变量传递

[英]Passing arguments as a variable when installing an MSI using Start-Process

我是 Powershell 的新手,当然,我正在尝试为项目即时学习 - 没有压力,对吧! :-)

我正在编写一个脚本,以安静模式运行 MSI 包,将激活代码作为参数传递给它,我必须从 XML 文件中提取该代码。

到目前为止,除了让 Start-Process 运行带有在变量中传递的参数的 MSI 之外,我已经完成了所有工作。

Set-ExecutionPolicy Bypass -Force
[System.Xml.XmlDocument]$XML_Doc = new-object System.Xml.XmlDocument
$XML_Doc.load('c:\myfolder\Configinfo.XML')
$ActivationID = $XML_Doc.CONFIGINFO.SITEINFO.ACTIVATEID
write-host "Activation Id is: $ActivationID"
$InstallString = "`'/I C:\myfolder\myinstaller.msi akey="+'"'+$ActivationID+'"'''
#$InstallString = "`'/I C:\myfolder\myinstaller.msi akey=`"$($ActivationID)`"'"
write-host "$InstallString"'''
Start-Process msiexec.exe -ArgumentList  $InstallString  -Wait -NoNewWindow
#Start-Process msiexec.exe -ArgumentList '/I C:\myfolder\myinstaller.msi akey="12345678-abcd-1a1b-x9x1-a1b2c3d4e5f6"' -Wait -NoNewWindow

以上是我现在正在使用的代码。 注释掉的最后一行是有效的激活字符串。

我已经验证 $ActivationID 正在拉回正确的值,并且 $InstallString 反映了 Start-Process 字符串的注释版本中的参数列表。

任何帮助,将不胜感激!

首先,让我欢迎您使用 Powershell! 它是一种伟大的语言,也是一个围绕共同事业聚集的伟大社区。

由于您是该语言的新手,您仍然可以学习新技巧,这是一件好事,因为人们普遍认为 Write-Host cmdlet 几乎总是一个糟糕的选择。 如果你不相信我,你应该相信Powershell 的发明者

既然已经解决了,我们应该看看您的 MSI 命令。 有了Powershell,我们不用直接打开msiexec,直接调用MSI即可。 我会将安装程序的路径分解成它自己的变量,然后我们可以在它上面添加我们所有的参数。 另外,不要忘记“/qn”开关,它实际上会使所有这些静音。 总而言之,您的新脚本将如下所示:

[System.Xml.XmlDocument]$XML_Doc = new-object System.Xml.XmlDocument
$XML_Doc.load('c:\myfolder\Configinfo.XML')
$ActivationID = $XML_Doc.CONFIGINFO.SITEINFO.ACTIVATEID
Write-Verbose "Activation Id is: $ActivationID"
$msipath = "C:\myfolder\myinstaller.msi"
$args = @("akey=$ActivationID", "/qn")
Write-Verbose "Install path is $msipath"
Write-Verbose "Activation key is $akey"
Start-Process $msipath -ArgumentList  $args -Wait -NoNewWindow

Start-Process命令不是必需的。 PowerShell 是一个外壳。 它可以运行命令。 直接把你想运行的命令放在脚本里就行了。

msiexec /i "C:\myfolder\myinstaller.msi" "AKEY=$ActivationID"

我引用了msiexec.exe的参数,以防它们中的任何一个包含空格。 PowerShell 会自动将$ActivationID变量扩展为双引号内的字符串。

您的ArgumentList传递不正确。

[Xml]$XML_Doc = Get-Content -Path 'C:\myfolder\Configinfo.xml'

$ActivationID = $XML_Doc.CONFIGINFO.SITEINFO.ACTIVATEID
Write-Host "Activation Id is: $ActivationID"

$Path = 'msiexec'
$ArgList = @('/i','"C:\path\file.msi"',"akey=`"$ActivationID`"")
Write-Host "$Path $ArgList"

Start-Process -FilePath $Path -ArgumentList $ArgList -Wait -NoNewWindow

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM