Bonjour à tous,

J'ai récupéré un vieux logon script que j'avais fait il y a 13 ans , mais depuis j'ai totalement arrêté vsbcript pour powershell sans être devenu un expert non plus en powershell.
Vu qu'il me prendrait trop de temps de réécrire ce script en powershell, j'ai décidé de le reprendre et d'y ajouter une seule fonction dont j'ai besoin.
Seulement je rencontre un problème à l’exécution de mon script et je sollicite votre aide pour corriger ce problème.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
'On Error Resume Next
 
' Script d'ouverture de session 
'
 
' D�claration des variables g�n�rales
 
Dim strComputer,OsInfo,IpInfoLocal,sProfil,userSid,LogDir,NetPath,LocalFile,OutFile,ProfilDir
Dim Current,ComputerName,sUsername,sDom,sDC,Hw,Cpu,Model,Marq,Tag,Mem
Dim objFSO,objTextFile,Env,IpForti
 
' constantes
 
Const ForWriting = 2
Const OverwriteExisting = True
Const ForAppend = 8
 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' COLLECTE DES PARAMETRES GENERAUX
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
strComputer = "."
 
' date et heure
Current = TimeHHMMSS()
 
' Initialisation des variables d'enrionnement
 
Env = SetENV()
 
LogDir = Env(0)
ComputerName = Env(1)
sUsername = Env(2)
sDC = Env(3)
sDom = Env(4)
ProfilDir = Env(5)
 
 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' COLLECTE DES INFOS SYSTEMES / RESEAU / MACHINE / UTILISATEURS / SESSION-PROFIL
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
'User Sid
userSid = getSid()
 
'User DN
strDN = getDN()
 
' Password Last Set
strPasswdLastSet = pwdLastSet()
 
' Hardware Marque Model S/N
Hw = HWinfo()
 
Cpu = Hw(0)
Marq = Hw(1)
Model = Hw(2)
Tag = Hw(3)
Mem = Hw(4)
 
'Os info 
OSinfo = OSType ()
 
'configuration  IP du poste
IpInfoLocal = IPConfig()
IpForti = FortiIP()
 
' Profil Status
sProfil = ProfilState()
 
 
 
 
 
'Fichier de Log Local et R�seau
 
NetPath = "\\svr-file10\login$\"
 
' Si connexion VPN nommer le fichier avec l'IP de l'interface VPN SSL
If IsNull(IpForti) Then
OutFile = NetPath & ComputerName & "__" & sUsername & "__" & IpInfoLocal & "__" & Current & "_.log"
Else
'WScript.Echo "IP Address: "  & IpForti
OutFile = NetPath & ComputerName & "__" & sUsername & "__" &  IpForti  & "__" & Current & "_.log"
end If
 
Set WshShell = CreateObject("WScript.Shell")
 
UserPath = WshShell.ExpandEnvironmentStrings("%userprofile%") & "\session_log"
Set objFSO = CreateObject("scripting.filesystemobject")
 
If objFSO.FolderExists(UserPath) = False Then
         objFSO.CreateFolder(UserPath)
End If
 
 
LocalFile = LogDir & "\" & Current & "_.log"
 
Set objTextFile = objFSO.CreateTextFile(LocalFile, True) 
 
 
 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ECRITURE DES RESULTATS DE REQUETTE DANS LE FICHIER DE LOG
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
objTextFile.WriteLine Vbtab & "######################## INFOS ORDINATEUR ########################"
objTextFile.WriteBlankLines (2)
objTextFile.WriteLine "Computer						" & Vbtab & ": " & ComputerName
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "Constructeur					" & Vbtab & ": " & Marq
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "Model						" & Vbtab & ": " & Model
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "S/N							" & Vbtab & ": " & Tag
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "CPU							" & Vbtab & ": " & Cpu
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "Physical Memory				" & Vbtab & ": " & Mem & " MB "
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "OS							" & Vbtab & ": " & OSinfo
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "Adresse IP Locale			" & Vbtab & ": " & IpInfoLocal
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "Adresse IP Forticlient		" & Vbtab & ": " & " A venir "
objTextFile.WriteBlankLines (2)
objTextFile.WriteLine Vbtab & "######################## INFOS UTILISATEUR ########################"
objTextFile.WriteBlankLines (2)
objTextFile.WriteLine "Domaine						" & Vbtab & ": " & sDom
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "Logon Server					" & Vbtab & ": " & sDC
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "Username						" & Vbtab & ": " & sUsername
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "Status du profil				" & Vbtab & ": " & sProfil
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "Chemin du profil				" & Vbtab & ": " & ProfilDir
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "SID du compte				" & Vbtab & ": " & userSid
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "DN du compte utilisateur		" & Vbtab & ": " & strDN
objTextFile.WriteBlankLines (1)
objTextFile.WriteLine "Password Last Set			" & Vbtab & ": " & strPasswdLastSet
 
 
If Not (IsServer () = True) Then 
	objTextFile.WriteBlankLines (2)
	objTextFile.WriteLine "RSOP Information : " 
	getRSOP()
	objTextFile.WriteBlankLines (1)
