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
| '*********************************************************
' 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 objFSO.FileExists(PathGptIni) Then
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
For i = 0 to NbrLigne
if instr(TABGptIni(i),"gPCMachineExtensionNames") then
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(TABGptIni(i),"Version=") AND (instr(TABGptIni(i),"gPCFunctionalityVersion")=0) then
version = right(TABGptIni(i),len(TABGptIni(i))-instr(TABGptIni(i),"="))
version = version + 65536
TABGptIni(i) = "Version=" & version
end if
Next
end if
if sMACHINEorUSER = "USER" then
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
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 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]"
sStartup = sStartup & VbCrLf &iLastStartup & "CmdLine=" & sScript
sStartup = sStartup & VbCrLf & iLastStartup & "Parameters=" & sParameters
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]"
sShutdown = sShutdown & VbCrLf & iLastShutdown & "CmdLine=" & sScript
sShutdown = sShutdown & VbCrLf & iLastShutdown & "Parameters=" & sParameters
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 /Force /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 |