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

1
  • 2
    Variables are not interpolated in strings with single quotes. Use double quotes instead. Commented Sep 12, 2012 at 18:15

2 Answers 2

16

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

"$computer\admin"
Sign up to request clarification or add additional context in comments.

4 Comments

Or no quotes at all in this case. PowerShell will handle quoting automatically anyway.
Yup, that is what I would normally do. Just wanted to point out the diff between single and double-quoted strings. :-)
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?
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.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.