Create Machine variables in Configuration Manager 2012
|
by David O'Brien on 05/08/2012 | 0 Comments | 5,760 Views
|
I scripted quite a lot these last few days/weeks and needed to use, as a good scripter like me would (*g*), lots of variables.
Variables in Configuration Manager can come real handy when executing Task Sequences.
You can set variables on collections and machines.
Real life example
A company would like to implement a OSD task sequence that’s totally, 100% generic. Any device has to get installed by this single task sequence. ![]()
You all probably know that this is something customers/companies would like to see. Of course there are a lot of reasons not to to everything in only one task sequence (ie usability, size limitation of TS, etc)
Out of this requirement came the following idea to set all needed variables via script. It’s surprisingly easy!
A bit more info: The script which I’m going to show you below is part of a workflow during which some more scripts are called and thus all sorts of different environment/script/Powershell variables are set and inherited by this script. To make understanding the script a bit easier I tried to make the script as generic as possible.
XML is the key
I chose to set all the variables I need per machine basis. In the fully automated environment we could use collection variables which set some default variables per collection and then set machine specific variables via script.
Where do I define the variables I want to set on my machines?
I thought about parameters I could use with the script, but that’s kind of too far away from user friendly, ini files are really oldschoooooool and not always easy to understand (at least I think so) and then I came across XML files. Then I thought, Powershell has some really cool ways of working with XML files.
So I went ahead with defining my variables in an XML file which looks like this:
1: <ConfigMgrMachineVariables> 2: <ConfigMgrMachineVariable> 3: <varName>OS</varName> 4: <varValue>WindowsXP</varValue>5: <IsMasked>false</IsMasked>
6: </ConfigMgrMachineVariable> 7: <ConfigMgrMachineVariable> 8: <varname>HardwareID</varname> 9: <varvalue>$HardwareID</varvalue>10: <IsMasked>false</IsMasked>
11: </ConfigMgrMachineVariable> 12: <ConfigMgrMachineVariable> 13: <varname>Password</varname> 14: <varvalue>1234567890</varvalue>15: <IsMasked>true</IsMasked>
16: </ConfigMgrMachineVariable> 17: </ConfigMgrMachineVariables>Easy, isn’t it?
The variable “OS” is defined from line 2 to line 6. That’s it! ![]()
Line no. 9 is something special. I can go and use environment variables during my XML interpretation and that’s really cool. This way I don’t have to hardcode everything into the XML file.
Interpret XML
This is the script that interprets the above XML file:
1: function create-Variable {2: $MachineSettings = Get-WMIObject -Namespace "Root\SMS\Site_$sitename" -class "SMS_MachineSettings" -Filter $ResourceID
3: 4: # Create new instance of MachineSettings if not found
5: If (!$MachineSettings) {6: $NewMachineSettingsInstance = $([wmiclass]"\\.\Root\SMS\Site_$($sitename):SMS_MachineSettings").CreateInstance()
7: $NewMachineSettingsInstance.ResourceID = $ResourceID 8: $NewMachineSettingsInstance.SourceSite = $sitename 9: $NewMachineSettingsInstance.LocaleID = 1033 10: $NewMachineSettingsInstance.psbase 11: $NewMachineSettingsInstance.psbase.Put() 12: $MachineSettings += $NewMachineSettingsInstance 13: } 14: 15: 16: ForEach ($Server in $MachineSettings) {
17: $Server.psbase.Get() 18: $MachineVariables = $Server.MachineVariables 19: [xml]$varfile = Get-Content .\configmgrmachvar.xml20: ForEach ($ConfigMgrMachineVariable in $varfile.ConfigMgrMachineVariables.ConfigMgrMachineVariable) {
21: $ConfigMgrVar = $([wmiclass]"Root\SMS\Site_$($sitename):SMS_MachineVariable").CreateInstance()
22: $ConfigMgrVar.Name = $ConfigMgrMachineVariable.varName 23: $ConfigMgrVar.Value = $ExecutionContext.InvokeCommand.ExpandString($ConfigMgrMachineVariable.varValue) 24: #$ConfigMgrVar.IsMasked = [Int]$($ConfigMgrMachineVariable.IsMasked) 25: 26: [System.Management.ManagementBaseObject[]]$MachineVariables += $ConfigMgrVar 27: } 28: $Server.MachineVariables = $MachineVariables 29: $Server.psbase.Put() 30: } 31: } 32: 33: $sitename = "PRI"
34: $ResourceID = "" #you have to find out the ResourceID of the machine you want to add the variables to and set it here
35: create-Variable #call the functionLines 1 to 15 are mainly blabla, the interesting part is below!
I’m going through the whole XML file (line 19) and then taking every variable block (as said above that’s from <ConfigMgrMachineVariable> to </ConfigMgrMachineVariable>) I pipe the values into a newly created WMI instance to create a new machine variable.
I will soon try to integrate this script into my ConfigMgr Admin GUI, to make setting machine variables easier than it already is.
- ‹ previous
- 25 of 55
- next ›
Recent Articles
About the author
![]() |
David O'Brien IT-Consultant Blogs about VDI, AppDNA, Microsoft ConfigMgr and deployment topics in general are my "thing" |





Add Comment