Skip to content

Instantly share code, notes, and snippets.

@glennsarti
Last active August 29, 2018 04:17
Show Gist options
  • Save glennsarti/b4744a57e11d3c2b44d85ffee9a15674 to your computer and use it in GitHub Desktop.
Save glennsarti/b4744a57e11d3c2b44d85ffee9a15674 to your computer and use it in GitHub Desktop.
Running Puppet Under a Specific CodePage

These files can be used to force Puppet.BAT to run under a specific code page (In this case 950)

  1. Grab all 3 files (puppet.vbs, install.vbs and RunPuppetAgent.xml)
  2. IMPORTANT - Make sure RunPuppetAgent.xml is saved with UTF-16LE encoding.
  3. Modify puppet.vbs to use the codepage you want (Currently 950)
  4. Run cscript.exe install.vbs
  • This will copy puppet.vbs into the C:\Program Files\Puppet Labs\Puppet\bin directory
  • Stop and Disable the Puppet Agent service
  • Create a Scheduled Task to run the Puppet Agent every 30mins (with a 30min skew) under SYSTEM account
Option Explicit
Dim fso, thisDir, installDir, objShell
Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
thisDir = fso.GetParentFolderName(WScript.ScriptFullName)
' Assume puppet is in "C:\Program Files\Puppet Labs\Puppet"
installDir = "C:\Program Files\Puppet Labs\Puppet"
Sub ExecCmd(ByVal command)
WScript.Echo "Running " + command
Dim objResult
Set objResult = objShell.Exec(command)
' Wait for process to exit and output the STDOUT and STDERR streams
Do While objResult.Status = 0
If Not objResult.StdOut.AtEndOfStream Then
WScript.Echo "STDOUT: " + objResult.StdOut.ReadAll
End If
If Not objResult.StdErr.AtEndOfStream Then
WScript.Echo "STDERR: " + objResult.StdErr.ReadAll
End If
WScript.Sleep 100
Loop
WScript.Echo "EXIT: " & objResult.ExitCode
End Sub
WScript.Echo "Copying puppet VBScript wrapper..."
fso.CopyFile thisDir + "\puppet.vbs", installDir + "\bin\puppet.vbs", true
WScript.Echo "Stopping the Puppet Agent ..."
ExecCmd "sc.exe stop puppet"
WScript.Echo "Disabling the Puppet Agent ..."
ExecCmd "sc.exe config puppet start= disabled"
WScript.Echo "Creating scheduled task to run Puppet Agent"
ExecCmd "schtasks.exe /create /TN RunPuppetAgent /XML """ + thisDir + "\RunPuppetAgent.xml"""
WScript.Echo "Installed"
Option Explicit
Dim fso, thisDir, objShell, puppetArgs, index
Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
thisDir = fso.GetParentFolderName(WScript.ScriptFullName)
' Grab the puppet arguments
puppetArgs = ""
For index = 0 to WScript.Arguments.count - 1
If index = 0 Then
puppetArgs = WScript.Arguments(0)
Else
puppetArgs = puppetArgs + " " + WScript.Arguments(index)
End If
Next
Dim objResult
' Assume that puppet.bat is in the same directory as this script
Set objResult = objShell.Exec("CMD /C chcp 950 & CALL """ + thisDir + "\puppet.bat"" " + puppetArgs)
' Wait for puppet to exit and output the STDOUT and STDERR streams
Do While objResult.Status = 0
If Not objResult.StdOut.AtEndOfStream Then
WScript.Echo "STDOUT: " + objResult.StdOut.ReadAll
End If
If Not objResult.StdErr.AtEndOfStream Then
WScript.Echo "STDERR: " + objResult.StdErr.ReadAll
End If
WScript.Sleep 100
Loop
' Pass through the exit code
WScript.Quit objResult.ExitCode
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2018-08-29T11:50:32</Date>
<Author>RANDOM\USER</Author>
<URI>\RunPuppetAgent</URI>
</RegistrationInfo>
<Triggers>
<TimeTrigger>
<Repetition>
<Interval>PT30M</Interval>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<StartBoundary>2018-08-29T11:50:00</StartBoundary>
<Enabled>true</Enabled>
<RandomDelay>PT30M</RandomDelay>
</TimeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\System32\cscript.exe</Command>
<Arguments>//B //NoLogo "C:\Program Files\Puppet Labs\Puppet\bin\puppet.vbs" agent --onetime</Arguments>
<WorkingDirectory>C:\Program Files\Puppet Labs\Puppet\bin</WorkingDirectory>
</Exec>
</Actions>
</Task>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment