talatham
6/13/2013 - 2:09 PM

FUNCTION - SCCM - MISC: VBscript functions for SCCM

VBScript functions for SCCM.

Includes:
* Connecting to SCCM
* Finding folders, packages and machines
* Creating software metering rules
* Remove machines from collections
'---------------- USAGE -------------------------

'Define the SCCM Site Server
Const SiteServer = "TAL001"
Const SiteCode = "TAL"

Call ConnectToSite() : 'Connect to Primary Site Server

'---------------- FUNCTION -------------------------
   
'Connect to the SCCM server
Sub ConnectToSite()
     
    On Error Resume Next
    Set oLocator = CreateObject("WbemScripting.SWbemLocator")
     
    'Connect to the SCCM Site Server
    Set oSMS = oLocator.ConnectServer(SiteServer, "root\sms\site_" & SiteCode)

    'To connect to a site under specific credentials:
    'Set oSMS = oLocator.ConnectServer(SiteServer, "root\sms\site_" & SiteCode, UserName, Password)

     
    'Quit the script with an error message if unable to connect
    If Err Then
        Err.Clear
        wScript.echo "Cannot connect to SCCM."
        wScript.Quit
    End If
     
    wScript.echo "Connected to " & SiteCode
    oSMS.Security_.ImpersonationLevel = 3
    oSMS.Security_.AuthenticationLevel = 6
End Sub
'------------- USAGE --------------------

RemoveMachineFromCollection("CEN00063")

'------------- FUNCTION -----------------

'Add a machine into the specified collection
Sub RemoveMachineFromCollection(collectionID)

  Dim oCollection : Set oCollection = oSMS.Get("SMS_Collection.CollectionID="&"""" & collectionID & """") :'Set collection
	Dim oCollectionRule : Set oCollectionRule = oSMS.Get("SMS_CollectionRuleDirect").SpawnInstance_() :'Set membership rule
	oCollectionRule.ResourceID = sResourceID
	oCollection.DeleteMembershipRule oCollectionRule
	wScript.echo "Removed from collection."
End Sub
'------------------USAGE------------------------------
Const ObjectType=3
Const ParentFolder=42

folderID = FindFolder("Microsoft Corporation")
wScript.Echo folderID

'------------------FUNCTION------------------------

' Search for a folder name, return the ID of the folder if found.
Function FindFolder(folderName)

  Set Folders = oSMS.ExecQuery ("select * from SMS_ObjectContainerNode where ObjectType = '" & ObjectType & "' and ParentContainerNodeID = '" & ParentFolder & "' and Name='" & folderName & "'")
	wScript.Echo "Searching for folder: " & folderName
	For Each oFolder in Folders
		'Return folder ID
		FindFolder = oFolder.ContainerNodeID
	Next
End Function
'------------------USAGE------------------------------

machineID = FindMachine("D01226954")
wScript.Echo packageID

'------------------FUNCTION------------------------

'Query site for machine name. Return resourceID.
Function FindMachine(machineName)

  Set oResults = oSMS.ExecQuery("SELECT ResourceID FROM SMS_R_System WHERE Name = '" & machineName & "'")
	For Each oResourceID In oResults
		FindMachine = oResourceID.ResourceID
	Next
End Function
'------------------USAGE------------------------------

packageID = FindPackage("Microstation")
wScript.Echo packageID

'------------------FUNCTION------------------------

' Search for a package name, return the ID of the package if found
Function FindPackage(packageName)

  Set Packages = oSMS.ExecQuery ("select * from SMS_Package where Name='" & packageName & "'")
	For Each oPackage in Packages
		'Return package ID
		FindPackage = oPackage.PackageID
	Next
End Function
Const LanguageID="65535"
Const Comment="Created by VBScript"
Const SiteCode="CEN"

'------------- USAGE --------------------

CreateMeteringRule ("Test Product", "test.exe", "1.0")

'------------- FUNCTION -----------------

'Create new software metering rule with given parameters
Sub CreateMeteringRule(productName, fileName, fileVersion)

  Dim oProduct : Set oProduct = oSMS.Get("SMS_MeteredProductRule").SpawnInstance_ : 'Create new MeteredProductRule object
	
	'Populate the MeteredProductRule object properties
	oProduct.ProductName = productName
	oProduct.FileName = fileName
	oProduct.OriginalFileName = fileName
	oProduct.FileVersion = fileVersion
	oProduct.LanguageID = LanguageID
	oProduct.Comment = Comment
	oProduct.SiteCode = SiteCode
	
	'Save the new rule
	On Error Resume Next
	oProduct.Put_
	
	If Err Then
		wScript.echo "Could not create rule ( " & productName & "). Error: " & Err.Description & " (" & Err.number & ")"
		Err.Clear
	Else
		wScript.Echo "Rule: " & productName & " created."
	End If
	
	
End Sub