0

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"}'
2
  • What version of PowerShell are you running? Commented Jun 13, 2021 at 0:35
  • 1
    Remove Invoke-Expression and the single quotes, leaving just cmd /c start powe...... Commented Jun 13, 2021 at 1:21

1 Answer 1

1

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' }"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This helped getting the variable passed. I subsequently had got another error saying $host.ui.rawui.windowtitle was not recogonzed. I had to append that portion with a back tick to get everything working perfectly. Thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.