PowerShell Add Variables to Machines from CSV SCCM ConfigMgr

So it used to be a bit trickier to add a custom variable or multiple variables to a machine object in SCCM/ConfigMgr. But since the addition of the awesome New-CMDeviceVariable Cmdlet it’s a breeze!

Basically I’m in the midst of automating a clients Server Builds. As they have a lovely spreadsheet with all details of each server I thought I’d nock this up as a starter this evening.

So the script imports the CSV specified and from there creates the variables supplied in the CSV to the machine that is named in the CSV (that’s a lot of CSV). If the script cant find the Machine object (say you are doing hundreds or thousands!) that is named in the CSV file it will log that name to a text file called “NotFound.txt” in the script root folder.

As always comments throughout the script explain every step.

Anyway here is the script and CSV example:

CSV:

Remotely Start a Service with a list of Machines

So the other day I was on client site dealing with WES 2009 HP t510 thin clients. Now if anyone has ever dealt with WES 2009 and ConfigMgr (SCCM) then you will know it can be challenging.

Anyway the clients on the machines were failing the client check, this was because the Task Scheduler Service is not started by default in WES 2009. The Client Health check that the SCCM client performs on itself is started from a scheduled task, if that service is not started… Well there is your problem.

I needed a way to start that service on all the clients now without deploying to them as there was a write filter enabled or making a GPO change and rebooting them as users were working and on top, the next Maintenance Window was not for about 8 hours. So I came up with this very simple but highly effective for each loop. Firstly I wanted to double check they were all stopped so..

[code language=”powershell”]
foreach ($Machine in get-content c:\temp\Machines.txt) {cmd /c SC \\$Machine query Schedule}
[/code]

The text file must have a machine name per line and I’m sure you’ve noticed I’m using a CMD instead of PowerShell.. WES 2009 need I say more?

Once I was satisfied I changed one switch and hey presto it fired off the start command there and then to each machine.

[code language=”powershell”]
foreach ($Machine in get-content c:\temp\Machines.txt) {cmd /c SC \\$Machine start Schedule}
[/code]

For those who are not looking at WES 2009 here is the PowerShell command.

[code language=”powershell”]
foreach ($Machine in get-content c:\temp\Machines.txt) {
Get-Service -Name "Task Scheduler" -ComputerName $Machine | Set-Service -Status Running
}
[/code]

I did also change the GPO to auto start the service on the next reboot so I would never have to do it again 🙂

 

Copyright 2016 SCCMOG | All Rights Reserved