List-Collection.ps1

This script will load the module SccmFunctions from the same directory the script is in. In the Module is the function Get-Sccmcollection defined. This function will return all members from a collection. The script will then loop through the members and print them onthe screen. So save the following code as list-collection.ps1 :

#***************************************************************************
#** Script:   List-Collection.ps1
#** Version:  1.0
#** Created:  12/2/2010 10:09AM
#** Author:   Adriaan Westra

#**
#** Purpose / Comments:
#**    List members of a SCCM Collection
#**
#**
#** Changelog :
#** 12/2/2010 10:09AM  : Initial version
#**
#***************************************************************************
# Clear the screen
Clear-Host
# import SccmFunctions so call to Get-SccmCollection function can be made
Import-Module .\SccmFunctions.psm1
#$DebugPreference = "Continue"                    # Display debug information
$DebugPreference = "SilentlyContinue"             # Do not display debug information
$SCCMServer = "asussrv01" 	                 # Name of the SCCM Site Server
$CollectionName = "All Windows 7 PC"             # Name of the collection to list 
#Loop through the results of the function List-collection to display the member names
foreach ($member in Get-SccmCollection  -servername $sccmserver -Collection $CollectionName) {
        $member.name 
}
# End of script.
exit 0

And save the following code to the same directory as SccmFunctions.psm1:

#***************************************************************************
#** Function:  Get-SccmCollection
#** Version:  1.0
#** Created:  01-12-2010 11:44
#** Author:   Adriaan Westra
#**
#** Purpose / Comments:
#**
#**  Get members from a SMS Collection.
#**
#** Change Log :
#**
#** 01-12-2010 11:44 : Initial Version
#**
#** Arguments :  
#**           -serverName      : Name of the SCCM Site Server
#**           -Collection      : Name of the collection
#**
#**
#** Returns : 
#**
#**           All members of the collection
#**   
#***************************************************************************
Function Get-SccmCollection {
[CmdletBinding()] 
PARAM 
    ( 
        [Parameter(Position=1)] $serverName,   
        [Parameter(Position=2)] $Collection
    ) 
    Write-Debug "Start Function"
    
    Write-Debug $serverName
    Write-Debug $Collection
    Write-Debug "Getting provider location for default site on server $serverName" 
# Get the providerlocation from WMI 
    $providerLocation = get-wmiobject -query `
                        "select * from SMS_ProviderLocation where ProviderForLocalSite = true" ` 
                        -namespace "root\sms" -computername $serverName -errorAction Stop 
# providerLocation.NamespacePath : \\Servername\root\sms\site_SiteCode                       
    Write-Debug "Provider location : $($providerLocation.NamespacePath)" 
   
# Sitpath = root\sms\site_SiteCode
    $SitePath = $providerLocation.NamespacePath.split( "\\",4)[3] 
# Get the collection from WMI
    $SmsCollection = Get-WmiObject -ComputerName $serverName -Namespace $SitePath `
                    -Query "Select * From SMS_Collection Where Name='$($Collection)'"
    $SmsCollection.Get()
    Write-Debug  "Collection ID : $($SmsCollection.CollectionId)"
# return the Members from the collection.
    return Get-WmiObject -ComputerName $serverName -Namespace $SitePath -Query `
               "SELECT * FROM SMS_FullCollectionMembership " + ` 
               "WHERE CollectionID='$($SmsCollection.CollectionId)' order by name"
    
    Write-Debug "End Function"
}

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *