Script VBS : Copier des variables de collection SCCM


Vous utilisez sans doute des variables de collections SCCM pour conditionner vos déploiements.

Je vous invite d’ailleurs à lire un de mes précédents articles à ce sujet : ICI.

Lors de ces implémentations, il est souvent pénible (car manuel) de manipuler les variables de collection. En effet, SCCM ne prévoit pas de « Clic Droit » pour copier les variables (souvent nombreuses) d’une collection à une autre …

Pour cela, j’ai donc décidé de « développer » un script VBS qui vous permettra très simplement de copier les variables d’une collection A vers une collection B.

L’appel se fait de manière très simple :

cscript CopyCollVar.vbs [source collection ID] [target collection ID]

Le script est téléchargeable ici : CopyCollVar

ou ci-dessous !

 
'*****************************************************************************************************************
' Description : This script is used to copy SCCM  "[source collection ID]" variables to the "[target collection ID]"
' Usage       : cscript CopyCollVar.vbs [source collection ID]  [target collection ID]
'
'Replace :  YOURSERVER by your local sccm server
'			YOURLOGIN by your domain login (domain\login) (or comment this line if not necessary)
'			YOUTPASSWORD by you domain password (or comment this line if not necessary)
'*****************************************************************************************************************

Const SiteServer = "YOURSERVER" 
' ***************************************
Dim connection
Dim automaticAuthentication
Dim userName, userPassword
' Specify username and password to authenticate with user credentials. Otherwise leave these lines commented.
'userName = "YOURLOGIN"
'userPassword = "YOURPASSWORD"

Set objArgs = WScript.Arguments
If objArgs.count = 2 Then
	srcCollection = objArgs(0)
	trgCollection = objArgs(1)
Else
	WScript.echo "Script syntax:" & vbCrLf & "cscript CopyCollVar.vbs [source collection ID]  [target collection ID]"
	WScript.quit
End If

If userName <> "" Then
	automaticAuthentication = False
Else
	automaticAuthentication = True
End If

Set connection = Connect(SiteServer,userName,userPassword)
If Err.Number<>0 Then
    WScript.echo "Unable to connect to site server "&SiteServer&"! Quitting..."
    WScript.quit
End If

copyVariables srcCollection, trgCollection

WScript.echo "Collection variables copied from "&srcCollection&" to "&trgCollection
WScript.Quit

' this function browse the source collection variables and call "CreateCollectionVariable" to create the variables on the target collection
Sub copyVariables(srcID, trgID)
	Set ObjSrcCollection = connection.Get("SMS_Collection.CollectionID='"&srcID&"'")
	Set ObjTrgCollection = connection.Get("SMS_Collection.CollectionID='"&trgID&"'")
	
	Set ObjSrcCollectionSettings = connection.Get("SMS_CollectionSettings.CollectionID='"&srcID&"'")
	
	for each variable in  ObjSrcCollectionSettings.CollectionVariables 	
		CreateCollectionVariable variable.Name, variable.value, trgID
	next
End Sub

' this function create the collection variables.
Sub CreateCollectionVariable( name, value, collectionId)

	Dim collectionSettings
	Dim collectionVariables
	Dim collectionVariable
	Dim Settings
	' See if the settings collection already exists. if it doesn't, create it.
	Set settings = connection.ExecQuery _
	("Select * From SMS_CollectionSettings Where CollectionID = '" & collectionID & "'")
   
	If settings.Count = 0 Then
		Wscript.Echo "Creating collection settings object"
		Set collectionSettings = connection.Get("SMS_CollectionSettings").SpawnInstance_
		collectionSettings.CollectionID = collectionId
		collectionSettings.Put_
	End If  

	' Get the collection settings object.
	Set collectionSettings = connection.Get("SMS_CollectionSettings.CollectionID='" & collectionId &"'" )
   
	' Get the collection variables.
	collectionVariables=collectionSettings.CollectionVariables

	' Create and populate a new collection variable.
	Set collectionVariable = connection.Get("SMS_CollectionVariable").SpawnInstance_
	collectionVariable.Name = name
	collectionVariable.Value = value
	collectionVariable.IsMasked = false

	' Add the new collection variable.
	ReDim Preserve collectionVariables (UBound (collectionVariables)+1)
	Set collectionVariables(UBound(collectionVariables)) = collectionVariable

	collectionSettings.CollectionVariables=collectionVariables
	collectionSettings.Put_
 End Sub   

' ~~~~~~~~~~~~~~
' this function connect to SCCM namespace
Function Connect(server, userName, userPassword)
'    On Error Resume Next
    Dim net
    Dim localConnection
    Dim providerLoc
    Dim location
    
    Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
    swbemLocator.Security_.AuthenticationLevel = 6 'Packet Privacy
    
    ' If  the server is local, don't supply credentials.
    Set net = CreateObject("WScript.NetWork") 
    If UCase(net.ComputerName) = UCase(server) Then
        localConnection = true
        userName = ""
        userPassword = ""
        server = "."
    End If
    
    ' Connect to the server.
    If automaticAuthentication Then
			Set objWMIService =    GetObject _
			    ("winmgmts:{impersonationLevel=Impersonate}!\\"&server&"\root\sms")
    Else
	    Set objWMIService = swbemLocator.ConnectServer(server, "root\sms",userName,userPassword)
		End If
    If Err.Number<>0 Then
        WScript.echo "Error! Couldn't connect. Error "&err.number&" (" + Err.Description + ")."
        Connect = null
        Exit Function
    End If

    ' Determine where the provider is and connect.
    Set providerLoc = objWMIService.InstancesOf("SMS_ProviderLocation")
        For Each location In providerLoc
            If location.ProviderForLocalSite = True Then
            	If automaticAuthentication Then
								Set objWMIService =    GetObject _
								    ("winmgmts:{impersonationLevel=Impersonate}!\\"&server&"\root\sms\site_" + location.SiteCode)            		
            	Else
                Set objWMIService = swbemLocator.ConnectServer(location.Machine, "root\sms\site_" + location.SiteCode,userName,userPassword)
              End If
                If Err.Number<>0 Then
                    WScript.echo "Error! Couldn't connect:" + Err.Description
                    Connect = Null
                    Exit Function
                End If
                Set Connect = objWMIService
                Exit Function
            End If
        Next
    Set Connect = null ' Failed to connect.
End Function
Please follow and like us:

Comments

  1. By its_me

  2. By Avi

  3. By Merci

  4. By User77

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Social media & sharing icons powered by UltimatelySocial