Question

I'm having a problem happening only in windows... (Bless windows...)

If I run this script at powershell:

schtasks.exe /create /tn "Test_22"  /tr "powershell.exe C:\site.org\tasks\Test_22\Test_22.ps1"  /sc once /st 17:22 /sd (get-date (get-date -Date "2013-09-10") -Format dd/MM/yyyy) /ru System /rl HIGHEST  

Works exactly as I want! But that is only in the computers with locale at Spanish. The ones with locale as English failed, cause 10/09/2013 means October 9th 2013. And that is ok... the problem is, If I make this:

((Get-Culture).DateTimeFormat).ShortDatePattern

It does not returns MM/dd/yyyy as it should, but M/d/yyyy which is not correct, cause for some amazing reason, for schtasks.exe is not the same 9/10/2013 and 09/10/2013.

Is there any way I can fix this without having to make:

switch (((Get-Culture).DateTimeFormat).ShortDatePattern) {
   case M/d/yyyy : format = StupidWindows
}

Or something like that?

Worth mentioning I'm not an expert at powershell, so maybe the stupid one is me and not windows (although after a while investigating this, I'm very sure the stupid one is windows and not me...)

Était-ce utile?

La solution

How about this (broken into multiple lines using the backtick escape character for readability):

$StartDate = New-Object -TypeName DateTime -ArgumentList:(2013,09,10)
$FormatHack = ($([System.Globalization.DateTimeFormatInfo]::CurrentInfo.ShortDatePattern) -replace 'M+/', 'MM/') -replace 'd+/', 'dd/'

schtasks.exe /create 
    /tn "Test_22" `
    /tr "powershell.exe C:\site.org\tasks\Test_22\Test_22.ps1" `
    /sc once `
    /st 17:22 `
    /sd $StartDate.ToString($FormatHack) `
    /ru System `
    /rl HIGHEST

If you are using Powershell 3, look into the PSScheduledJobs module as well.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top