End If 
 
'/******************* REMONTE LES HOTFIX INSTALLES SUR LE POSTE **************************/
'objTextFile.WriteBlankLines (2)
'objTextFile.WriteLine "Hot Fix installed : "
'objTextFile.WriteLine "=============================="
'objTextFile.WriteBlankLines (1)
 
'Call GetHotFix()
 
objTextFile.Close 
 
 
 
'Copie du fichier sur le share avec indication du nom d'ordinateur du login utilisateur et horodatage dans le nom du fichier
objFSO.CopyFile LocalFile , Outfile , OverwriteExisting
 
 
 
' Vide le contenu du session_log ant�rieur � 60 jours
 
Call cleanLog(LogDir,NetPath)
 
 
 
 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''' BLOCS DE FONCTIONS '''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
' Fonction heure
 
 
Function TimeHHMMSS()
 
    Dim retv, d
    d = Now
    retv = Right("00" & Hour(d), 2) & "-" & Right("00" & Minute(d), 2) & "-" & Right("00" & Second(d), 2)
    TimeHHMMSS = Day(d) & "-" & Month(d) & "-" & Year(d) & "_" & retv
 
End Function
 
 
' Fonction RegValueExists
 
Function RegValueExists(sRegValue)
     ' Returns True or False based of the existence of a registry value.
     Dim objShell
     Dim  RegReadReturn 
     Set objShell = CreateObject("WScript.shell")
     RegValueExists = True  ' init value
     On Error Resume Next
     RegReadReturn = objShell.RegRead(sRegValue)
     If Err.Number <> 0 Then
       RegValueExists = False
     End if
     On Error Goto 0
 
     Set objShell = Nothing
 
End Function
 
 
' Function IPConfig 
 
Function IPConfig () 
Dim  objWMIService,IPItems
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set IPItems = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
 
		For Each IPConfig In IPItems
			If Not IsNull(IPConfig.IPAddress) Then
				For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
					If varIP="" Then
						varIP=IPConfig.IPAddress(0)
					End If
				Next
			End If
		Next
 
IPConfig = varIP
 
End Function 
 
' Function IP Forticlient
 
Function FortiIP()
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
 
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")
 
For Each objItem in colItems
 
If  objItem.Description = "Fortinet SSL VPN Virtual Ethernet Adapter"  Or  objItem.Description = "Fortinet Virtual Ethernet Adapter (NDIS 6.30)" Then
	If Not IsNull(objItem.IPAddress(0)) then
		FortiIP = objItem.IPAddress(0)
	End if
End if
 
Next
 
 
End Function 
 
 
' Fonction getSID
 
Function getSid()
 
Dim sUsername,sDomain,oWMI,objAccount
 
Set WshShell = CreateObject("WScript.Shell")
sUsername = wshShell.ExpandEnvironmentStrings("%USERNAME%")
sDomain = wshShell.ExpandEnvironmentStrings("%USERDOMAIN%")
 
Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
 
Set objAccount = oWMI.Get("Win32_UserAccount.Name='" & sUsername & "',Domain='" & sDomain & "'")
 
getSID = objAccount.SID
 
Set oWMI = Nothing
Set WshShell = Nothing
 
End Function
 
'Fonction GetDN()  // DistinguishedName
 
Function getDN()
 
Set oTranslate = CreateObject("NameTranslate")
Set oNetwork = CreateObject("WScript.Network")
oTranslate.Init 3,""
oTranslate.Set 3, oNetwork.UserDomain & "\" & oNetwork.UserName
getDN = oTranslate.Get(1)
 
Set oTranslate = Nothing
Set oNetwork = Nothing
 
End Function
 
