Salut !
Essayez d'organiser votre script par des procédures et des fonctions comme ceci :
Option Explicit
' Déclaration variables
' variables objets
Dim wshnetwork, objWMIService, wshShell
' variables collections
Dim colAdapters, colItems
Dim objAdapter, objItem
' variables string
Dim strComputer,fso, strUserName, strPathDir, strComputerName
Dim TxtFile,MailLog,MsgHTML, strComputerDomain,Msg,Title,arrFiles,sFile
' Initialisation variables
Title = "Envoi de mail Infos Poste"
strComputer = "."
Set wshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
strUserName = wshShell.ExpandEnvironmentStrings("%USERNAME%")
strPathDir = wshShell.ExpandEnvironmentStrings( "%TEMP%" )
strComputerName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
strComputerDomain = wshShell.ExpandEnvironmentStrings("%USERDOMAIN%")
TxtFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "txt"
MailLog = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "log"
arrFiles = Array(TxtFile,MailLog)
For each sFile in arrFiles
if fso.FileExists(sFile) Then 'Si le fichier sFile existe
fso.DeleteFile sFile 'alors on le supprime
end If
Next
Msg = ""
Msg = Msg & "strUserName :" & vbTab & vbTab & strUserName & vbCrLf & _
"strPathDir :" & vbTab & vbTab & strPathDir & vbCrLf & _
"strComputerName :" & vbTab & vbTab & strComputerName & vbCrLf & _
"strComputerDomain :" & vbTab & strComputerDomain & vbCrLf & vbCrLf
Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objAdapter in colAdapters
'--------------------------INFO système d'exploitation---------------------------
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each objItem in colItems
Msg = Msg & "Machine Name:" & vbTab & vbTab & objItem.CSName & vbCrLf & _
"Manufacturer:" & vbTab & vbTab & objItem.Manufacturer & VbCrLf & _
"Systeme d'exploitation :" & vbTab & objItem.Caption & vbCrLf & _
"Editeur :" & vbTab & vbTab & vbTab & objItem.Manufacturer & vbCrLf & _
"Version :" & vbTab & vbTab & vbTab & objItem.Version & vbCrLf & _
"Service pack :" & vbTab & vbTab & objItem.CSDVersion & vbCrLf &_
"Architecture :" & vbTab & vbTab & objItem.OSArchitecture & vbCrLf & _
"GUID :" & vbTab & vbTab & vbTab & objItem.SerialNumber & vbCrLf & _
"Langue :" & vbTab & vbTab & vbTab & objItem.OSLanguage & vbCrLf & _
"LastBoot :" & vbTab & vbTab & vbTab & WMIDateStringToDate(objItem.LastBootUpTime)
Next
Next
Set colAdapters = Nothing
Set objWMIService = Nothing
Set wshnetwork = Nothing
Set wshShell = Nothing
Set colItems = Nothing
'***************************************************************************************
Function WMIDateStringToDate(dtmDate)
WMIDateStringToDate = CDate(Mid(dtmDate, 7, 2) & "/" & _
Mid(dtmDate, 5, 2) & "/" & Left(dtmDate, 4) _
& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function
'***************************************************************************************
Function EMail( myFrom, myTo, mySubject, myTextBody, myHTMLBody, myAttachment, mySMTPServer, mySMTPPort )
' This function sends an e-mail message using CDOSYS
'
' Arguments:
' myFrom = Sender's e-mail address ("John Doe <jdoe@mydomain.org>" or "jdoe@mydomain.org")
' myTo = Receiver's e-mail address ("John Doe <jdoe@mydomain.org>" or "jdoe@mydomain.org")
' mySubject = Message subject (optional)
' myTextBody = Actual message (text only, optional)
' myHTMLBody = Actual message (HTML, optional)
' myAttachment = Attachment as fully qualified file name, either string or array of strings (optional)
' mySMTPServer = SMTP server (IP address or host name)
' mySMTPPort = SMTP server port (optional, default 25)
'
' Returns:
' status message
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
' Standard housekeeping
Dim i, objEmail
' Use custom error handling
On Error Resume Next
' Create an e-mail message object
Set objEmail = CreateObject( "CDO.Message" )
' Fill in the field values
With objEmail
.From = myFrom
.To = myTo
' Other options you might want to add:
' .Cc = ...
' .Bcc = ...
.Subject = mySubject
.TextBody = myTextBody
.HTMLBody = myHTMLBody
If IsArray( myAttachment ) Then
For i = 0 To UBound( myAttachment )
.AddAttachment Replace( myAttachment( i ), "\", "\\" ),"",""
Next
ElseIf myAttachment <> "" Then
.AddAttachment Replace( myAttachment, "\", "\\" ),"",""
End If
If mySMTPPort = "" Then
mySMTPPort = 25
End If
With .Configuration.Fields
.Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ) = 2
.Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ) = mySMTPServer
.Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ) = mySMTPPort
.Update
End With
' Send the message
.Send
End With
' Return status message
If Err Then
EMail = "ERROR " & Err.Number & ": " & Err.Description
Err.Clear
Else
EMail = Now & " ==> Message sent ok !"
End If
' Release the e-mail message object
Set objEmail = Nothing
' Restore default error handling
On Error Goto 0
End Function
'***************************************************************************************
Function GMail( myFrom, myTo, mySubject, myTextBody, myHTMLBody, myAttachment ,myLogin , myPassword)
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM
Dim i, objEmail
' Use custom error handling
On Error Resume Next
' Create an e-mail message object
Set objEmail = CreateObject( "CDO.Message" )
' Fill in the field values
With objEmail
.From = myFrom
.To = myTo
' Other options you might want to add:
' .Cc = ...
' .Bcc = ...
.Subject = mySubject
.TextBody = myTextBody
.HTMLBody = myHTMLBody
If IsArray( myAttachment ) Then
For i = 0 To UBound( myAttachment )
.AddAttachment Replace( myAttachment( i ), "\", "\\" ),"",""
Next
ElseIf myAttachment <> "" Then
.AddAttachment Replace( myAttachment, "\", "\\" ),"",""
End If
With .Configuration.Fields
.Item( "http://schemas.microsoft.com/cdo/configuration/sendusing" ) = 2
.Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ) = "smtp.gmail.com"
.Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ) = 465
.Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate" ) = cdoBasic
.Item( "http://schemas.microsoft.com/cdo/configuration/sendusername" ) = myLogin
.Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword" ) = myPassword
.Item( "http://schemas.microsoft.com/cdo/configuration/smtpusessl" ) = True
.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout" ) = 60
.Update
End With
' Send the message
.Send
End With
' Return status message
If Err Then
GMail = "ERROR " & Err.Number & ": " & Err.Description
Err.Clear
Else
GMail = Now & " ==> Message sent ok !"
End If
' Release the e-mail message object
Set objEmail = Nothing
' Restore default error handling
On Error Goto 0
End Function
'***************************************************************************************
Sub WriteLog(strText,File)
Dim fso,ts
Const ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(File,ForAppending, True)
ts.WriteLine strText
ts.Close
End Sub
'***************************************************************************************
Function GetPublicIP()
Dim http : Set http = CreateObject( "MSXML2.ServerXmlHttp" )
http.Open "GET", "http://icanhazip.com", False
http.Send
GetPublicIP = http.responseText
Set http = Nothing
End Function
'***************************************************************************************
MsgBox Msg,vbInformation,Title
Call WriteLog(Msg,TxtFile)
MsgHTML = Replace(Msg,vbCrLf,"<br>")
Dim arrAttach : arrAttach = Array(TxtFile)
'EMail( myFrom, myTo, mySubject, myTextBody, myHTMLBody, myAttachment, mySMTPServer, mySMTPPort )
Call WriteLog( EMail( "User <User@orange.fr>","User <User@orange.fr>",GetPublicIP(),"Message TEXT!",MsgHTML,arrAttach,"smtp.orange.fr",25 ),MailLog )
'GMail( myFrom, myTo, mySubject, myTextBody, myHTMLBody, myAttachment ,myLogin , myPassword)
'Call WriteLog(GMail( "user@gmail.com","user2@gmail.com",GetPublicIP(),"Message TEXT!",MsgHTML,arrAttach,"VotreLoginGmail","VotrepassGmail"),MailLog)