Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Variable (PowerShell) inside of a command

$computer = gc env:computername

# Argument /RU '$computer'\admin isn't working.
SchTasks /create /SC Daily /tn "Image Verification" /ST 18:00:00 /TR C:\bdr\ImageVerification\ImageVerification.exe /RU '$computer'\admin /RP password

Basically I need to provide the computer name in the scheduled task...

Thank you in advance!

like image 545
RogerLawrence Avatar asked Sep 12 '12 18:09

RogerLawrence


2 Answers

Single quoted strings will not expand variables in PowerShell. Try a double quoted string e.g.:

"$computer\admin"
like image 181
Keith Hill Avatar answered Sep 19 '22 15:09

Keith Hill


To complement Keith Hill's helpful answer with additional information:

Both "$computer\admin" and the unquoted form, $computer\admin, would work, because unquoted string arguments are implicitly treated as if they were "..."-enclosed (double-quoted), i.e. as expandable strings that perform string interpolation (replace embedded variable references and expressions with their values), as opposed to verbatim strings ('...', single-quoted) that do not interpret their content.

When in doubt, use "..." explicitly, notably when the string contains metacharacters such as | and <

For the complete rules on how unquoted tokens are parsed as command arguments, see this answer.

Pitfalls:

The partial quoting you attempted:

'$computer'\admin

even if corrected to "$computer"\admin to make interpolation work, would not work, because PowerShell - perhaps surprisingly - then passes the value of $computer and verbatim string \admin as two arguments. Only if a compound string with partial quoting starts with an unquoted string is it recognized as a single argument (e.g. $computer"\admin" would work) - see this answer for more information.

Another notable pitfall is that only stand-alone variable references such as $computer and $env:COMPUTERNAME can be embedded as-is in "..."; to embed an expression - which includes property access and indexed access - or a command, you need to enclose them in $(...), the subexpression operator. E.g., to embed the value of expression $someArray[0] or $someObj.someProp in an expandable string, you must use "$($someArray[0])" or "$($someObj.someProp)" - see this answer for the complete rules.

like image 35
mklement0 Avatar answered Sep 19 '22 15:09

mklement0