Function pwdLastSet()
Set objUser = GetObject _
    ("LDAP://" & strDN)
 
pwdLastSet = objUser.PasswordLastChanged
 
End Function
 
' Fonction OSType
 
Function OSType ()
 
Dim  Reg_OS_1, Reg_OS_2, Reg_OS_3,Reg_OS_4, Reg_Bool
Dim  WshShell
 
Reg_OS_1 = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\" & "ProductName"
Reg_OS_2 = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\" & "CSDVersion"
Reg_OS_3 = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\" & "CurrentVersion"
Reg_OS_4 = "HKLM\SOFTWARE\Wow6432Node\"
Reg_OS_5 = "No Service Pack"
 
Set WshShell = CreateObject("WScript.shell")
 
If RegValueExists(Reg_OS_2) then
 
	If RegValueExists(Reg_OS_4) then
 
	Reg_Bool = "x64"
	OsType = WshShell.RegRead (Reg_OS_1) & "  " & WshShell.RegRead (Reg_OS_2) & "  " & WshShell.RegRead (Reg_OS_3) & "  " & Reg_Bool
 
	Else
 
	Reg_Bool = "x86"
	OsType = WshShell.RegRead (Reg_OS_1) & "  " & WshShell.RegRead (Reg_OS_2) & "  " & WshShell.RegRead (Reg_OS_3) & "  " & Reg_Bool
 
	End If
 
Else
 
	If RegValueExists(Reg_OS_4) then
 
	Reg_Bool = "x64"
	OsType = WshShell.RegRead (Reg_OS_1) & "  " & Reg_OS_5 & "  " & WshShell.RegRead (Reg_OS_3) & "  " & Reg_Bool
 
	Else
 
	Reg_Bool = "x86"
	OsType = WshShell.RegRead (Reg_OS_1) & "  " & Reg_OS_5 & "  " & WshShell.RegRead (Reg_OS_3) & "  " & Reg_Bool
 
	End If
 
End if
 
Set WshShell = Nothing 
 
End Function
 
 
 
' Fonction profil status
 
 
Function ProfilState()
 
Dim WshShell
Dim KeyProfileStatus,Etat
 
Set WshShell = CreateObject("WScript.shell")
 
 
KeyProfileStatus = WshShell.RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\" & userSid & "\State")
 
 
		Select case KeyProfileStatus
			Case 0
			 Etat = "Normal"
			Case 16
			 Etat = "Itinerant"
			Case 20 
			 Etat = "Itinerant" & Vbtab & "1er ouverture de Session - Profil local supprim�"
			Case 24 
			 Etat = "Itinerant" & Vbtab & "Profil serveur supprim�"
			Case 262149 
			 Etat = "Mandatory" & Vbtab & "1er ouverture de Session"
			Case 393217 
			 Etat = "Mandatory"
			Case 516 
			 Etat = "Normal" & Vbtab & "1er ouverture de Session"	
			Case 540 
			 Etat = "Itinerant" & Vbtab & "1er ouverture de Session"					
			Case 2564 
			 Etat = "Temporaire"				
			Case Else 
			 Etat ="Inconnu"
 
		End Select
 
		ProfilState =  Etat
 
Set WshShell = Nothing 
 
End Function
 
 
' Function IsServer
 
Function IsServer()
 
Dim ColSys,OSVersion
Dim result
 
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
 
Set objWMIService = GetObject("winmgmts:\\" & strcomputer & "\root\CIMV2") 
Set ColSys = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
 
For Each OS In ColSys
 
	OSVersion = Left(OS.Version,3)
 
			If InStr(OS.Caption, "Server") Or InStr(OS.Caption, "Serveur") Then
 
				result = True
 
			Else
 
				result = False
 
			End If
 
Next 
 
IsServer = result
 
 
End Function
 
 
' Proc�dure getRSOP
 
 
Sub getRSOP()
 
Dim oReg,strKeyPath
Const HKEY_LOCAL_MACHINE = &H80000002
 
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\" & userSid & "\GPO-List"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
 
