简体   繁体   English

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

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

I am new to Powershell and, of course, trying to learn on the fly for a project- No pressure, right!我是 Powershell 的新手,当然,我正在尝试为项目即时学习 - 没有压力,对吧! :-) :-)

I am working on a script to run an MSI package in quiet mode, passing it an activation code as an argument, that I have to extract from an XML file.我正在编写一个脚本,以安静模式运行 MSI 包,将激活代码作为参数传递给它,我必须从 XML 文件中提取该代码。

So far, I have everything working except for getting Start-Process to run the MSI with the arguments being passed in a Variable.到目前为止,除了让 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

Above is the code I am working with now.以上是我现在正在使用的代码。 The last line that is commented out is an activation string that works.注释掉的最后一行是有效的激活字符串。

I have verified that $ActivationID is pulling back the correct value, and that $InstallString mirrors the argument list in the commented version of the Start-Process string.我已经验证 $ActivationID 正在拉回正确的值,并且 $InstallString 反映了 Start-Process 字符串的注释版本中的参数列表。

Any help would be appreciated!任何帮助,将不胜感激!

First off, let me welcome you to Powershell!首先,让我欢迎您使用 Powershell! It's a great language and a great community gathered around a common cause.它是一种伟大的语言,也是一个围绕共同事业聚集的伟大社区。

Since you're new to the language, you can still learn new tricks and that's a good thing, because it's generally accepted that the Write-Host cmdlet is nearly always a poor choice.由于您是该语言的新手,您仍然可以学习新技巧,这是一件好事,因为人们普遍认为 Write-Host cmdlet 几乎总是一个糟糕的选择。 If you don't trust me, you should trust the inventor of Powershell .如果你不相信我,你应该相信Powershell 的发明者

Now that that's out of the way, we should look at your MSI command.既然已经解决了,我们应该看看您的 MSI 命令。 With Powershell, we don't have to directly open msiexec, and we can call the MSI directly.有了Powershell,我们不用直接打开msiexec,直接调用MSI即可。 I would break the path to the installer into its own variable, and then we can add all of our arguments on top of it.我会将安装程序的路径分解成它自己的变量,然后我们可以在它上面添加我们所有的参数。 Also, don't forget the "/qn" switch which will actually make all of this silent.另外,不要忘记“/qn”开关,它实际上会使所有这些静音。 All in all, your new script will look something like this:总而言之,您的新脚本将如下所示:

[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

The Start-Process commands aren't necessary. Start-Process命令不是必需的。 PowerShell is a shell. PowerShell 是一个外壳。 It can run commands.它可以运行命令。 Just put the commands you want to run directly in the script.直接把你想运行的命令放在脚本里就行了。

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

I quoted the parameters to msiexec.exe in case any of them contain spaces.我引用了msiexec.exe的参数,以防它们中的任何一个包含空格。 PowerShell will automatically expand the $ActivationID variable into the string inside the double quotes. PowerShell 会自动将$ActivationID变量扩展为双引号内的字符串。

Your ArgumentList is being passed incorrectly.您的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