# Wednesday, December 16, 2009

I was wondering today if it would be possible to create an Update Search Folder in Powershell. Turns out you can but it isn’t as straight forward as you might think. According to the SCCM SDK Search Folders are an instance of the SMS_ObjectContainerNode class. You can find the details about this class here.

I’m using the SCCM.psm1 file by Michael Niehaus with a few modifications. I’ve added code to force asking for credentials and also added a function to create new SCCM object. My version is available at the end of this post. The additional function looks like:

function New-SCCMObject {

    [CmdletBinding()] 
    PARAM 
    ( 
        [Parameter(Position=1)] $class 
    ) 
	
	$staticClass = get-wmiobject -query "SELECT * FROM Meta_Class WHERE __Class = '$class'" -computername $sccmServer -namespace $sccmNamespace -Credential $sccmCredentials
	
	$staticClass.CreateInstance()

}

SMS_ObjectContainerNode has a property called SearchString that controls what the search filters are. I figured the easiest way to understand what should go in the SearchString property would be create a search folder manually and then view the property. Here is my Search Folder:
Screenshot

Here is the code to view the contents of the SearchString Property:

#Get the Update Search Folder and Return the XML.
$searchFolder = Get-SCCMObject -class SMS_ObjectContainerNode -filter "Name='All Current Workstation Updates'"
$searchFolder.Get()

#list the XML
$searchFolder.SearchString

The XML looks like:

	
		
			
				'Product:041e4f9f-3a3d-4f58-8b2f-5e6fe95c4591'
				'Product:558f4bc3-4827-49e1-accf-ea79fd72d4c9'
			
		
		
			
				MS
			
		
		
			
				 = 0
			
		
		
			
				 = 0
			
		
	
Pretty straight forward right?? Well no not really but anyway, here is how to create a search folder:

$server = "server"
$siteCode = "ABCa"

$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
Import-Module $scriptDir\SCCM.psm1

Connect-SCCMServer -serverName $server -siteCode $siteCode

$xml=[xml] @'

	
		
			
				'Product:041e4f9f-3a3d-4f58-8b2f-5e6fe95c4591'
				'Product:558f4bc3-4827-49e1-accf-ea79fd72d4c9'
			
		
		
			
				MS
			
		
		
			
				 = 0
			
		
		
			
				 = 0
			
		
	

'@

#create a new instance of a search folder
$newFolder = New-SCCMObject -class SMS_ObjectContainerNode

#Set the Properties
$newFolder.Name = "Workstation Updates"
$newFolder.ObjectType = 1011 #1011 = SMS_SoftwareUpdate
$newFolder.ParentContainerNodeID = 0
$newFolder.SearchFolder = $true
$newFolder.SearchString = $xml.InnerXML
$newFolder.FolderFlags = 1

#Save the folder
$newFolder.Put()

posted on Wednesday, December 16, 2009 2:00:30 PM (E. Australia Standard Time, UTC+10:00)  #    Comments [0]