简体   繁体   中英

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! :-)

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.

So far, I have everything working except for getting Start-Process to run the MSI with the arguments being passed in a Variable.

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.

Any help would be appreciated!

First off, let me welcome you to 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. If you don't trust me, you should trust the inventor of Powershell .

Now that that's out of the way, we should look at your MSI command. With Powershell, we don't have to directly open msiexec, and we can call the MSI directly. 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. 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. PowerShell is a shell. 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. PowerShell will automatically expand the $ActivationID variable into the string inside the double quotes.

Your ArgumentList is being passed incorrectly.

[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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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