Partager via


More On SCCM and Powershell

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

get-help profiles

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:

connect-SCCMserver

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.

Disclaimer: The information on this site is provided "AS IS" with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation. Use of included script samples are subject to the terms specified in the Terms of Use .

This post was contributed by Tom Parker, a Dedicated Supportability Engineer with Microsoft Premier Field Engineering, UK.

Comments

  • Anonymous
    January 01, 2003
    What's the purpose of the $property parameter?  In the Add-SCCMCollectionRule cmdlet it only passes one parameter ($filter) to Get-SCCMComputer. Cheers

  • Anonymous
    January 01, 2003
    Hi Tom, I've put the functions together in a file and cannot add a computer to a collection.  Working with your example there is no connect-siteserver cmdlet, there's a Connect-SCCMServer cmdlet, so I assume you mean that.  There is no add-collectionmember cmdlet either, but there is a Add-SCCMCollectionRule cmdlet which Michael uses as an example.  I've tried using that one in it he calls Get-SCCMComputer which is not defined in any of his cmdlets. Do you have any working examples of this or actually succeeded in adding a computer to a collection? Thanks

  • Anonymous
    January 01, 2003
    Hi Packetboy, It's my understanding that all server products released from 2010 onwards will have to have powershell cmdlets included. I'm not aware of any coming for SCCM so I'm afraid that we'll probably have to wait until the next major release. Cheers, Tom

  • Anonymous
    January 01, 2003
    Fenster, Many thanks for that, you're absolutly correct. I've edited the post to make the nameing consistant. As yo can tell, in my own environment I've renamed some of these functions to make them more instictive for me. Here's the code for a Get-SCCMComputer function: function Get-SCCMComputer {    [CmdletBinding()]    PARAM    (        [Parameter(Position=1)] $filter, [Parameter(Position=2)] $Property    )    Get-SCCMObject SMS_G_System_System $filter $Property } With that in the relevant profile the Add-SCCMCollectionRule should now work. Thanks, Tom

  • Anonymous
    January 01, 2003
    Hi there Tom do you know when native powershell cmdlets will be included with SCCM?  I thought that all MS products now had to support powershell due to the common engineering criteria thing MS has... I would love some native PS cmds for SCCM!! It just seems its the last system centre app not to have them... Cheers Packetboy.