Adding Get-DscConfiguration, and Get/Set-DscLocalConfiguration cmdlets

This commit is contained in:
George Fleming
2016-01-05 12:03:22 -08:00
parent b21977bcce
commit ef6f5d06e5
5 changed files with 229 additions and 11 deletions
@@ -14,7 +14,7 @@ namespace Microsoft.PowerShell.Commands.Omi
/// implementation for the Start-DscConfiguration command
/// </summary>
[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
/// <summary>
/// implementation for the Get-DscConfiguration command
/// </summary>
[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
@@ -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
/// <summary>
/// implementation for the Set-DscLocalConfiguration command
/// </summary>
[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
/// <summary>
/// implementation for the Get-DscLocalConfiguration command
/// </summary>
[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
@@ -207,17 +207,25 @@ namespace Microsoft.PowerShell.Commands.Omi
}
IEnumerable<XElement> valueArrays = property.Elements(VALUEARRAY);
foreach (XElement valueArray in valueArrays)
if (valueArrays.Count() > 0)
{
IEnumerable<XElement> values = valueArray.Elements(VALUE);
foreach (XElement value in values)
foreach (XElement valueArray in valueArrays)
{
Dictionary<string, string> d = new Dictionary<string, string>(dCommon);
data.Properties.Add(VALUE);
d[VALUE] = value.Value;
data.Values.Add(d);
}
}
IEnumerable<XElement> values = valueArray.Elements(VALUE);
foreach (XElement value in values)
{
Dictionary<string, string> d = new Dictionary<string, string>(dCommon);
data.Properties.Add(VALUE);
d[VALUE] = value.Value;
data.Values.Add(d);
}
}
}
else
{
data.Values.Add(dCommon);
}
}
}
@@ -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"
}
}
@@ -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";
};