Get ConfigMgr Machine Variables With Powershell

Machine Variables can be retrieved from computer objects in SCCM with the Powershell script at the bottom of the page.

Variables in SCCM can be very useful: Task Sequence Variables, Collection Variables, Machine Variables. I use Machine Variables to identify specific settings such as which Language to install the OS with – this one is pretty useful when you consider that UK, France and German-speaking countries, for a start, all have slight differences in keyboard layout.

I’m a big fan of complexity. I like massively over-engineered solutions for small tasks. The point is that even a motorised egg-cooker and alarm clock system such as Doc Brown’s in Back to the Future, offsets the overhead of required initial engineering against the savings made over time – it is just a matter of time before an automated task like this wins the efficiency battle. Yes, it takes 10 minutes for you to crack eggs in a pan and cook them and slap them onto a piece of bread, but it is ten minutes every time, of your time. If like me you could happily eat an egg sandwich every morning, that equates to 70 minutes per week, 60 hours in one year. I reckon Doc Brown would take less than 60 hours to conceive and build an egg-sandwich machine, lets say 30 hours, that leaves him 30 hours more free time in the first year and 60 every year thereafter. And the egg sandwich is always the same.

So building a complex solution can save time and increase efficiency. That is all.

What has this to do with Machine Variables? Not a lot except that building scripts which can automate tasks is a bit like building an egg-sandwich machine. When you then build the scripts to work against variables, or to generate variables and then consequently re-reference themselves against the generated variables, then you start to look at a pretty complex solution for clicking the screen that asks you which language to install an OS in. But this screen doesn`t appear straight away in OSD, so you would either wait for it, or go away and come back. Eventually a scripted, complex solution wins the efficiency argument, trust me it`s just a matter of time.

So, I have a script which writes machine variables to a computer object in SCCM. I want the script to reference these variables later, how do I get the variables and values?

In the Windows.SMS.Environment you can retrieve the SCCM variables on the local machine.

You could use remoting to get the variables in the same way.

But I want to reset a LastPXEAdvertisement according to flag set in a variable and the machine isn’t switched on. So I could open the SCCM console and find the machine, right-click and check the flag, then reset the PXEAdvert, for example. Then write another variable to say it has been reset.

I think I have gone on long enough about why automate, and why build complexity…. Here is a script to pull back the Machine Variables for a computer object from SCCM using Powershell:

objComputer = gwmi -computername “$smsProvider” -namespace “root\sms\site_$siteCode” -class “sms_r_system” | where{$_.Name -eq $computerName}

$objMachineSettings = gwmi -computername “$smsProvider” -namespace “root\sms\site_$siteCode” -class “sms_machinesettings” | where{$_.ResourceID -eq $objComputer.ResourceID}


$variable = ($objMachineSettings.MachineVariables | where{$_.Name -eq “$desiredVariable”}).Value

And that, aside from script variables which is something else entirely, is a script which will return the value of a specific variable. Use the SMS_MachineSettings Class and then populate the fields with the GET() command and there you are. Simple. But then complex machines are built of many, many simple parts. The trick is how to put them together.

Hope it helps someone, I spent a bit of time wondering why the MachineVariables field was empty until I realized I needed the second Get expression.

Time now for a nice, uncomplicated weekend….


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s