Skip to content

Instantly share code, notes, and snippets.

@jpogran
Created May 24, 2019 14:34
Show Gist options
  • Save jpogran/a9e97d4662da0f782c2649a6e59360df to your computer and use it in GitHub Desktop.
Save jpogran/a9e97d4662da0f782c2649a6e59360df to your computer and use it in GitHub Desktop.
Puppet Disable LCM
define dsc::lcm_config (
$refresh_mode = 'Disabled'
) {
validate_re($refresh_mode, '^(Disabled|Push)$', 'refresh_mode must be one of \'Disabled\', \'Push\'')
exec { "dsc_provider_set_lcm_refreshmode_${refresh_mode}":
provider => 'powershell',
onlyif => "if ((Get-DscLocalConfigurationManager).RefreshMode -eq '${refresh_mode}') {exit 1} else {exit 0}",
command => template('dsc/set-lcm-mode.ps1.erb'),
}
}
$lcmRefreshMode = '<%= @refresh_mode %>'
$generationDate = (Get-Date -Format 'M/d/yyyy HH:mm:ss')
$encoding = [Text.Encoding]::UTF8
$namespace = 'root/Microsoft/Windows/DesiredStateConfiguration'
$className = 'MSFT_DSCLocalConfigurationManager'
$generationHost = [Environment]::MachineName
$mofData = @"
/*
@TargetNode=''localhost''
@GeneratedBy=PUPPET
@GenerationDate=$generationDate
@GenerationHost=$generationHost
*/
instance of MSFT_DSCMetaConfiguration as `$MSFT_DSCMetaConfiguration1ref
{
RefreshMode = "$lcmRefreshMode";
};
instance of OMI_ConfigurationDocument
{
Version="2.0.0";
MinimumCompatibleVersion = "1.0.0";
CompatibleVersionAdditionalProperties= { "MSFT_DSCMetaConfiguration:StatusRetentionTimeInDays" };
Author="PUPPET";
GenerationDate="$generationDate";
GenerationHost="$generationHost";
Name="LCMSettings";
};
"@
$totalSize = [BitConverter]::GetBytes($mofData.Length + 4)
$bytes = $totalSize + $encoding.GetBytes($mofData)
$cimClass = Get-CimClass -Namespace $namespace -ClassName $className
$sessionOptions = New-CimSessionOption -Protocol Wsman
$wsmanSession = New-CimSession -ComputerName localhost -SessionOption $sessionOptions
$invokeParams = @{
Verbose = $true
CimClass = $cimClass
CimSession = $wsmanSession
MethodName = 'SendMetaConfigurationApply'
Arguments = @{
ConfigurationData = $bytes;
}
}
Invoke-CimMethod @invokeParams
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment