Times are changing, so are our scripts. I can’t tell you how often I used this VB-Script http://ccmexec.com/2012/07/remove-from-collection-and-clear-pxe-flag-vbscript-using-status-filter-rule/ , but I think now, the new cmdlets for powershell are more comfortable. So I decided to change my auto-remove to a powershell script.
This is what you need, that the script will run:
-Trust the Module that has to be imported (This will be explained in the Blog)
-The script has to be executed in x86 mode
-The server that runs the rule has to be added with permissions
So, let’s start with the prerequisites, they are Powershell 3 and the SCCM 2012 SP1 Console, and you have to trust the module of the configuration manager psd file. This can be done through starting the powershell console via the SCCM console:
You should decide to always trust this module, otherwise the script will always ask, and when run through Status Filter Rules, the script will fail to run.
It is also necessary that the SCCM Server has right in SCCM itself. You can achieve this, when you add the SCCM Server via Console. I decided to use the Full Administrator role, it’s upon you, how far you want to go. If you don’t add the server, the script will fail, because the psdrive of your site will not be found. Typical error message is: “Cannot find drive. A drive with the name ‘$Sitecode’ does not exist.” When you start a powershell session with “NT AuthoritySystem” and try manually to import the module, when changing on the ps-drive, it will fail. So add your server as in the print-screen beyond.The Server also needs the DCOM Remote Access Permission:
You will have to change the variable “$CollectionIDs” to apply it on your environment. You can change the variable $bEventlogEntry to “0”, then the script will not write in the Application log of the Server. And with $bClearPXE = “1” the PXE flag will be cleared after the computer is removed from the collections.
Here is the script (It’s on my skydrive):
https://1drv.ms/t/s!Aq0GcVCqC0RlhTtNDcR9uJRgGf3X
————————–
#Call example:
#C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass RemoveFromCollection_1.0.ps1 %msgsc %msgsys
#%msgsc = Site-Code
#%msgsys = ComputerName
#Set required Input Parameters
Param(
[string]$SiteCode,
[string]$ComputerName
)
#--------------------
#User defined variables here
$CollectionIDs = "S0100025;S010002C;S010001E"
$bEventlogEntry = "1"
$bClearPXE = "0"
#End user defined variables
#--------------------
If($SiteCode -and $ComputerName){}
else{
Write-Host "Required Input is missing! Omit SiteCode and Computername."
exit
}
#$SiteCode = "S01"
#$ComputerName = "rudolph"
#Check for 32-bit execution
If ($env:Processor_Architecture -ne "x86") {
write-host "Script has to be executed in x86 mode!"
exit
}
#Import SCCM Module
$ModuleName = (get-item $env:SMS_ADMIN_UI_PATH).parent.FullName + "ConfigurationManager.psd1"
Import-Module $ModuleName
CD $SiteCode":"
#Remove Client from collections
#Get collection id array
$aCollections = ($CollectionIDs).Split(";")
#check for each collection if a directmember chip exist, and remove it
foreach($Collection in $aCollections){
If((Get-CMDeviceCollectionDirectMembershipRule -CollectionId $Collection -ResourceName $ComputerName).count -eq 1) {
#Write Eventlog entry
If($bEventlogEntry -eq 1){
write-eventlog -logname Application -source "SMS Client" -eventID 3001 -entrytype Information -message "Computer $ComputerName will be removed from Collection $Collection" -category 1 -rawdata 10,20
}
#Remove Client from collection
Remove-CMDeviceCollectionDirectMembershipRule -CollectionId $Collection -ResourceName $ComputerName -Force
#Clear PXE Flag
If($bClearPXE -eq 1){
Clear-CMPxeDeployment -DeviceName $ComputerName
}
}
}
————————–
When you have copied the script, then it’s time to create the Status Filter Rule:
Fill up the following values in the Wizard:
Name: Remove Client when OSD finished
Component: Task Sequence Manager
Message ID: 11171
In the action tab, you can choose to create a eventlog entry, if you want to see, that the status filter rule really triggers. And you will have to specify the “Run a program” option as following, make sure that you are using the full Path of the powershell executable:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass “<path to script>RemoveFromCollection_1.0.ps1” %msgsc %msgsys
As written above, Powershell x86 is needed, probably you will have to define the execution policy and don’t forget the variables “%msgsc %msgsys”. The picture:
Finish the wizard and enjoy the automatic removing of your clients from your OSD Collections.
Hope this helps, Martin 🙂
Leave a Reply to RussR Cancel reply