Share via


SCCM- remove the device after successful execution of Task seqeunces

Question

Friday, November 16, 2018 1:34 PM

hi 

Could you please suggest us any solution on the below.

After successful deployment of Task sequence the device should be removed from SCCM collection.

Thanks in Advance..

Jessy

JessyDG

All replies (3)

Saturday, November 17, 2018 3:30 PM

Plenty of examples:

https://www.systemcenterdudes.com/239/

https://ccmexec.com/2010/03/remove-a-computer-from-a-collections-when-osd-task-sequence-is-completed/

http://servertechs.info/remove-client-from-collection-after-successful-sccm-task-sequence-powershell/

In short: Use status filter rule 11171 and run a script on the SCCM server to do the magic.


Monday, November 19, 2018 8:21 AM

Hi Jessy,

Whether you are running CM2012 or CMCB in your environment, the following blog will be helpful to you:

https://servertechs.info/remove-client-from-collection-after-successful-sccm-task-sequence-powershell/

Best regards,
Larry

Please remember to mark the replies as answers if they help. If you have feedback for TechNet Subscriber Support, contact [email protected].


Wednesday, December 18, 2019 12:41 PM

You can use this script, it removes the device only from that collection, where the status message comes.

#####################################
# Remove Computer from Collection   #
# Markus Schmidt                    #
# 15.11.2019                        #
#####################################

# Configure Status Filter Rule
# General Tab
# Source: Client
# Component: Task SEquence Manager
# Message ID: 11171
# Actions Tab
# Run a program: "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe"  "set-executionpolicy bypass"; "PATH_TO_SCRIPT -SiteCode '%msgsc' -ComputerName '%msgsys' -MessageID '%msgid' -ProcessID '%msgpid' -ThreadID '%msgtid' -CollectionSyntax 'TASK_SEUQENCE_PREFIX*'"

# Customize following:
#PATH_TO_SCRIPT
#TASK_SEQUENCE_PREFIX 

Param
( 
    [string]$SiteCode, 
    [string]$ComputerName,
    [string]$MessageID,
    [string]$ProcessID,
    [string]$ThreadID,
    [string]$CollectionSyntax
)

# User Variables
if(-not $CollectionSyntax)
{
    $CollectionSyntax = "osd*" # MY Default Task Sequence OSD Prefix
}
$bEventlogEntry = "1" 
$debuglogpath = "C:\temp"

### DEBUG aktivieren ###
#$debugpreference = "Continue"
########################

### Debug ###
if($debugpreference -eq "Continue")
{
    $time = Get-Date -Format "yyyyMMddmmss"
    Start-Transcript "$debuglogpath\remove_computer_from_collection_$time.txt"
}
########################

Write-Debug "SiteCode: $SiteCode"
Write-Debug "ComputerName: $ComputerName"
Write-Debug "MessageID: $MessageID"
Write-Debug "ProcessID: $ProcessID"
Write-Debug "ThreadID: $ThreadID"
Write-Debug "CollectionSyntax: $CollectionSyntax"

#End user defined variables 
# 

If($SiteCode -and $ComputerName -and $MessageID -and $ProcessID -and $ThreadID)
{
} 
else
{ 
    Write-Error "Required Input is missing!"
    Write-Host "Script aborted"
    # Stop Transcript
    if($debugpreference -eq "Continue"){Stop-Transcript}
    exit 
} 

#Import SCCM Module 
Write-Debug "Import SCCM Module"
$ModuleName = (get-item $env:SMS_ADMIN_UI_PATH).parent.FullName + "\ConfigurationManager.psd1"
Import-Module $ModuleName 
New-PsDrive -Name $SiteCode -PSProvider AdminUI.PS.Provider\CMSite -Root localhost

if( -not (get-psdrive -Name $SiteCode -ErrorAction SilentlyContinue))
{
    Write-Error "PS Drive not available"
    Write-Debug "Script aborted"
    # Stop Transcript
    if($debugpreference -eq "Continue"){Stop-Transcript}
    exit 
}

#Change directory 
Set-Location $SiteCode":" 

#Build query with attributes
$query = "SELECT * FROM SMS_StatusMessage right join SMS_StatMsgAttributes on SMS_StatMsgAttributes.RecordID = SMS_StatusMessage.RecordID WHERE SMS_StatusMessage.machinename like '$ComputerName' AND SMS_StatusMessage.MessageID = $MessageID AND SMS_StatusMessage.ProcessID = $ProcessID AND SMS_StatusMessage.ThreadID = $ThreadID AND SMS_StatMsgAttributes.AttributeID = 400"

# Wait min 60 Seconds until wmi entry is available
Write-Debug "Start Sleep"
Start-Sleep -Seconds 60

# Get Package ID from WMI Database
$PacketAttribute = Get-WmiObject -Namespace "root\SMS\site_$SiteCode" -computername localhost -Query $query | Select-Object -ExpandProperty SMS_StatMsgAttributes
Write-Debug $PacketAttribute.AttributeValue

#search only for osd* collections based on package id
$CollectionID = (Get-CMTaskSequenceDeployment -TaskSequenceId $PacketAttribute.AttributeValue).CollectionID
$Collection = Get-CMDeviceCollection -Id $CollectionID | Where-Object {$PSITEM.Name -like $CollectionSyntax}
if(-not $Collection)
{
    Write-Debug "Status Message not from $CollectionSyntax Collection"
    Write-Debug "Collection was"(Get-CMDeviceCollection -Id $CollectionID).Name
    Write-Debug "Script aborted"
    # Stop Transcript
    if($debugpreference -eq "Continue"){Stop-Transcript}
    exit 
}

If($bEventlogEntry -eq 1)
{ 
    Write-EventLog -Logname Application -Source "SMS Client" -EventID 3001 -Entrytype Information -Message ("Computer " + $ComputerName + "will be removed from Collection " + $collection.Name) -Category 1 -Rawdata 10,20
} 

#Remove from osd collection and clear pxe
$Collection | Remove-CMDeviceCollectionDirectMembershipRule -ResourceName $ComputerName -Force
Clear-CMPxeDeployment -DeviceName $ComputerName 

Write-Debug "Script finished"

# Stop Transcript
if($debugpreference -eq "Continue"){Stop-Transcript}