Skip to content

Instantly share code, notes, and snippets.

@DexterPOSH
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DexterPOSH/247218c3a811b097adc3 to your computer and use it in GitHub Desktop.
Save DexterPOSH/247218c3a811b097adc3 to your computer and use it in GitHub Desktop.
#Load the ConfigurationManager Module
Import-Module -Name "$(split-path $Env:SMS_ADMIN_UI_PATH)\ConfigurationManager.psd1"
#Change location to my CMSite Provider
Cd DEX:
#Load the Default Parameter Values for Get-WMIObject cmdlet
$PSDefaultParameterValues =@{"get-wmiobject:namespace"="Root\SMS\site_DEX";"get-WMIObject:computername"="DexSCCM"}
#Add the Assembly Reference
Add-Type -Path "$(Split-Path $env:SMS_ADMIN_UI_PATH)\Microsoft.ConfigurationManagement.DesiredConfiguration.dll" -Verbose
Add-Type -Path "$(Split-Path $Env:SMS_ADMIN_UI_PATH)\Microsoft.ConfigurationManagement.ApplicationManagement.dll"
#Creating Type Accelerators - for making assemlby refrences easier later
$accelerators = [PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')
$accelerators::Add('SccmSerializer',[type]'Microsoft.ConfigurationManagement.ApplicationManagement.Serialization.SccmSerializer')
#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))
#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"
#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()
#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")
#tried setting the SDMPackageXML and using the put() method but it doesn't work.
#Below cmdlet updates the XML
Set-CMConfigurationItem -DesiredConfigurationDigestPath C:\temp\newxml.xml -Name $testCI.Localizeddisplayname -Verbose
@nateberrier
Copy link

I would LOVE LOVE LOVE LOVE to be able to do this for script settings, but have been able to find any documentation on it other than what you have written. Would you be able to help me out on this?

Trying to automatically create CIs with Script Settings that fail if the script outputs anything

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment