I thought I would add to our previous post on Powershell. Powershell can make SCCM admin far easier for repetitive tasks. To make life really easy we can start creating our own “cmdlets”. Anyone can write their own cmdlets for Powershell but it’s heavy duty coding so we’re going to fake it by adding functions into a profile. Profiles are loaded when powershell starts so the functions created are available at the powershell console. By giving the functions sensible names then using them with powershell makes it seem as if we’ve created our own cmdlets.
You can get lots of information on profiles by typing
and there’s also this MSDN article.
Whilst there isn’t a great deal of information out there in terms of powershell scripts for SCCM we’re standing on the shoulders of Giants because Michael Niehaus has already done a lot of the hard work for us and published a set of functions here, here and here. I thoroughly recommend putting the three bits of code together in a single profile and experimenting with the resulting “cmdlets”.
Once the scripts that Michael gives in his posts have been appended to each other and put into a relevant profile then to connect to a local site (i.e. we’re running up powershell on a site server) all we have to do is type:
and we’re connected into the SMS provider. This means that we now have all the access to create and delete objects as we have through the MMC. Of course you could be really clever and include this command in the profile so that you automatically connect to the site when you run up Powershell. Now as Michael points out in the third of the links above all an admin has to do to an add a list of computers to an existing collection is to have that list in the form of a txt file and it becomes the simple matter of reading the list of machines out of the file (using the Get-Content cmdlet) and porting them to the add-collectionmember “cmdlet”:
get-content c:\MyListOfMachines.txt | add-SCCMCollectionRule –collectionID CEN00001
I’d say that that is a pretty simple bit of coding and it’s a lot easier than the vbs script I had to achieve the same thing on good old SMS 2003.
The great thing about storing these functions in a profile.ps1 file is that as an admin writes more functions and adds them in then the scripts required to do stuff become simpler and simpler.
That’s all for now, hope you find this useful. Lots more functions to come.
This post was contributed by Tom Parker, a Dedicated Supportability Engineer with Microsoft Premier Field Engineering, UK.