Script de mises à jour Windows (VBFRANCE.COM)
Net Storm :: Codes & Tutoriels :: VB
Page 1 sur 1
Script de mises à jour Windows (VBFRANCE.COM)
Voici un script de mise à jour Windows. Source: Vb France.com
(Et non je ne suis pas assez calé en VB )
Merci d'avoir lu ce script jusqu'à la fin et j'espère que vous y trouverez une utilité
PS: Un logiciel déjà tout fait conçu par Microsoft effectue la même opération
(Et non je ne suis pas assez calé en VB )
- Code:
'OBJETS NECESSAIRES
Dim WshShell, sTmp, fso, f
Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateUpdateSearcher()
Set WshShell=CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
'FIN OBJETS NECESSAIRES
'CREATION RAPPORT
Const ForAppend = 8, ForWriting = 2
sTmp = WshShell.ExpandEnvironmentStrings("%TMP%\rapport.txt")
'sTmp = "c:\tmp\rapport.txt"
set f = fso.openTextFile(sTmp, ForWriting,true)
'RECUPERATION NOM SERVEUR
Set WshNetwork = WScript.CreateObject("WScript.Network")
NomMachine = WshNetwork.ComputerName
'FIN RECUPERATION NOM SERVEUR
f.writeline(Now & " Mise à jour du serveur : " & NomMachine)
f.writeline(" ")
'FIN CREATION RAPPORT
'RECHERCHE DES MISES A JOUR
updateSession.ClientApplicationID = "Recherche et installation des mises à jour microsoft"
WScript.Echo "Recherche des mises à jour..." & vbCRLF
Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
WScript.Echo "Liste des mises à jour applicables sur la machine:"
'f.writeline("- Liste des mises à jour applicables sur la machine:")
For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
WScript.Echo I + 1 & "> " & update.Title
' f.writeline(" " & I + 1 & "> " & update.Title)
Next
'f.writeline(" ")
If searchResult.Updates.Count = 0 Then
WScript.Echo "Il n'y a pas de mises à jour applicables."
f.writeline(" Il n'y a pas de mises à jour applicables.")
f.writeline(" ")
f.close
'APPEL FONCTION ENVOI MAIL
'call SendMail("mon@mail.fr", "Rapport du serveur " & NomMachine, "Rapport de la machine " & NomMachine)
WScript.Quit
End If
'FIN RECHERCHE DES MISES A JOUR
'CREATION DE LA LISTE DES MISES A JOUR ET LES TELECHARGER
WScript.Echo vbCRLF & "Création d'une liste des mises à jour à télécharger:"
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
For I = 0 to searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
addThisUpdate = false
If update.InstallationBehavior.CanRequestUserInput = true Then
WScript.Echo I + 1 & "> Annulée: " & update.Title & " parce qu'elle nécessite l'intervention de l'utilisateur"
f.writeline("- " & update.Title & " a été annulée car elle nécessite l'intervention de l'utilisateur")
Else
If update.EulaAccepted = false Then
update.AcceptEula()
addThisUpdate = true
Else
addThisUpdate = true
End If
End If
If addThisUpdate = true Then
WScript.Echo I + 1 & "> Ajout: " & update.Title
updatesToDownload.Add(update)
End If
Next
If updatesToDownload.Count = 0 Then
WScript.Echo "Toutes les mises à jour applicables ont été ignorés."
f.writeline(" ")
f.writeline("- Toutes les mises à jour applicables ont été ignorés.")
f.close
'APPEL FONCTION ENVOI MAIL
'call SendMail("mon@mail.fr", "Rapport du serveur " & NomMachine, "Rapport de la machine " & NomMachine)
WScript.Quit
End If
WScript.Echo vbCRLF & "Téléchargement des mises à jour..."
Set downloader = updateSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload
downloader.Download()
'FIN CREATION DE LA LISTE DES MISES A JOUR ET LES TELECHARGER
'INSTALLATION DES MISES A JOURS
Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
rebootMayBeRequired = false
WScript.Echo vbCRLF & "Mises à jour téléchargés avec succés:"
For I = 0 To searchResult.Updates.Count-1
set update = searchResult.Updates.Item(I)
If update.IsDownloaded = true Then
WScript.Echo I + 1 & "> " & update.Title
updatesToInstall.Add(update)
If update.InstallationBehavior.RebootBehavior > 0 Then
rebootMayBeRequired = true
End If
End If
Next
If updatesToInstall.Count = 0 Then
WScript.Echo "Les mises à jour ne ce sont pas téléchargés."
f.writeline(" ")
f.writeline("- Les mises à jour ne ce sont pas téléchargés.")
f.close
'APPEL FONCTION ENVOI MAIL
'call SendMail("mon@mail.fr", "Rapport du serveur " & NomMachine, "Rapport de la machine " & NomMachine)
WScript.Quit
End If
If rebootMayBeRequired = true Then
WScript.Echo vbCRLF & "Ces mises à jour peuvent nécessiter un redémarrage."
End If
strInput = "Y"
If (strInput = "Y" or strInput = "y") Then
WScript.Echo "Installation des mises à jour..."
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()
'RESULTAT DE L'INSTALLATION
WScript.Echo "Résultat de l'installation: " & _
installationResult.ResultCode
WScript.Echo "Redémarrage requis: " & installationResult.RebootRequired & vbCRLF
WScript.Echo "Liste des mises à jour installées " & "et les résultats d'installation individuels:"
f.writeline(" ")
'RESULTAT POUR LES MISES A JOUR INSTALLEES CORECTEMENT
f.writeline("Liste des mises à jour installées : ")
For I = 0 to updatesToInstall.Count - 1
If installationResult.GetUpdateResult(i).ResultCode = 2 then
WScript.Echo I + 1 & "> " & _
updatesToInstall.Item(i).Title & _
": " & installationResult.GetUpdateResult(i).ResultCode
f.writeline(" " & I + 1 & "> " & updatesToInstall.Item(i).Title & ": " & installationResult.GetUpdateResult(i).ResultCode)
End If
Next
'RESULTAT POUR LES MISES A JOUR EN ERREUR
f.writeline("Liste des mises en erreur avec leurs codes erreur : ")
For I = 0 to updatesToInstall.Count - 1
If installationResult.GetUpdateResult(i).ResultCode <> 2 then
WScript.Echo I + 1 & "> " & _
updatesToInstall.Item(i).Title & _
": " & installationResult.GetUpdateResult(i).ResultCode
f.writeline(" " & I + 1 & "> " & updatesToInstall.Item(i).Title & ": " & installationResult.GetUpdateResult(i).ResultCode)
Set WshShell = createobject("wscript.shell")
Set oExec = WshShell.Exec("SnmpTrapGen.exe -r:@IPAGENTSNMP -v:1 -c:""public"" -to:.1.3.6.1.2.1.1.1.0")
End If
Next
'FIN RESULTAT DE L'INSTALLATION
End If
'FIN INSTALLATION DES MISES A JOURS
f.close
'APPEL FONCTION ENVOI MAIL
'call SendMail("mon@mail.fr", "Rapport du serveur " & NomMachine, "Rapport de la machine " & NomMachine)
'REDEMARRAGE DE LA MACHINE
'If installationResult.RebootRequired = true then
' Set objShell = CreateObject("WScript.Shell")
' objShell.run "shutdown -t 5 -r"
'End If
'FIN REDEMARRAGE DE LA MACHINE
'FIN DU SCRIPT
'FONCTION ENVOI MAIL
Function SendMail(strTo, strBody, strTitre)
Dim WshShell, sTmp
Set WshShell=CreateObject("WScript.Shell")
sTmp = WshShell.ExpandEnvironmentStrings("%TMP%\rapport.txt")
Set objMail = CreateObject("CDO.Message")
Set objConfig = CreateObject("CDO.configuration")
Set objFields = objConfig.Fields
With objFields
.Item("http://schemas.microsoft.com/cdo/configuration/SendUsing")= 2 'Définit le type d'envoi en SMTP
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")= "mon.serveurmail.fr" 'Serveur de mail
.Item("http://schemas.microsoft.com/cdo/configuration/SMTPServerPort")= 25 'Port du serveur smtp
.Update
End With
With objMail
Set .Configuration = objConfig
.To = strTo 'adresse destinataire
.Cc = strCc
.Bcc = strBcc
.AddAttachment(sTmp) 'Piece jointe
.From = "updateserver" & NomMachine & "@mon.domaine.fr" 'adresse expéditeur
.Subject = strTitre 'Objet
.HTMLBody = strBody 'Corp du message
.Send
End With
End Function
'FIN FONCTION ENVOI MAIL
Merci d'avoir lu ce script jusqu'à la fin et j'espère que vous y trouverez une utilité
PS: Un logiciel déjà tout fait conçu par Microsoft effectue la même opération
Net Storm :: Codes & Tutoriels :: VB
Page 1 sur 1
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum
|
|