I was working at a client site and was going through their server rollout procedure. I was quite shocked as to how many manual tasks they still had. One of these tasks was to add a computer directly to a SCCM collection. According to their requirements, they had to use direct membership and could not do a WMI call. So I created the following script and added it to their task sequence.

Problem –

The task sequence runs on the client machine and we really don’t want to install the SCCM PowerShell cmdlets on every server. Instead, what we’ll do is we’ll run the PowerShell remotely. The computer that is running the task sequence will open a remote connect and run them against the SCCM server. The SCCM server has the ConfigureManager PowerShell module, it can do the work for us!


Things to think about –

  • The computer running the task sequence needs to be able to use PowerShell remoting
  • Firewall’s are opened
  • SCCM Server has had Windows Remote Shell enabled
  • The account that runs it must have access to update the collection
$SccmServer = "SCCM01"
$PathToSCCMModule = "D:\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"

 $MemberName = $env:COMPUTERNAME
$SCCMSession = New-PSSession -ComputerName $SccmServer
Invoke-Command -Session $SccmSession -ArgumentList @($PathToSCCMModule, $MemberName) -ScriptBlock {
    Param (
    Import-Module $PathToSCCMModule -ErrorAction SilentlyContinue
    $SccmSite = (Get-PSDrive -PSProvider CMSite | Sort-Object -Property Name | Select-Object -First 1).Name
    Set-Location "$($SccmSite):"

     $ResourceID = (Get-CMDevice -Name $MemberName).ResourceID
    If ($ResourceID) {
        Add-CMDeviceCollectionDirectMembershipRule -CollectionName "SCEP - Servers" -ResourceId $ResourceID