Pages

Monday, April 9, 2018

Powershell Script to Create SCCM Collections Based on Device Models

In an environment that SysAdmins doesn't have any idea about how many number of device models exists, managing device drivers will be a cumbersome task.

Following is a Powershell script to create collections based on device models that already inventoried by SCCM.


I'm not the creator of this. I found this on internet. I just did small modifications to make the script work without errors. All credits should go to original author of the script. 😃😃

-----------------------------------------------------------------------------------------------------------------------
[CmdletBinding()]
Param (
    [Parameter(Mandatory = $true)]
    [string]$cmSiteID
)
#region Modules
# Import ConfigMgr module
Import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + ‘\ConfigurationManager.psd1’)
#endregion
#region Variables
$rootCMDrive = "$($cmSiteID):"
Set-Location $rootCMDrive
$curModels = @()
$Schedule = New-CMSchedule –RecurInterval Days –RecurCount 7
#endregion

#region Preparation
# Create Models folder
 New-item $rootCMDrive\DeviceCollection\Models
#endregion
function newModelCollection ([string]$modelName)
{
    Set-Location $rootCMDrive
    if (!(Get-CMDeviceCollection -Name $modelName))
    {
        New-CMDeviceCollection -Name $modelName -LimitingCollectionName 'All Systems' -RefreshSchedule $Schedule -RefreshType 2
        Add-CMDeviceCollectionQueryMembershipRule -CollectionName $modelName -QueryExpression "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System inner join SMS_G_System_COMPUTER_SYSTEM on SMS_G_System_COMPUTER_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_G_System_COMPUTER_SYSTEM.Model = `"$($modelName)`"" -RuleName "Model is $($modelName)"
        Move-CMObject -InputObject (Get-CMDeviceCollection -Name $modelName) -FolderPath "$rootCMDrive\DeviceCollection\Models"
    }
}
Set-Location $rootCMDrive
$curModels = Invoke-CMWmiQuery -query "select distinct SMS_G_System_COMPUTER_SYSTEM.Manufacturer, SMS_G_System_COMPUTER_SYSTEM.Model from  SMS_R_System inner join SMS_G_System_COMPUTER_SYSTEM on SMS_G_System_COMPUTER_SYSTEM.ResourceID = SMS_R_System.ResourceId" | Select Manufacturer, Model

Foreach ($model in $curModels)
{
        newModelCollection($model.model)
}
-----------------------------------------------------------------------------------------------------------------------

2 comments:

  1. I get "Invoke-CMWmiQuery : The term 'Invoke-CMWmiQuery' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

    ReplyDelete
    Replies
    1. SCCM 2012 R2 CU3 if that makes a difference

      Delete