I'm currently creating a script that require to schedule a reboot on a remote computer at 3AM on the next day. Since I can't be sure that all computers on our network use Powershell v3 or higher, I can't rely on New-ScheduledTask cmdlet. Therefore, I decided to use schtasks. For some unknown reason, when I use a variable with the /sd parameter of schtasks, I receive an error saying that a value is attended for /sd. But, if I type the day manually (example, 2015-08-13), it works perfectly.
So, this is my problem. To make a long story short...
PS U:\> $tomorrowDate = ((Get-Date).AddDays(1)).ToString(("yyyy-MM-dd"))
PS U:\> $tomorrowDate | gm
TypeName: System.String
PS U:\> Invoke-Command -computername p199403.mydomain -ErrorAction stop -ScriptBlock { schtasks /F /create /sc once /tn "Reboot to delete offline cache" /tr "shutdown -r -f" /sd $tomorrowDate /st 03:00 }
Erreur: Syntaxe incorrecte. Valeur attendue pour '/sd'.
#Translation : Error : Invalid Syntax. Value required for '/sd'
doesn't work, but the code below is working (as you can see, I "enforced" my date as a string to be sure to test with the same type of "variable") :
PS U:\> Invoke-Command -computername p199403.mydomain -ErrorAction stop -ScriptBlock { schtasks /F /create /sc once /tn "Reboot to d
elete offline cache" /tr "shutdown -r -f" /sd ""2015-08-13"" /st 03:00 }
Op'ration r'ussie : la tƒche planifi'e "Reboot to delete offline cache" a 't' cr''e.
# Translation : Operation successful : The task "Reboot to delete offline cache" is created
I can't see where the problem, so any suggestion or recommandation would be appreciated.