12
$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!

mklement0
  • 382,024
  • 64
  • 607
  • 775
RogerLawrence
  • 135
  • 1
  • 2
  • 9
  • 2
    Variables are not interpolated in strings with single quotes. Use double quotes instead. – Lee Sep 12 '12 at 18:15

2 Answers2

16

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

"$computer\admin"
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 4
    Or no quotes at all in this case. PowerShell will handle quoting automatically anyway. – Joey Sep 12 '12 at 18:19
  • 2
    Yup, that is what I would normally do. Just wanted to point out the diff between single and double-quoted strings. :-) – Keith Hill Sep 12 '12 at 18:37
  • I get an error: ERROR: Logon Failure: unknown username or password... Yet I know that the user exists any thoughts? I am doing this correctly? – RogerLawrence Sep 12 '12 at 19:34
  • If you use /RU it looks like the username needs to be something like "NT AUTHORITY\LocalService". Perhaps you do not want to use the "run as" account and use just /U and /P instead? Also the time spec should be just 18:00 according the usage. – Keith Hill Sep 12 '12 at 19:51
1

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775