Reputation: 363
I have a simple PowerShell script (.ps1 file, detailed below) and I'm trying to pass a variable that needs to be present in the window title and also a log file name. The script runs, but the places where the variable's string is suppose to be erroneously shows up blank (i.e., "session-" and "log-20210612_155506-session-.log"). What is the correct way to pass this variable??
$mysessionID = [guid]::NewGuid().toString().ToLower()
invoke-expression 'cmd /c start powershell -NoExit -Command {cd .\;$host.ui.RawUI.WindowTitle = "session-$mysessionID"; start-sleep 0 ; .\chia_plot.exe -p 987d987987987fd879879 -f 9x79879f987987sd -t D:\ -d R:\ -n -1 -r 4 -u 128 | Tee-Object -FilePath ".\logs\log-$(get-date -f yyyyMMdd_HHmmss)-session-$mysessionID.log"}'
Upvotes: 0
Views: 182
Reputation: 2280
The problem is that you start the string passed to Invoke-Expression
with a single quote. In Powershell, the single quote does not allow string interpolation, but the double quote does. You should change the Invoke-Expression
call to start with double quotes, but this will require that you change the inner double quotes to single quotes. For example:
Invoke-Expression "cmd /c start powershell -NoExit -Command { cd .\; $host.ui.RawUI.WindowTitle = 'session-$mysessionID'; start-sleep 0; .\chia_plot.exe -p 987d987987987fd879879 -f 9x79879f987987sd -t D:\ -d R:\ -n -1 -r 4 -u 128 | Tee-Object -FilePath '.\logs\log-$(get-date -f yyyyMMdd_HHmmss)-session-$mysessionID.log' }"
Upvotes: 1