Freigeben über


More Powershell “Cmdlets”

Following on from our last post on Powershell for SCCM here are some more “cmdlets” (functions really).

The Add-SCCMCollectionRule that Michael Niehaus provided can only add computer objects to a collection but one of my customers has been distributing software to users so we needed to be able to add users to collections:

function Add-DirUserCollectionRule
{
    [CmdletBinding()]
    PARAM
    (
    [Parameter(ValueFromPipelineByPropertyName=$true)] $collectionID,
        [Parameter(ValueFromPipeline=$true)] [String[]] $Createname
    )
    Process
    {

$coll = [wmi]"\\$sccmServer\$($sccmNamespace):SMS_Collection.CollectionID='$collectionID'"
        $ruleClass = [wmiclass]"\\$sccmServer\$($sccmNamespace):SMS_CollectionRuleDirect"
$RuleClass
$UserRule=Get-User "userName='$CreateName'"
$NewRuleName=$UserRule.name
$NewRuleResourceID = $UserRule.ResourceID
                        $newRule = $ruleClass.CreateInstance()

                        $newRule.RuleName = $NewRuleName
                        $newRule.ResourceClassName = "SMS_R_User"
                        $newRule.ResourceID = $NewRuleResourceID

                        $null = $coll.AddMembershipRule($newRule)
$coll.requestrefresh()
Clear-Variable -name oldrule -errorAction SilentlyContinue
Clear-Variable -name Coll -errorAction SilentlyContinue

    }
}

 

This is based on Michael’s code but adds a user to a collection in the form of a direct rule. It in turn uses a get-user “cmdlet”  which looks like this:

Function Get-User
{

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $filter,
    [Parameter(Position=2)] $Property
    )

    Get-SCCMObject SMS_R_User $filter $Property
}

 

So to add a user “contoso\parkert” to a collection with an CollectionID of CON00001 we type

Add-DirUserCollectionRule CON00001 contoso\parkert

 

Next up we have get-collectionmember which, as you would expect brings back the members of a collection whether they due to direct or query based rules.

function Get-CollectionMember
{

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $CollectionID,
    [Parameter(Position=2)] $Property
    )
    $filter = "CollectionID = '" + $CollectionID + "'"
    Get-SCCMObject SMS_CollectionMember_a $filter $Property
}

So:

get-collectionmember sms00001

Will return all resources in the All Systems collection. Don’t forget that these are being returned by Powershell as objects so that your script can then do something with them, such as add them to a different collection.

 

Finally, for now:

function Get-SubCollection
{

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $filter,
    [Parameter(Position=2)] $Property
    )
    Get-SCCMObject SMS_CollectToSubCollect $filter $property
}

which, guess what, returns child collections of a specified collectionid.

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
    Why does your Get-User function have a $property parameter when you don't actually pass anything to this parameter? i.e. $UserRule=Get-User "userName='$CreateName'" From Get-User you then pass three parameters (SMS_R_User $filter $Property) to Get-SCCMObject, but that function on Michael's post only accepts two parameters.  Do these examples work or are using modified functions from Michaels original post?