diff --git a/src/Microsoft.PowerShell.Commands.Omi/DscConfiguration.cs b/src/Microsoft.PowerShell.Commands.Omi/DscConfiguration.cs index 549dc11ed0..cd2c6d5fc7 100644 --- a/src/Microsoft.PowerShell.Commands.Omi/DscConfiguration.cs +++ b/src/Microsoft.PowerShell.Commands.Omi/DscConfiguration.cs @@ -14,7 +14,7 @@ namespace Microsoft.PowerShell.Commands.Omi /// implementation for the Start-DscConfiguration command /// [Cmdlet( VerbsLifecycle.Start, "DscConfiguration" )] - [OutputType(typeof(string))] + [OutputType(typeof(object))] public sealed class StartDscConfigurationCommand : Cmdlet { #region parameters @@ -84,6 +84,45 @@ namespace Microsoft.PowerShell.Commands.Omi } #endregion + + #region Get-DscConfiguration + + /// + /// implementation for the Get-DscConfiguration command + /// + [Cmdlet( VerbsCommon.Get, "DscConfiguration" )] + [OutputType(typeof(object))] + public sealed class GetDscConfigurationCommand : Cmdlet + { + #region methods + + protected override void ProcessRecord() + { + if (!Platform.IsLinux()) + { + throw new PlatformNotSupportedException(); + } + + OmiInterface oi = new OmiInterface(); + + const string nameSpace = "root/Microsoft/DesiredStateConfiguration"; + const string instanceName = "{ MSFT_DSCLocalConfigurationManager }"; + const string methodName = "GetConfiguration"; + + string arguments = $"iv {nameSpace} {instanceName} {methodName} -xml"; + oi.ExecuteOmiCliCommand(arguments); + + OmiData data = oi.GetOmiData(); + object[] array = data.ToObjectArray(); + + WriteObject(array, true); + + } // EndProcessing + + #endregion + } + + #endregion } // namespace Microsoft.PowerShell.Commands diff --git a/src/Microsoft.PowerShell.Commands.Omi/DscLocalConfiguration.cs b/src/Microsoft.PowerShell.Commands.Omi/DscLocalConfiguration.cs new file mode 100644 index 0000000000..fa09bf5acd --- /dev/null +++ b/src/Microsoft.PowerShell.Commands.Omi/DscLocalConfiguration.cs @@ -0,0 +1,129 @@ +/********************************************************************++ +Copyright (c) Microsoft Corporation. All rights reserved. +--********************************************************************/ +using System; +using System.IO; +using System.Text; +using System.Management.Automation; + +namespace Microsoft.PowerShell.Commands.Omi +{ + #region Set-DscLocalConfiguration + + /// + /// implementation for the Set-DscLocalConfiguration command + /// + [Cmdlet(VerbsCommon.Set, "DscLocalConfiguration" )] + [OutputType(typeof(object))] + public sealed class SetDscLocalConfigurationCommand : Cmdlet + { + #region parameters + + [Parameter(Mandatory = true)] + [Alias("CM")] + public string ConfigurationMof + { + get + { + return mofPath; + } + set + { + mofPath = value; + } + } + private string mofPath; + + #endregion + + #region methods + + protected override void ProcessRecord() + { + if (mofPath == null) + { + throw new ArgumentNullException(); + } + + if (!Platform.IsLinux()) + { + throw new PlatformNotSupportedException(); + } + + OmiInterface oi = new OmiInterface(); + + const string nameSpace = "root/Microsoft/DesiredStateConfiguration"; + const string instanceName = "{ MSFT_DSCLocalConfigurationManager }"; + const string methodName = "SendMetaConfigurationApply"; + + string mof = File.ReadAllText(mofPath); + byte[] asciiBytes = Encoding.ASCII.GetBytes(mof); + + StringBuilder sb = new StringBuilder(); + sb.Append(" { ConfigurationData [ "); + foreach (byte b in asciiBytes) + { + sb.Append(b.ToString()); + sb.Append(' '); + } + sb.Append("] "); + sb.Append("}"); + string parameters = sb.ToString(); + + string arguments = $"iv {nameSpace} {instanceName} {methodName} {parameters} -xml"; + oi.ExecuteOmiCliCommand(arguments); + + OmiData data = oi.GetOmiData(); + object[] array = data.ToObjectArray(); + + WriteObject(array, true); + + } // EndProcessing + + #endregion + } + + #endregion + + #region Get-DscConfiguration + + /// + /// implementation for the Get-DscLocalConfiguration command + /// + [Cmdlet(VerbsCommon.Get, "DscLocalConfiguration" )] + [OutputType(typeof(object))] + public sealed class GetDscLocalConfigurationCommand : Cmdlet + { + #region methods + + protected override void ProcessRecord() + { + if (!Platform.IsLinux()) + { + throw new PlatformNotSupportedException(); + } + + OmiInterface oi = new OmiInterface(); + + const string nameSpace = "root/Microsoft/DesiredStateConfiguration"; + const string instanceName = "{ MSFT_DSCLocalConfigurationManager }"; + const string methodName = "GetMetaConfiguration"; + + string arguments = $"iv {nameSpace} {instanceName} {methodName} -xml"; + oi.ExecuteOmiCliCommand(arguments); + + OmiData data = oi.GetOmiData(); + object[] array = data.ToObjectArray(); + + WriteObject(array, true); + + } // EndProcessing + + #endregion + } + + #endregion + +} // namespace Microsoft.PowerShell.Commands + + diff --git a/src/Microsoft.PowerShell.Commands.Omi/OmiInterface.cs b/src/Microsoft.PowerShell.Commands.Omi/OmiInterface.cs index fc93cdbde4..dcae1f4b17 100644 --- a/src/Microsoft.PowerShell.Commands.Omi/OmiInterface.cs +++ b/src/Microsoft.PowerShell.Commands.Omi/OmiInterface.cs @@ -207,17 +207,25 @@ namespace Microsoft.PowerShell.Commands.Omi } IEnumerable valueArrays = property.Elements(VALUEARRAY); - foreach (XElement valueArray in valueArrays) + + if (valueArrays.Count() > 0) { - IEnumerable values = valueArray.Elements(VALUE); - foreach (XElement value in values) + foreach (XElement valueArray in valueArrays) { - Dictionary d = new Dictionary(dCommon); - data.Properties.Add(VALUE); - d[VALUE] = value.Value; - data.Values.Add(d); - } - } + IEnumerable values = valueArray.Elements(VALUE); + foreach (XElement value in values) + { + Dictionary d = new Dictionary(dCommon); + data.Properties.Add(VALUE); + d[VALUE] = value.Value; + data.Values.Add(d); + } + } + } + else + { + data.Values.Add(dCommon); + } } } diff --git a/src/Microsoft.PowerShell.Commands.Omi/test/Start-DscConfiguration.Tests.ps1 b/src/Microsoft.PowerShell.Commands.Omi/test/Start-DscConfiguration.Tests.ps1 index 40bd943ada..1fe1a39c38 100644 --- a/src/Microsoft.PowerShell.Commands.Omi/test/Start-DscConfiguration.Tests.ps1 +++ b/src/Microsoft.PowerShell.Commands.Omi/test/Start-DscConfiguration.Tests.ps1 @@ -3,9 +3,10 @@ $here = Split-Path -Parent $MyInvocation.MyCommand.Path $mof = "$here/assets/sample.mof" +$mofMeta = "$here/assets/sampleMeta.mof" $file = "/tmp/linux.txt" -Describe "Start-DscConfiguration" { +Describe "DscConfiguration" { Import-Module Microsoft.PowerShell.Commands.Omi It "Should create file per sample MOF file" { @@ -14,4 +15,21 @@ Describe "Start-DscConfiguration" { $s.VALUE | Should Be "0" $file | Should Exist } + + It "Should get DSC configuration successfully" { + $s = Get-DscConfiguration | Where-Object {$_.NAME -eq "ReturnValue"} + $s.VALUE | Should Be "0" + } + + It "Should set Meta MOF file properly" { + $s = Set-DscLocalConfiguration -ConfigurationMof $mofMeta | + Where-Object {$_.NAME -eq "ReturnValue"} + $s.VALUE | Should Be "0" + } + + It "Should get DSC local configuration successfully" { + $s = Get-DscLocalConfiguration | Where-Object {$_.NAME -eq "ReturnValue"} + $s.VALUE | Should Be "0" + } + } diff --git a/src/Microsoft.PowerShell.Commands.Omi/test/sampleMeta.mof b/src/Microsoft.PowerShell.Commands.Omi/test/sampleMeta.mof new file mode 100644 index 0000000000..f811287cc9 --- /dev/null +++ b/src/Microsoft.PowerShell.Commands.Omi/test/sampleMeta.mof @@ -0,0 +1,24 @@ +/* +@TargetNode='10.199.192.181' +@GeneratedBy=johnkord +@GenerationDate=01/15/2015 14:34:29 +@GenerationHost=JKORDIC-1 +*/ + + +instance of MSFT_DSCMetaConfiguration as $MSFT_DSCMetaConfiguration1ref +{ +DownloadManagerName = "WebDownloadManager"; +RefreshMode = "Push"; +ConfigurationMode = "ApplyAndAutoCorrect"; + +}; + +instance of OMI_ConfigurationDocument +{ +Version="1.0.0"; +Author="johnkord"; +GenerationDate="01/15/2015 14:34:29"; +GenerationHost="JKORDIC-1"; +}; +