'********************************************************* ' En-tête '********************************************************* ' °Author : BLUTEAU Erwan et christian grandjean ' °Date : 28.01.2011 ' °Version : 3.0 ' ' °Script description : ' Add entries in local GPO for "Machine => Startup/Shutdown" or "User ' => Logon/Logoff" entries. ' ' °Script parameters : ' sScript = Script or application to run ' sParameters = command line parameters ' sAddInStartupOrLogin ' True = Startup / Logon ' False = Shutdown / Logoff ' sMACHINEorUSER ' MACHINE = Machine Policy ' USER = User Policy ' ' °Script workflow : ' 1. Set ini file to normal mode ' 2. Edit ini file ' 3. Create/Add changes ' 4. Delete GPO registry key ' 5. Set ini file to hidden mode ' 6. Run GPUPDATE to integrate entries in registry ' ' °Remarks : ' The script accept maximum 9 entries in ini file ' '******************************************************** ' Fin en-tête '********************************************************* Const ForReading =1 'Constante pour l ouverture en lecture d un fichier Const ForWriting = 2 const ForAppending = 8 Const DeleteReadOnly = True '********************************************************* 'recupération des paramétres '********************************************************* Set objArgs = Wscript.Arguments ' Objet pour récupérer les arguments 'Param 1 TargetRunningScript=Wscript.Arguments(0) 'Param 2 if Wscript.Arguments.count <= 3 then TargetRunningScriptParam="" else TargetRunningScriptParam = Wscript.Arguments(1) if Wscript.Arguments.count >= 5 then for i = 2 to Wscript.Arguments.count-3 TargetRunningScriptParam = TargetRunningScriptParam & " " & Wscript.Arguments(i) next end if end if 'Param 3 TargetTime=Wscript.Arguments(Wscript.Arguments.count-2) 'Param 4 TargetGPO=Wscript.Arguments(Wscript.Arguments.count-1) 'wscript.echo "param1=" & TargetRunningScript 'wscript.echo "param2=" & TargetRunningScriptParam 'wscript.echo "param3=" & TargetTime 'wscript.echo "param4=" & TargetGPO 'LANCEMENT SCRIPT WriteGPOScriptPolicyGPTINIFILE TargetTime,TargetGPO WriteGPOScriptPolicy TargetRunningScript,TargetRunningScriptParam,TargetTime,TargetGPO '********************************************************* ' FIN '********************************************************* 'WScript.Sleep 10000 WScript.Quit '********************************************************* ' LES FONCTIONS '********************************************************* Sub WriteGPOScriptPolicyGPTINIFILE(ByVal sAddInStartupOrLogin, ByVal sMACHINEorUSER) Dim objFSO,ooShell Dim sSys32 Dim FicGptIni, PathGptIni, TmpVar, NbrLigne, i,ext, version Dim TABGptIni() Set ooShell = CreateObject("WSCript.Shell") 'Pour RUN de commandes Set objFSO = CreateObject("Scripting.FileSystemObject") ' Set sSys32 = objFSO.GetSpecialFolder(1) PathGptIni = sSys32 & "\GroupPolicy\gpt.ini" PathGptIniTMP = PathGptIni & ".tmp" NbrLigne = 0 IF not objFSO.FileExists(PathGptIni) Then Set FicGptIni = objFSO.OpenTextFile(PathGptIni, ForWriting,true) FicGptIni.write("[General]") FicGptIni.close end if Set FicGptIni = objFSO.OpenTextFile(PathGptIni, ForReading) Do Until FicGptIni.AtEndOfStream NbrLigne = NbrLigne + 1 TmpVar = FicGptIni.ReadLine() ReDim preserve TABGptIni(NbrLigne) TABGptIni(NbrLigne)=TmpVar 'wscript.echo TABGptIni(NbrLigne) Loop 'wscript.echo "***********************" 'wscript.echo "***********************" if sMACHINEorUSER = "MACHINE" then gPCMachineLine = 0 generalLine = 0 For i = 0 to NbrLigne if instr(1,TABGptIni(i),"gPCMachineExtensionNames", 1) then gPCMachineLine = 1 if instr(TABGptIni(i),"[{42B5FAAE-6536-11D2-AE5A-0000F87571E3}{40B6664F-4972-11D1-A7CA-0000F87571E3}]") then 'wscript.echo "deja présent" else ext = right(TABGptIni(i),len(TABGptIni(i))-instr(TABGptIni(i),"=")) TABGptIni(i) = "gPCMachineExtensionNames=" & "[{42B5FAAE-6536-11D2-AE5A-0000F87571E3}{40B6664F-4972-11D1-A7CA-0000F87571E3}]" & ext end if end if if instr(1,TABGptIni(i),"Version=",1) AND (instr(1,TABGptIni(i),"gPCFunctionalityVersion",1)=0) then version = right(TABGptIni(i),len(TABGptIni(i))-instr(TABGptIni(i),"=")) version = version + 65536 TABGptIni(i) = "Version=" & version end if if instr(1,TABGptIni(i),"[General]", 1) then generalLine = 1 end if if i = NbrLigne AND generalLine = 0 then TABGptIni(i) = TABGptIni(i) & vbNewLine & "[General]" end if if i = NbrLigne AND gPCMachineLine = 0 then TABGptIni(i) = TABGptIni(i) & vbNewLine & "gPCMachineExtensionNames=" & "[{42B5FAAE-6536-11D2-AE5A-0000F87571E3}{40B6664F-4972-11D1-A7CA-0000F87571E3}]" & vbNewLine & "Version=1" end if Next end if if sMACHINEorUSER = "USER" then gPCUserLine = 0 For i = 0 to NbrLigne if instr(TABGptIni(i),"gPCUserExtensionNames") then if instr(TABGptIni(i),"[{42B5FAAE-6536-11D2-AE5A-0000F87571E3}{40B66650-4972-11D1-A7CA-0000F87571E3}]") then 'wscript.echo "deja présent" else ext = right(TABGptIni(i),len(TABGptIni(i))-instr(TABGptIni(i),"=")) TABGptIni(i) = "gPCUserExtensionNames=" & "[{42B5FAAE-6536-11D2-AE5A-0000F87571E3}{40B66650-4972-11D1-A7CA-0000F87571E3}]" & ext end if end if if instr(TABGptIni(i),"Version=") AND (instr(TABGptIni(i),"gPCFunctionalityVersion")=0) then version = right(TABGptIni(i),len(TABGptIni(i))-instr(TABGptIni(i),"=")) version = version + 1 TABGptIni(i) = "Version=" & version end if if i = NbrLigne AND gPCUserLine = 0 then TABGptIni(i) = TABGptIni(i) & vbNewLine & "gPCUserExtensionNames=" & "[{42B5FAAE-6536-11D2-AE5A-0000F87571E3}{40B6664F-4972-11D1-A7CA-0000F87571E3}]" & vbNewLine & "Version=1" end if Next end if on error goto 0 If objFSO.FileExists(PathGptIniTMP) Then objFSO.DeleteFile(PathGptIniTMP) End If Set FicGptIniTEMP = objFSO.OpenTextFile(PathGptIniTMP, ForWriting,true) for k = 0 to NbrLigne 'wscript.echo TABGptIni(k) FicGptIniTEMP.write(TABGptIni(k) & vbNewLine) next FicGptIniTEMP.close objFSO.CopyFile PathGptIniTMP, PathGptIni, 1 If objFSO.FileExists(PathGptIniTMP) Then objFSO.DeleteFile(PathGptIniTMP) End If End Sub '********************************************************* Sub WriteGPOScriptPolicy(ByVal sScript, ByVal sParameters, ByVal sAddInStartupOrLogin, ByVal sMACHINEorUSER) Dim sScriptPath, sSys32, sTMP Dim oINI, ooShell, ooFSO, oINIProperties, oRegFile Dim iLastStartup, iLastShutdown Dim sOption1, sOption2, sStartup, sShutdown sStartup = "" sShutdown = "" sMACHINEorUSER = Ucase(sMACHINEorUSER) Set ooShell = CreateObject("WSCript.Shell") Set ooFSO = CreateObject("Scripting.FileSystemObject") Set sSys32 = ooFSO.GetSpecialFolder(1) ' Check if COMPUTER or USER Policy If sMACHINEorUSER = "MACHINE" Then sScriptPath = sSys32 & "\GroupPolicy\Machine\Scripts\scripts.ini" Elseif sMACHINEorUSER = "USER" Then sScriptPath = sSys32 & "\GroupPolicy\User\Scripts\scripts.ini" Else WScript.Echo "Please set value for sMACHINEorUSER !" WScript.Quit End If If (Not ooFSO.FileExists(sScriptPath)) Then ' Add new value in startup or shutdown If sAddInStartupOrLogin Then iLastStartup = 0 If sMACHINEorUSER = "MACHINE" Then sStartup = VbCrLf & "[Startup]" If sMACHINEorUSER = "USER" Then sStartup = VbCrLf & "[Logon]" sStartup = sStartup & VbCrLf & iLastStartup & "CmdLine=" & sScript sStartup = sStartup & VbCrLf & iLastStartup & "Parameters=" & sParameters Else iLastShutdown = 0 If sMACHINEorUSER = "MACHINE" Then sStartup = VbCrLf & "[Shutdown]" If sMACHINEorUSER = "USER" Then sStartup = VbCrLf & "[Logoff]" sShutdown = sShutdown & VbCrLf &iLastShutdown & "CmdLine=" & sScript sShutdown = sShutdown & VbCrLf &iLastShutdown & "Parameters=" & sParameters End If Else ' Set file as normal Set oINIProperties = ooFSO.GetFile(sScriptPath) oINIProperties.Attributes = 0 'The File become Hidden! ' read Set oINI = ooFSO.OpenTextFile(sScriptPath, 1) iLastStartup = 0 iLastShutdown = 0 Do While 1 = 1 On Error Resume Next sTMP = oINI.ReadLine 'If sTMP = VbCrLf Then sTMP = "" If Err > 0 Then Exit Do On Error GoTo 0 If InStr(1, UCase(sTMP), UCase("[Startup]")) > 0 And sMACHINEorUSER = "MACHINE" Then sOption1 = sTMP If InStr(1, UCase(sTMP), UCase("[Shutdown]")) > 0 And sMACHINEorUSER = "MACHINE" Then sOption2 = sTMP If InStr(1, UCase(sTMP), UCase("[Logon]")) > 0 And sMACHINEorUSER = "USER" Then sOption1 = sTMP If InStr(1, UCase(sTMP), UCase("[Logoff]")) > 0 And sMACHINEorUSER = "USER" Then sOption2 = sTMP If sOption1 = "[Startup]" And sOption2 <> "[Shutdown]" Or sOption1 = "[Logon]" And sOption2 <> "[Logoff]" Then sStartup = sStartup & VbCrLf & sTMP On Error Resume Next 'WScript.Echo CInt(Left(sTMP, 1)) iLastStartup = Cint(Left(sTMP, 1)) + 1 On Error GoTo 0 'WScript.Echo "Start Next value = " & iLastStartup Else sShutdown = sShutdown & VbCrLf & sTMP On Error Resume Next 'WScript.Echo CInt(Left(sTMP, 1)) iLastShutdown = CInt(Left(sTMP, 1)) + 1 On Error GoTo 0 'WScript.Echo "Shutdown Next value = " & iLastShutdown End If Loop ' Add new value in startup or shutdown If sAddInStartupOrLogin Then ' If key dont exist If InStr(1, sStartup, "[Startup]") = 0 And sMACHINEorUSER = "MACHINE" Then sStartup = VbCrLf & "[Startup]" If InStr(1, sStartup, "[Logon]") = 0 And sMACHINEorUSER = "USER" Then sStartup = VbCrLf & "[Logon]" ' Don't add the script agin if it's already there. Not very foolproof but.... If InStr(1, sStartup, "CmdLine=" & sScript, 1) = 0 Then sStartup = sStartup & VbCrLf &iLastStartup & "CmdLine=" & sScript sStartup = sStartup & VbCrLf & iLastStartup & "Parameters=" & sParameters End If Else ' If key dont exist If InStr(1, sShutdown, "[Shutdown]") = 0 And sMACHINEorUSER = "MACHINE" Then sShutdown = VbCrLf & "[Shutdown]" If InStr(1, sShutdown, "[Logoff]") = 0 And sMACHINEorUSER = "USER" Then sShutdown = VbCrLf & "[Logoff]" If InStr(1, sShutdown, "CmdLine=" & sScript, 1) = 0 Then sShutdown = sShutdown & VbCrLf & iLastShutdown & "CmdLine=" & sScript sShutdown = sShutdown & VbCrLf & iLastShutdown & "Parameters=" & sParameters End If End If ' Close file oINI.Close End If On Error GoTo 0 ' prin result 'WScript.Echo sStartup & sShutdown ' Write result in ini file Set oINI = ooFSO.CreateTextFile(sScriptPath, True) oINI.WriteLine sStartup & sShutdown oINI.Close ' Set file as hidden Set oINIProperties = ooFSO.GetFile(sScriptPath) oINIProperties.Attributes = 2 'The File become Hidden! ' Remove registry key 'Set oRegFile = ooFSO.CreateTextFile(CurDir & "\~TMP-REG-WriteGPOScriptPolicy-Script.reg", True) 'oRegFile.WriteLine "Windows Registry Editor Version 5.00" 'oRegFile.WriteLine "[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts]" 'oRegFile.Close 'oShell.Run "REGEDIT /S " & CurDir & "\~TMP-REG-WriteGPOScriptPolicy-Script.reg" 'oFSO.DeleteFile CurDir & "\~TMP-REG-WriteGPOScriptPolicy-Script.reg", True ' Check if COMPUTER or USER Policy If sMACHINEorUSER = "MACHINE" Then ' Reload configuration from ini file ooShell.Run "GPUPDATE /Target:Computer /Wait:30", 0, True 'if err.number <> 0 then ' wscript.echo "ERREUR : " & err.Number & " : " & err.Description ' wscript.sleep 10000 'end if Elseif sMACHINEorUSER = "USER" Then ' Reload configuration from ini file ooShell.Run "GPUPDATE /Target:User /Force /Wait:30", 0, True Else WScript.Echo "Please set value for sMACHINEorUSER !" WScript.Quit End If End Sub