Modules/DscOperations/DscOperations.psm1
#Region './prefix.ps1' 0 Set-StrictMode -Version latest $ErrorActionPreference = 'Stop' Import-Module $PSScriptRoot/../GuestConfigPath -Force $script:ExecuteDscOperationsScript = @" using System; using System.Runtime.InteropServices; using System.Management.Automation; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; namespace GuestConfig {{ public class DscOperations {{ [DllImport("{0}", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention = CallingConvention.StdCall)] public static extern IntPtr new_dsc_library_context(string assignment_name, string dsc_binary_path, IntPtr writeMessageCallback, IntPtr writeErrorCallback, IntPtr writeResultCallback); [DllImport("{0}", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention = CallingConvention.StdCall)] public static extern Int32 test_dsc_configuration(IntPtr context, string job_id, string assignment_name, string file_path); [DllImport("{0}", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention = CallingConvention.StdCall)] public static extern void start_dsc_configuration(IntPtr context, string job_id, string assignment_name, string file_path, bool p_use_existing, bool p_force); [DllImport("{0}", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention = CallingConvention.StdCall)] public static extern Int32 get_dsc_configuration(IntPtr context, string job_id, string assignment_name, string file_path); [DllImport("{0}", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention = CallingConvention.StdCall)] public static extern Int32 publish_dsc_assignment(IntPtr context, string job_id, string assignment_name, string assignments_path); [DllImport("{0}", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention = CallingConvention.StdCall)] public static extern Int32 set_dsc_meta_configuration(IntPtr context, string job_id, string assignment_name, string assignments_path); [DllImport("{0}", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention = CallingConvention.StdCall)] public static extern void delete_dsc_library_context(IntPtr context); internal enum MessageChannel {{ Warning, Verbose, Debug, Error }} public DscOperations() {{ m_messages = new List<Tuple<MessageChannel, string>>(); m_result = ""; WriteMessageDelegate delegate_write_message = new WriteMessageDelegate(WriteMessage); GCHandle m_write_message_gc_handle = GCHandle.Alloc(delegate_write_message); m_write_message_callback = Marshal.GetFunctionPointerForDelegate(delegate_write_message); WriteErrorDelegate delegate_write_error = new WriteErrorDelegate(WriteError); m_write_error_gc_handle = GCHandle.Alloc(delegate_write_error); m_write_error_callback = Marshal.GetFunctionPointerForDelegate(delegate_write_error); WriteResultDelegate delegate_write_result = new WriteResultDelegate(WriteResult); m_write_result_gc_handle = GCHandle.Alloc(delegate_write_result); m_write_result_callback = Marshal.GetFunctionPointerForDelegate(delegate_write_result); }} ~DscOperations() {{ if (m_write_message_gc_handle.IsAllocated) {{ m_write_message_gc_handle.Free(); }} if (m_write_error_gc_handle.IsAllocated) {{ m_write_error_gc_handle.Free(); }} if (m_write_result_gc_handle.IsAllocated) {{ m_write_result_gc_handle.Free(); }} }} public string TestDscConfiguration(PSCmdlet ps_cmdlet, string job_id, string configuration_name, string gc_bin_path) {{ IntPtr context = IntPtr.Zero; try {{ ClearMessages(); context = new_dsc_library_context(configuration_name, gc_bin_path, m_write_message_callback, m_write_error_callback, m_write_result_callback); if(context == IntPtr.Zero) {{ ps_cmdlet.WriteError(CreateErrorRecord("TestGuestConfiguration", "Failed to initialize Guest Configuration library.", true)); }} Int32 result = test_dsc_configuration(context, job_id, configuration_name, ""); for (int i = 0; i < m_messages.Count; i++) {{ var message = m_messages[i]; if(message.Item1 == MessageChannel.Error) {{ ps_cmdlet.WriteError(CreateErrorRecord("TestGuestConfiguration", message.Item2, false)); }} else if(message.Item1 == MessageChannel.Warning) {{ ps_cmdlet.WriteWarning(message.Item2); }} else if(message.Item1 == MessageChannel.Debug) {{ ps_cmdlet.WriteDebug(message.Item2); }} else {{ ps_cmdlet.WriteVerbose(message.Item2); }} }} }} finally {{ delete_dsc_library_context(context); }} return m_result; }} public void StartDscConfiguration(PSCmdlet ps_cmdlet, string job_id, string configuration_name, string gc_bin_path, bool p_use_existing, bool p_force) {{ IntPtr context = IntPtr.Zero; try {{ ClearMessages(); context = new_dsc_library_context(configuration_name, gc_bin_path, m_write_message_callback, m_write_error_callback, m_write_result_callback); if(context == IntPtr.Zero) {{ ps_cmdlet.WriteError(CreateErrorRecord("StartGuestConfiguration", "Failed to initialize Guest Configuration library.", true)); }} start_dsc_configuration(context, job_id, configuration_name, "", p_use_existing, p_force); for (int i = 0; i < m_messages.Count; i++) {{ var message = m_messages[i]; if(message.Item1 == MessageChannel.Error) {{ ps_cmdlet.WriteError(CreateErrorRecord("StartGuestConfiguration", message.Item2, false)); }} else if(message.Item1 == MessageChannel.Warning) {{ ps_cmdlet.WriteWarning(message.Item2); }} else if(message.Item1 == MessageChannel.Debug) {{ ps_cmdlet.WriteDebug(message.Item2); }} else {{ ps_cmdlet.WriteVerbose(message.Item2); }} }} }} finally {{ delete_dsc_library_context(context); }} }} public string GettDscConfiguration(PSCmdlet ps_cmdlet, string job_id, string configuration_name, string gc_bin_path) {{ IntPtr context = IntPtr.Zero; try {{ ClearMessages(); context = new_dsc_library_context(configuration_name, gc_bin_path, m_write_message_callback, m_write_error_callback, m_write_result_callback); if(context == IntPtr.Zero) {{ ps_cmdlet.WriteError(CreateErrorRecord("TestGuestConfiguration", "Failed to initialize Guest Configuration library.", true)); }} Int32 result = get_dsc_configuration(context, job_id, configuration_name, ""); for (int i = 0; i < m_messages.Count; i++) {{ var message = m_messages[i]; if(message.Item1 == MessageChannel.Error) {{ ps_cmdlet.WriteError(new ErrorRecord( new InvalidOperationException(message.Item2), "TestGuestConfiguration", ErrorCategory.InvalidResult, null)); }} else if(message.Item1 == MessageChannel.Warning) {{ ps_cmdlet.WriteWarning(message.Item2); }} else {{ ps_cmdlet.WriteVerbose(message.Item2); }} }} }} finally {{ delete_dsc_library_context(context); }} return m_result; }} public void PublishDscConfiguration(PSCmdlet ps_cmdlet, string job_id, string configuration_name, string gc_bin_path, string policy_path) {{ IntPtr context = IntPtr.Zero; try {{ ClearMessages(); context = new_dsc_library_context(configuration_name, gc_bin_path, m_write_message_callback, m_write_error_callback, m_write_result_callback); if(context == IntPtr.Zero) {{ ps_cmdlet.WriteError(CreateErrorRecord("TestGuestConfiguration", "Failed to initialize Guest Configuration library.", true)); }} Int32 result = publish_dsc_assignment(context, job_id, configuration_name, policy_path); if(result != 0) {{ ps_cmdlet.WriteError(CreateErrorRecord("TestGuestConfiguration", "Failed to publish Guest Configuration policy package.", true)); }} }} finally {{ delete_dsc_library_context(context); }} }} public void SetDscLocalConfigurationManager(PSCmdlet ps_cmdlet, string job_id, string configuration_name, string gc_bin_path, string policy_path) {{ IntPtr context = IntPtr.Zero; try {{ ClearMessages(); context = new_dsc_library_context(configuration_name, gc_bin_path, m_write_message_callback, m_write_error_callback, m_write_result_callback); if(context == IntPtr.Zero) {{ ps_cmdlet.WriteError(CreateErrorRecord("TestGuestConfiguration", "Failed to initialize Guest Configuration library.", true)); }} Int32 result = set_dsc_meta_configuration(context, job_id, configuration_name, policy_path); if(result != 0) {{ ps_cmdlet.WriteError(CreateErrorRecord("TestGuestConfiguration", "Failed to set Meta config settings.", true)); }} }} finally {{ delete_dsc_library_context(context); }} }} private delegate Int32 WriteMessageDelegate(Int32 channel, IntPtr message); private delegate Int32 WriteErrorDelegate(IntPtr error); private delegate Int32 WriteResultDelegate(IntPtr result); private string m_result; private List<Tuple<MessageChannel, string>> m_messages; private GCHandle m_write_message_gc_handle; private GCHandle m_write_error_gc_handle; private GCHandle m_write_result_gc_handle; private IntPtr m_write_message_callback; private IntPtr m_write_error_callback; private IntPtr m_write_result_callback; internal Int32 WriteMessage(Int32 channel, IntPtr message_ptr) {{ string message; message = Marshal.PtrToStringAnsi(message_ptr); m_messages.Add(Tuple.Create((MessageChannel)channel, message)); return 0; }} internal Int32 WriteError(IntPtr error_ptr) {{ string error; error = Marshal.PtrToStringAnsi(error_ptr); m_messages.Add(Tuple.Create(MessageChannel.Error, error)); return 0; }} internal Int32 WriteResult(IntPtr result_ptr) {{ m_result = Marshal.PtrToStringAnsi(result_ptr); return 0; }} private void ClearMessages() {{ m_messages.Clear(); }} private ErrorRecord CreateErrorRecord(string error_id, string error_message, bool include_error_from_message_list) {{ string error = error_message + "\r\n"; for (int i = 0; include_error_from_message_list && i < m_messages.Count; i++) {{ var message = m_messages[i]; if(message.Item1 == MessageChannel.Error) {{ error = message.Item2 + "\r\n"; }} }} return new ErrorRecord( new InvalidOperationException(error), error_id, ErrorCategory.InvalidResult, null); }} }} }} "@ #EndRegion './prefix.ps1' 324 #Region './Public/Get-DscConfiguration.ps1' 0 <# .SYNOPSIS Get DSC configuration. .Parameter ConfigurationName Configuration name. .Example Get-DscConfiguration -ConfigurationName WindowsTLS #> function Get-DscConfiguration { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $ConfigurationName ) $job_id = [guid]::NewGuid().Guid $gcBinPath = Get-GuestConfigBinaryPath $dsclibPath = $(Get-DscLibPath) -replace '[""\\]','\$&' if (-not ([System.Management.Automation.PSTypeName]'GuestConfig.DscOperations').Type) { $addTypeScript = $ExecuteDscOperationsScript -f $dsclibPath Add-Type -TypeDefinition $addTypeScript -ReferencedAssemblies 'System.Management.Automation','System.Console','System.Collections' } $dscOperation = [GuestConfig.DscOperations]::New() $result = $dscOperation.GettDscConfiguration($PSCmdlet, $job_id, $ConfigurationName, $gcBinPath) return ConvertFrom-Json $result } #EndRegion './Public/Get-DscConfiguration.ps1' 38 #Region './Public/Publish-DscConfiguration.ps1' 0 <# .SYNOPSIS Publish DSC configuration. .Parameter ConfigurationName Configuration name. .Parameter Path Policy Path. .Example Publish-DscConfiguration -Path C:\metaconfig #> function Publish-DscConfiguration { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $ConfigurationName, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $Path ) $job_id = [guid]::NewGuid().Guid $gcBinPath = Get-GuestConfigBinaryPath $dsclibPath = (Get-DscLibPath) -replace '[""\\]','\$&' if (-not (Test-Path -Path $gcBinPath)) { throw "Guest Config binaries not found at path $gcBinPath" } if (-not (Test-Path -Path $dsclibPath)) { throw "Guest Config DSC Lib not found at path $dsclibPath" } if (-not (Test-Path -Path $Path)) { throw "Guest Config DSC Config not found at path $testPath" } if (-not ([System.Management.Automation.PSTypeName]'GuestConfig.DscOperations').Type) { $addTypeScript = $ExecuteDscOperationsScript -f $dsclibPath Add-Type -TypeDefinition $addTypeScript -ReferencedAssemblies 'System.Management.Automation','System.Console','System.Collections' } $dscOperation = [GuestConfig.DscOperations]::New() Write-Debug -Message "Running [GuestConfig.DscOperations] `$dscOperation.PublishDscConfiguration method with: $PSCmdlet, $job_id, $ConfigurationName, $gcBinPath, $Path." $null = $dscOperation.PublishDscConfiguration($PSCmdlet, $job_id, $ConfigurationName, $gcBinPath, $Path) } #EndRegion './Public/Publish-DscConfiguration.ps1' 61 #Region './Public/Set-DscLocalConfigurationManager.ps1' 0 <# .SYNOPSIS Set DSC LCM settings. .Parameter ConfigurationName Configuration name. .Example Set-DscLocalConfigurationManager -Path C:\metaconfig #> function Set-DscLocalConfigurationManager { [CmdletBinding()] param ( [Parameter(Position=0, Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $ConfigurationName, [Parameter(Position=1, Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $Path ) $job_id = [guid]::NewGuid().Guid $gcBinPath = Get-GuestConfigBinaryPath $dsclibPath = $(Get-DscLibPath) -replace '[""\\]','\$&' if (-not ([System.Management.Automation.PSTypeName]'GuestConfig.DscOperations').Type) { $addTypeScript = $ExecuteDscOperationsScript -f $dsclibPath Add-Type -TypeDefinition $addTypeScript -ReferencedAssemblies 'System.Management.Automation','System.Console','System.Collections' } $dscOperation = [GuestConfig.DscOperations]::New() $result = $dscOperation.SetDscLocalConfigurationManager($PSCmdlet, $job_id, $ConfigurationName, $gcBinPath, $Path) } #EndRegion './Public/Set-DscLocalConfigurationManager.ps1' 41 #Region './Public/Start-DscConfiguration.ps1' 0 <# .SYNOPSIS Start DSC configuration. .Parameter ConfigurationName Configuration name. .Example Start-DscConfiguration -ConfigurationName WindowsTLS #> function Start-DscConfiguration { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $ConfigurationName ) $job_id = [guid]::NewGuid().Guid $gcBinPath = Get-GuestConfigBinaryPath $dsclibPath = $(Get-DscLibPath) -replace '[""\\]','\$&' if (-not ([System.Management.Automation.PSTypeName]'GuestConfig.DscOperations').Type) { $addTypeScript = $ExecuteDscOperationsScript -f $dsclibPath Add-Type -TypeDefinition $addTypeScript -ReferencedAssemblies 'System.Management.Automation','System.Console','System.Collections' } $dscOperation = [GuestConfig.DscOperations]::New() $dscOperation.StartDscConfiguration($PSCmdlet, $job_id, $ConfigurationName, $gcBinPath, $True, $True) } #EndRegion './Public/Start-DscConfiguration.ps1' 34 #Region './Public/Test-DscConfiguration.ps1' 0 <# .SYNOPSIS Test DSC configuration. .Parameter ConfigurationName Configuration name. .Example Test-DscConfiguration -ConfigurationName WindowsTLS #> function Test-DscConfiguration { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $ConfigurationName ) $job_id = [guid]::NewGuid().Guid $gcBinPath = Get-GuestConfigBinaryPath $dsclibPath = $(Get-DscLibPath) -replace '[""\\]','\$&' if (-not ([System.Management.Automation.PSTypeName]'GuestConfig.DscOperations').Type) { $addTypeScript = $ExecuteDscOperationsScript -f $dsclibPath Add-Type -TypeDefinition $addTypeScript -ReferencedAssemblies 'System.Management.Automation','System.Console','System.Collections' } $dscOperation = [GuestConfig.DscOperations]::New() $result = $dscOperation.TestDscConfiguration($PSCmdlet, $job_id, $ConfigurationName, $gcBinPath) return (ConvertFrom-Json -InputObject $result) } #EndRegion './Public/Test-DscConfiguration.ps1' 38 |