Skip to main content

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"

Popular posts from this blog

Test connectivity via a specific network interface

Recently while working on a Private cloud implementation, I came across a scenario where I needed to test connectivity of a node to the AD/DNS via multiple network adapters.  Many of us would know that having multiple network routes is usually done to take care of redundancy. So that if a network adapter goes down, one can use the other network interface to reach out to the node. In order to make it easy for everyone to follow along, below is an analogy for the above scenario: My laptop has multiple network adapters (say Wi-Fi and Ethernet) connected to the same network. Now how do I test connectivity to a Server on the network only over say Wi-Fi network adapter?

PowerShell + SCCM : Run CM cmdlets remotely

Today I saw a tweet about using implicit remoting to load the Configuration Manager on my machine by Justin Mathews . It caught my eye as I have never really tried it, but theoretically it can be done. Note - The second tweet says "Cannot find a provider with the name CMSite", resolution to which is in the Troubleshooting section at the end.

Azure DevOps Tips & Tricks - Find private REST APIs

Original source -  Azure DevOps Tip - Find private APIs Often working with Azure DevOps, I hit a wall trying to automate some tasks but there are no REST API's made public yet. It was one of those task of automating creation of Environments in multi-stage YAML based pipelines in AzDO. Quick research reveals that this has been requested in uservoice  (please upvote). Let's see one of the very simple ways to discover some of these APIs.