Still getting a few of the nuances with Powershell.
Here’s the culprit line in my script:
invoke-command -Session $session -ScriptBlock {schtasks /create /RU `"SYSTEM`" /TN $using:TaskName /TR `"net share $using:ShareName /DELETE`" /V1 /Z /SC ONCE /ST 01:00 /SD $using:Date}
It works, but as you can tell, it’s ugly. I planned on splitting it by putting the whole scriptblock in a local var (let’s call it $schtasks) and with $using: I would refer to it in the invoke-command’s scriptblock as such:
$schtasks = schtasks /create /RU `"SYSTEM`" /TN $using:TaskName /TR `"net share $using:ShareName /DELETE`" /V1 /Z /SC ONCE /ST 01:00 /SD $using:Date invoke-command -Session $session -ScriptBlock {$schtasks}
However, if I do that, now I’m in a bit of a pickle:
[ul]If I put everything in $schtasks in double quotes the result is it echos it as a string (naturally).[/ul]
[ul]If I don’t put everything in $schtasks in double quotes, when it declares the variable it performs the whole command locally before I even have a chance to use it in the invoke-command.[/ul]
[ul]I suspect that if I could expand $schtasks properly, I need to make sure the variables inside of it ($using:ShareName, $using:Date) also expand properly.[/ul]
So what’s the best way to approach this?