Tuesday, July 01, 2014

PowerShell + SCCM 2012 : Add setting to a CI using PowerShell

Working with Peter van der Woude on how to add settings to a Configuration Items using PowerShell. I went with the manipulating the SDMPackageXML approach and Peter is working on a WQL Query way of doing this (can't wait to see his code).

Hoping that you know how to create a Configuration Item (CI) in ConfigMgr and how to add settings to a CI. This post is only on how to add a folder setting to a CI. Based on this methodology will try to add few more settings in the upcoming posts.

Let's get the ball rolling :

First get the prerequisites out of the way


001
002
003
004
005
006
007
008
009
010
011
012

#Load the ConfigurationManager Module
Import-Module -Name "$(split-path $Env:SMS_ADMIN_UI_PATH)\ConfigurationManager.psd1"


#Change location to my CMSite Provider
Cd DEX:


#Add the Assembly Reference to the DCM DLL
Add-Type -Path "$(Split-Path $env:SMS_ADMIN_UI_PATH)\Microsoft.ConfigurationManagement.DesiredConfiguration.dll" -Verbose

To begin with let's create a Configuration Item using the Cmdlet New-CMConfigurationItem and then load the xml into a variable $oldxml :

001
002
003
004
005
006
#create the CI and store it in a variable
$testCI = New-CMConfigurationItem -CreationType WindowsOS -Name "DexterPStest" -Description "Testing PS automation" -Verbose

#load the XML
$oldxml  = New-Object -TypeName xml
$oldxml.LoadXml($($testCI.SDMPackageXML))

After the above you should see a CI in the console (Note it's empty, no settings and the Revision is 1):






Now let's create a test Folder Setting


001
002
003
004
005
006

#now create a Folder setting
$foldersetting = New-Object -TypeName Microsoft.ConfigurationManagement.DesiredConfiguration.Settings.FolderSetting -ArgumentList "File_$([guid]::NewGuid())",'test','PS Automation'
$foldersetting.path = "C:\temp\test"
$foldersetting.FolderName = "Dexter"

The Settings you create have a method named SerializetoXML on them which can be used to get the XML, it needs an XMLWriter Object to be passed to it. So we create an XMLWriter Object with required settings and write the XML to a file (as I wanted to see the XML) but this can be done entirely using streams too:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018

#create the XML Writer Settings
$settings = New-Object system.Xml.XmlWriterSettings 
$settings.Indent = $true 
$settings.OmitXmlDeclaration = $false 
$settings.NewLineOnAttributes = $true 

# Create a new Writer
$writer = [system.xml.XmlWriter]::Create("C:\Temp\FolderSetting.xml", $settings) 

#Now let's serialize the XML and let it be written to our XML Writer
$foldersetting.SerializeToXml($writer)

#Flush the contents to the file
$writer.Flush()

#disposr the Writer Object
$writer.Dispose()

You would get a similar XML file :

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017

 <?xml version="1.0" encoding="utf-8"?>
<Folder
  LogicalName="File_49225e34-6398-4f90-8a93-17a203195784"
  Is64Bit="false"
  Depth="Base" xmlns="http://schemas.microsoft.com/SystemsCenterConfigurationManager/2009/07/10/DesiredConfiguration">
  <Annotation xmlns="http://schemas.microsoft.com/SystemsCenterConfigurationManager/2009/06/14/Rules">
    <DisplayName
      Text="test"
      ResourceId="ID-b36d34e8-2ac1-4a02-90bb-be17cd5e8b10" />
    <Description
      Text="PS Automation"
      ResourceId="ID-8f57bc35-a94b-4a6c-9e50-ddebcb549e60" />
  </Annotation>
  <Path>C:\temp\test</Path>
  <Filter>Dexter</Filter>
</Folder>


As I wrote the XML to a file I have to load it back. After that I need to Import the new FolderXML using the ImportNode method on the $OldXML object. Once it is imported you can append the child. But how do we know the node where we append the imported node ?

You can tackle this in 2 ways:

  1. Create a sample CI and study the SDMPackageXML property on it.
  2. If you have ConfigMgr SDK then go to the Samples>DesiredConfigurationManagement>Schema , there is a doc which shows how to author the XML digest using Visual Studio.
I was feeling a bit lazy so I went with the first approach ;)




001
002
003
004
005
006
007
008
009
010
011
012
013
#let's load the XML now
$FolderXML = New-Object -TypeName xml
$FolderXML.Load("C:\Temp\FolderSetting.xml")

#import the new Folder XML node to the old xml
$import = $oldxml.ImportNode($FolderXML.folder,$true)

#now add the above imported XML node
$oldxml.DesiredConfigurationDigest.OperatingSystem.Parts.AppendChild($import)

#save the new xml
$oldxml.Save("C:\temp\newxml.xml")

Now we have our XML ready, time to push this up to the CI instance.

We have a cmdlet by the name Set-CMConfigurationItem which allows us to specify the XML digest file to the parameter -DesiredConfigurationDigestPath. Let's give it a shot.

Note - I tried using the Put() method on the WMI instance of the CI it returns no error but it seems to do nothing. I will have to check on this later.


001
Set-CMConfigurationItem -DesiredConfigurationDigestPath C:\temp\newxml.xml -Name $testCI.Localizeddisplayname -Verbose

Oh Yeah ! it does work and I see the changes reflect in the CI settings tab:




I tried to keep things simple for now as there are lots of moving pieces and I want to tackle them one at a time.


001
Write-Output -InputObject "Cya till next post"

2 comments:

  1. Hey Deepak,

    Have you tried this for "windowsapplication" ci's?
    I can construct the xml manually and subsequently use set-cmdeploymenttype, but I can't build the xml using a setting like you do as I can't find a constructor for the detection method.

    ReplyDelete
    Replies
    1. To be honest, I did all this work with ConfigMgr 2012 two years back. Don't remember much of it as I don't do ConfigMgr solely nowadays (until need arises). I can't commit on when I will try creating CI for Windows Application.

      IMO it should be possible.

      Delete