VBS : Création d'une tâche planifié à une date précise

Résolu
SuperKoala42 - 13 mars 2013 à 08:57
 SuperKoala42 - 13 mars 2013 à 16:43
Bonjour à tous,

J'aimerais créer un script permettant la création d'une tâche planifié par rapport à une date précise (dernier jour du mois en cours, voir mon post précédent [url=http://www.vbfrance.com/forum/sujet-VBS-AIDE-MON-PREMIER-SCRIPT-RECUPERATION-DERNIER-JOUR_1630817.aspx] )

Voici mon code :
MyDate = DateSerial(Year(Date),Month(Date)+1,1)-1
MyDate = Right(MyDate,4) & Mid(MyDate,4,2) & Left(MyDate,2)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
    ("Notepad.exe", & MyDate &"123000.000000-420", _
        True , 1 OR 4 OR 16, , , JobID) 
Wscript.Echo errJobCreated


- J'ai du mal à comprendre à quoi sert la variable "strComputer" pour
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")


- Je pense avoir bien placé ma date via "123000.000000-420" dans :
errJobCreated = objNewJob.Create _
    ("Notepad.exe", & MyDate &"123000.000000-420", _
        True , 1 OR 4 OR 16, , , JobID)



Merci d'avance pour votre aide.

2 réponses

SuperKoala42
13 mars 2013 à 16:43
Bon apparement la doc de Microsoft nous dit que :

UTC time to run the job, in the form of "YYYYMMDDHHMMSS.MMMMMM(+-)OOO", where "YYYYMMDD" must be replaced by "********". The replacement is necessary because the scheduling service only allows jobs to be configured to run one time, or run on a day of the month or week. A job cannot be run on a specific date.

Donc j'ai contourné le problème comme cela :

Dim uneDate
Dim mTab
Dim paramMois
Dim paramTask

uneDate = FormatDateTime(DateSerial(Year(Date),Month(Date)+1,1)-1,vbShortDate)
Wscript.Echo "Voici le dernier jour du mois : " & uneDate
mTab = Split(Cstr (uneDate), "/")

paramMois = mtab(1)

Select Case paramMois
Case 31
paramTask = 1073741824
Case 30
paramTask = 536870912
Case 29
paramTask = 268435456
Case 28
paramTask = 134217728
Case Else
Wscript.Echo "False and close the programme"
Wscript.quit
End Select


strComputer = "."
Set objService = GetObject("winmgmts:\" & strComputer)
Set objNewJob = objService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
 ("Monitor.exe", "********123000.000000-420", _
 False , , paramTask, , JobID)
If Err.Number = 0 Then
 Wscript.Echo "New Job ID: " & JobID
Else
 Wscript.Echo "An error occurred: " & errJobCreated
End If


Je pense qu'il est possible d'optimiser le code, mais en tout cas ça marche :)!!! Il ne restera plus qu'a supprimer les tâches planifié qui se sont déjà exécuté.
3
SuperKoala42
13 mars 2013 à 10:29
Modification du code car :

MyDate = DateSerial(Year(Date),Month(Date)+1,1)-1
MyDate = Right(MyDate,4) & Mid(MyDate,4,2) & Left(MyDate,2)


retourne 2013/03/31/ sous XP

Voici mon code :
Dim uneDate
Dim mTab
Dim final

uneDate = FormatDateTime(DateSerial(Year(Date),Month(Date)+1,1)-1,vbShortDate)

mTab = Split(Cstr (uneDate), "/")
final = (mTab(2)+mTab(1)+mTab(0))


strComputer = "."
Set objService = GetObject("winmgmts:\" & strComputer)
Set objNewJob = objService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
 ("Notepad.exe", final & "123000.000000-420", _
 False , , , , JobID)
If Err.Number = 0 Then
 Wscript.Echo "New Job ID: " & JobID
Else
 Wscript.Echo "An error occurred: " & errJobCreated
End If


Mais je n'arrive pas à créer ma tâche le 31/03/13 uniquement..
J'ai même testé en dur via 20130331123000.000000-420 ( via les explications de Microsoft Tapez le texte de l'url ici.
0
Rejoignez-nous