For Each subkey In arrSubKeys
 
    strData = WshShell.RegRead("HKLM" & "\" & strKeyPath & "\" & subkey & "\DisplayName")
    objTextFile.WriteLine "GPO " & subkey & " :"  & strData
 
Next
 
Set oReg = Nothing
 
End Sub
 
'Proc�dure GetHotFix
 
Sub GetHotFix ()
 
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colQuickFixes = objWMIService.ExecQuery _
    ("Select * from Win32_QuickFixEngineering")
For Each objQuickFix in colQuickFixes
    objTextFile.WriteLine "KB ID: " & objQuickFix.HotFixID
Next
 
Set objWMIService = Nothing
Set colQuickFixes = Nothing
 
Set objSession = CreateObject("Microsoft.Update.Session")
Set objSearcher = objSession.CreateUpdateSearcher
intHistoryCount = objSearcher.GetTotalHistoryCount
 
'// Get WU history data:
Set colHistory = objSearcher.QueryHistory(1, intHistoryCount)
 
 
'// Loop through and print to screen:
For Each objEntry in colHistory
    objTextFile.WriteLine objEntry.Title
    objTextFile.WriteLine "Installed On: " & objEntry.Date
	objTextFile.WriteBlankLines (2)
Next
 
End sub
 
 
 
' Function HWinfo
 
Function HWinfo ()
 
Dim WshShell,oWMI,colParams,colTag,colMem
Dim sCPU,Reg_CPU,sMarq,sModel,sTag,sMem
 
Reg_CPU = "HKLM\Hardware\Description\System\CentralProcessor\0\" & "ProcessorNameString"
 
Set WshShell = CreateObject("WScript.Shell")
sCPU = WshShell.RegRead (Reg_CPU)
 
Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\.\root\cimv2")
 
Set colParams = oWMI.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objParam in colParams 
	sModel = objParam.Model
    sMarq = objParam.Manufacturer
    sMem = objParam.TotalPhysicalMemory/(1024*1024)
Next
 
Set colTag = oWMI.ExecQuery( _
    "SELECT * FROM Win32_ComputerSystemProduct") 
For Each objTag in colTag 
    sTag = objTag.IdentifyingNumber
Next
 
 
Set oWMI = Nothing
Set WshShell = Nothing
 
HWinfo = Array (sCPU,sMarq,sModel,sTag,sMem)
 
End Function
 
 
'Function setEnv() 
 
Function SetENV ()
 
Dim WshShell
Dim UserPath,strUsername,sDomain,sComputer,sServer
 
Set WshShell = CreateObject("WScript.Shell")
Profil = WshShell.ExpandEnvironmentStrings("%userprofile%")
UserPath = WshShell.ExpandEnvironmentStrings("%userprofile%") & "\session_log"
sComputer = WshShell.ExpandEnvironmentStrings("%computername%")
strUsername = WshShell.ExpandEnvironmentStrings("%username%")
sServer = WshShell.ExpandEnvironmentStrings("%logonserver%")
sDomain = WshShell.ExpandEnvironmentStrings("%USERDNSDOMAIN%")
 
Set WshShell = Nothing 
 
SetENV = Array (UserPath,sComputer,strUsername,sServer,sDomain,Profil)
 
End Function
 
 
 
'Proc�dure vidage dossier de log
 
Sub cleanLog(LocalPath,NetworkPath)
 
Dim FSO,objLocalFolder,objFile,objSubfolder,objNetFolder
Dim DayToKeep
 
Set FSO = CreateObject("scripting.filesystemobject")
 
DayToKeep = 60 
Set objLocalFolder = FSO.GetFolder(LocalPath)
 
 
For Each objFile In objLocalFolder.files
    If DateDiff("d", objFile.DateCreated,Now) > DayToKeep Then
        objFile.Delete True
    End If
Next
 
 
For Each objSubfolder In objLocalFolder.Subfolders
    If DateDiff("d", objSubfolder.DateCreated,Now) > DayToKeep Then
            objSubfolder.Delete True     
    End If
Next
 
Set objNetFolder = FSO.GetFolder(NetworkPath)
 
For Each objFile In objNetFolder.files
    If DateDiff("d", objFile.DateCreated,Now) > DayToKeep Then
        objFile.Delete True
    End If
Next
 
 
For Each objSubfolder In objNetFolder.Subfolders
    If DateDiff("d", objSubfolder.DateCreated,Now) > DayToKeep Then
            objSubfolder.Delete True     
    End If
Next
 
End sub

Voici l'erreur que je rencontre. C'est à l'utilisation de la variable IpForti qui contient le retour de la fonction FortiIP().
Nom : vbscriptJPG.JPG
Affichages : 147
Taille : 190,3 Ko



Merci de votre aide