Apr 26 2022 11:50 AM
Hi everyone, I am struggling with the use of Start-Process with an argument list.
It may seem obvious for some people but I find it hard to see what is going on here.
I have to wait for an installation to end before going to the next step hence the Start-Process.
Here is the code:
#Copy File
$bkFolder = "D:\Temp"
$bkFolderExist = Test-Path $bkFolder
$OptionFile = "D:\Temp\Option.txt"
$fileExist = Test-Path $OptionFile
If ($bkFolderExist -eq $false) {New-Item -ItemType Directory -Path "d:\Temp" -Force}
If ($fileExist -eq $true) {Copy-Item $dsm $bkFolder}
else {$env:COMPUTERNAME | Out-File -FilePath %temp%\Log.txt -Append
Exit}
#Stop service
Stop-Service "Service"
#Update
Start-Process "install.exe" -Wait -ArgumentList "/s, /v`"INSTALLDIR="D:\Software\App" AllUser=1 RebootYeNo="No" AddLocal="Runtime" REBOOT=ReallySuppress /qn /l*v "C:\Temp\Log.txt"`""
#Copy le fichier dsm dans baclient
$oldFile = "D:\Temp\File.txt"
$Folder = "D:\Folder"
if ($fileExist -eq $false) {Copy-Item $oldFile $Folder}
#Start service
Start-Service "Service"
My problem is that I get an error powershell.exe : Start-Process : A positional parameter cannot be found that accepts argument 'INSTALLDIR=D:\Software\App.
It is probably a problem with using some quotes.
If there is another way to start a process and wait for it to end before going forward, that will work also.
Thank a bunch for the help!
Mathieu
Apr 26 2022 05:19 PM - edited Apr 26 2022 05:25 PM
Solution
Hey, Mathieu.
Here's some tips:
For point 1, you can either fix the escaping (the only un-escaped double quotes would be at the very start and end with everything in between being escaped) or simply avoid escaping altogether by removing all the escape characters and using single quotes around the entire ArgumentList string. This allows you to use double quotes inside the string "normally".
For example, this:
`"INSTALLDIR="D:\Software\App" AllUser=1 RebootYeNo="No" AddLocal="Runtime" REBOOT=ReallySuppress /qn /l*v "C:\Temp\Log.txt"`""
Would become this (using the single quote approach):
'INSTALLDIR="D:\Software\App" AllUser=1 RebootYeNo="No" AddLocal="Runtime" REBOOT=ReallySuppress /qn /l*v "C:\Temp\Log.txt"'
Point 2 can be used to your advantage insofar as it means you can pass in smaller strings where it's much easier then to read and deal with any required escaping.
For example:
-ArgumentList @("/s", "/v", 'INSTALLDIR="D:\Software\App"', "AllUser=1", 'RebootYeNo="No"', 'AddLocal="Runtime"', "REBOOT=ReallySuppress", "/qn", '/l*v "C:\Temp\Log.txt"')
It's worth noting most of your arguments don't actually need double quotes since their values don't contain spaces, so my examples above are somewhat contrived. You can simply leave all of them out.
Also, is RebootYeNo meant to be RebootYesNo? I', not sure either way but figured it was worth a shout out.
Cheers,
Lain
Apr 27 2022 12:08 PM
Apr 26 2022 05:19 PM - edited Apr 26 2022 05:25 PM
Solution
Hey, Mathieu.
Here's some tips:
For point 1, you can either fix the escaping (the only un-escaped double quotes would be at the very start and end with everything in between being escaped) or simply avoid escaping altogether by removing all the escape characters and using single quotes around the entire ArgumentList string. This allows you to use double quotes inside the string "normally".
For example, this:
`"INSTALLDIR="D:\Software\App" AllUser=1 RebootYeNo="No" AddLocal="Runtime" REBOOT=ReallySuppress /qn /l*v "C:\Temp\Log.txt"`""
Would become this (using the single quote approach):
'INSTALLDIR="D:\Software\App" AllUser=1 RebootYeNo="No" AddLocal="Runtime" REBOOT=ReallySuppress /qn /l*v "C:\Temp\Log.txt"'
Point 2 can be used to your advantage insofar as it means you can pass in smaller strings where it's much easier then to read and deal with any required escaping.
For example:
-ArgumentList @("/s", "/v", 'INSTALLDIR="D:\Software\App"', "AllUser=1", 'RebootYeNo="No"', 'AddLocal="Runtime"', "REBOOT=ReallySuppress", "/qn", '/l*v "C:\Temp\Log.txt"')
It's worth noting most of your arguments don't actually need double quotes since their values don't contain spaces, so my examples above are somewhat contrived. You can simply leave all of them out.
Also, is RebootYeNo meant to be RebootYesNo? I', not sure either way but figured it was worth a shout out.
Cheers,
Lain