Variable in a start process command

Hello,

I have this code, and I can’t seem to get the logfile from the start-process command to output using hostname-kace_install.log. It outputs it literally like $Hostname-Kace_install.log. In the Out-File command it works like I want. I have tried changing the ’ quotes around the entire expression to ", but then I get an error.

"/i "C:\temp\ampagent.msi" HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log "C:\temp\$Hostname-Kace_install.log" /quiet"

Invoke-Command -ComputerName $RemoteComputer -ScriptBlock {
                    $Hostname = hostname
                    Start-Process "msiexec.exe" -ArgumentList '/i "C:\temp\ampagent.msi" HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log "C:\temp\$Hostname-Kace_install.log" /quiet' -Wait -NoNewWindow
                    Get-Service -DisplayName "*Kace*"
                    Get-Service -DisplayName "*Kace*" | Out-File -FilePath "C:\Temp\$Hostname-KaceService.txt"
                    Read-Host -Prompt "Press Enter key to continue"
                        }

Thanks for help,

Matt

You need to put the double quotes outside and the single quotes inside …

Start-Process "msiexec.exe" -ArgumentList "/i 'C:\temp\ampagent.msi' HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log 'C:\temp\$Hostname-Kace_install.log' /quiet" -Wait -NoNewWindow

Variables wont be expanded if they are inside single quotes, even when used in double quotes if the entire expression is wrapped again in single quotes.

$Hostname = hostname
Write-Output "this expands $Hotname"

Write-Output 'Inside single quotes "$Hotname" it wont expand even with double quotes'

so you can build the string to handle this situation either using appending or formating.

#Appending
... -ArgumentList ('/i "C:\temp\ampagent.msi" HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log "C:\temp\' + $Hostname + '-Kace_install.log" /quiet')

#Formating
$Argument = '/i "C:\temp\ampagent.msi" HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log "C:\temp\{0}-Kace_install.log" /quiet' -f $Hostname

... -ArgumentList $Argument

ok Ill try those as well kvprasoon, and Thanks Olaf as well.

I tried Olafs suggestion , tried this:

$RemoteComputer = Read-Host -Prompt 'Please enter computer name to Install Kace on'
$SessionRemote = New-PSSession $RemoteComputer
Copy-Item -path 'C:\Temp\ITScript\KaceInstall\ampagent.msi' -Destination 'c:\Temp' -ToSession $SessionRemote
                    Invoke-Command -ComputerName $RemoteComputer -ScriptBlock {
                    $Hostname = hostname
                    Start-Process "msiexec.exe" -ArgumentList "/i "C:\temp\ampagent.msi" HOST=xxxx.xxxxxx.xxx NOHOOKS=1 /qn /norestart /log 'C:\temp\$Hostname-Kace_install.log' /quiet" -Wait -NoNewWindow
                    Get-Service -DisplayName "*Kace*"
                    Get-Service -DisplayName "*Kace*" | Out-File -FilePath "C:\Temp\$Hostname-KaceService.txt"
                    Read-Host -Prompt "Press Enter key to continue"
                        }

and get this:

Copy-Item "C:\Temp\*-KaceService.txt" -Destination "C:\Temp\ITScript\KaceInstall\RemoteInstallLogs\" -FromSession $SessionRemote
Write-Host "Logged to C:\Temp\ITScript\KaceInstall\RemoteInstallLogs\$RemoteComputer-KaceService.txt"
Please enter computer name to Install Kace on: testserver
A positional parameter cannot be found that accepts argument 'C:\temp\ampagent.msi
HOST=xxxxx.xxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log 'C:\temp\TESTServer-Kace_install.log' /quiet'.
    + CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
    + PSComputerName        : testserver

I’d say there are even more ways to deal with such a situation. :wink:

You could make it a real argument list

Start-Process "msiexec.exe" -ArgumentList "/i 'C:\temp\ampagent.msi'", 'HOST=xxx.xxxxxxxxx.xxx', 'NOHOOKS=1',  '/qn', '/norestart' "/log 'C:\temp\$Hostname-Kace_install.log'", '/quiet' -Wait -NoNewWindow

… or you could use only double quotes and escape the inner ones …

Start-Process "msiexec.exe" -ArgumentList "/i ""C:\temp\ampagent.msi"" HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log ""C:\temp\$Hostname-Kace_install.log"" /quiet" -Wait -NoNewWindow

… or if the path you want to provide does not have any speces you could ommit the inner quotes …

Start-Process "msiexec.exe" -ArgumentList "/i C:\temp\ampagent.msi HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log C:\temp\$Hostname-Kace_install.log /quiet" -Wait -NoNewWindow

The code you posted is not what I suggested. :wink:

… and copying files on your local computer does affect the remote computer in any way! :point_up_2:t4: :smirk:

eh? I thought it was!

Anyway, this:
… or you could use only double quotes and escape the inner ones …

works great, thanks again.

-Matt

Just to show what the mistake was …

I recommended this:

Start-Process "msiexec.exe" -ArgumentList "/i 'C:\temp\ampagent.msi' HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log 'C:\temp\$Hostname-Kace_install.log' /quiet" -Wait -NoNewWindow

and what you posted was this:

Start-Process "msiexec.exe" -ArgumentList "/i "C:\temp\ampagent.msi" HOST=xxxx.xxxxxx.xxx NOHOOKS=1 /qn /norestart /log 'C:\temp\$Hostname-Kace_install.log' /quiet" -Wait -NoNewWindow