{"id":635,"date":"2013-05-20T16:15:16","date_gmt":"2013-05-20T14:15:16","guid":{"rendered":"http:\/\/sccmfaq.wordpress.com\/?p=635"},"modified":"2013-05-20T16:15:16","modified_gmt":"2013-05-20T14:15:16","slug":"sccm-2012-sp1-remove-client-from-collection-after-osd","status":"publish","type":"post","link":"https:\/\/blog.hosebei.ch\/?p=635","title":{"rendered":"SCCM 2012 SP1 &#8211; Remove Client from Collection after OSD"},"content":{"rendered":"<p>Times are changing, so are our scripts. I can&#8217;t tell you how often I used this VB-Script <a href=\"http:\/\/ccmexec.com\/2012\/07\/remove-from-collection-and-clear-pxe-flag-vbscript-using-status-filter-rule\/\">http:\/\/ccmexec.com\/2012\/07\/remove-from-collection-and-clear-pxe-flag-vbscript-using-status-filter-rule\/<\/a>\u00a0, but I think now, the new cmdlets for powershell are more comfortable. So I decided to change my auto-remove to a powershell script.<!--more--><\/p>\n<p><strong>This is what you need, that the script will run:<br \/>\n-Trust the Module that has to be imported <\/strong>(This will be explained in the Blog)<br \/>\n<strong>-The script has to be executed in x86 mode<br \/>\n-The server that runs the rule\u00a0has to be added with permissions<\/strong><\/p>\n<p style=\"text-align:left;\">So, let&#8217;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:<br \/>\n<a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection06.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-thumbnail wp-image-674 aligncenter\" alt=\"Start SCCM Powershell\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection06.png?w=150\" width=\"150\" height=\"120\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection06.png 380w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection06-300x242.png 300w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a><\/p>\n<p style=\"text-align:left;\">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.<\/p>\n<p style=\"text-align:left;\"><a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection01.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-636 aligncenter\" alt=\"Trust module\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection01.png?w=300\" width=\"300\" height=\"44\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection01.png 997w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection01-300x44.png 300w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection01-768x113.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>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&#8217;s upon you, how far you want to go. If you don&#8217;t add the server, the script will fail, because the psdrive of your site will not be found. Typical error message is: &#8220;Cannot find drive. A drive with the name &#8216;$Sitecode&#8217; does not exist.&#8221; When you start a powershell session with &#8220;NT AuthoritySystem&#8221; 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.<a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection05.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-659 aligncenter\" alt=\"SCCM Server permission\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection05.png?w=300\" width=\"300\" height=\"249\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection05.png 692w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection05-300x249.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>The Server also needs the DCOM Remote\u00a0Access Permission:<br \/>\n<a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection07.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-690 aligncenter\" alt=\"DCOM SCCM Permission\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection07.png?w=300\" width=\"300\" height=\"266\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection07.png 692w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection07-300x266.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>You will have to change the variable &#8220;$CollectionIDs&#8221; to apply it on your environment. You can change the variable $bEventlogEntry to &#8220;0&#8221;, then the script will not write in the Application log of the Server. And with $bClearPXE = &#8220;1&#8221; the PXE flag will be cleared after the computer is removed from the collections.<\/p>\n<p>Here is the script (It&#8217;s on my skydrive):<br \/>\n<a href=\"https:\/\/1drv.ms\/t\/s!Aq0GcVCqC0RlhTtNDcR9uJRgGf3X\">https:\/\/1drv.ms\/t\/s!Aq0GcVCqC0RlhTtNDcR9uJRgGf3X<\/a><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<code><br \/>\n#Call example:<br \/>\n#C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy ByPass RemoveFromCollection_1.0.ps1 %msgsc %msgsys<br \/>\n#%msgsc = Site-Code<br \/>\n#%msgsys = ComputerName<\/code><br \/>\n<code><br \/>\n#Set required Input Parameters<br \/>\nParam(<br \/>\n[string]$SiteCode,<br \/>\n[string]$ComputerName<br \/>\n)<\/code><br \/>\n<code><br \/>\n#--------------------<br \/>\n#User defined variables here<br \/>\n<\/code><code><br \/>\n$CollectionIDs = \"S0100025;S010002C;S010001E\"<br \/>\n$bEventlogEntry = \"1\"<br \/>\n$bClearPXE = \"0\"<\/code><br \/>\n<code><br \/>\n#End user defined variables<br \/>\n#--------------------<\/code><\/p>\n<p><code>If($SiteCode -and $ComputerName){}<br \/>\nelse{<br \/>\nWrite-Host \"Required Input is missing! Omit SiteCode and Computername.\"<br \/>\nexit<br \/>\n}<\/code><\/p>\n<p><code>#$SiteCode = \"S01\"<br \/>\n#$ComputerName = \"rudolph\"<\/code><\/p>\n<p><code>#Check for 32-bit execution<br \/>\nIf ($env:Processor_Architecture -ne \"x86\") {<br \/>\nwrite-host \"Script has to be executed in x86 mode!\"<br \/>\nexit<br \/>\n}<\/code><\/p>\n<p><code>#Import SCCM Module<br \/>\n$ModuleName = (get-item $env:SMS_ADMIN_UI_PATH).parent.FullName + \"ConfigurationManager.psd1\"<br \/>\nImport-Module $ModuleName<br \/>\nCD $SiteCode\":\"<\/code><\/p>\n<p><code>#Remove Client from collections<br \/>\n#Get collection id array<br \/>\n$aCollections = ($CollectionIDs).Split(\";\")<\/code><\/p>\n<p><code>#check for each collection if a directmember chip exist, and remove it<br \/>\nforeach($Collection in $aCollections){<br \/>\nIf((Get-CMDeviceCollectionDirectMembershipRule -CollectionId $Collection -ResourceName $ComputerName).count -eq 1) {<\/code><br \/>\n<code><br \/>\n#Write Eventlog entry<br \/>\nIf($bEventlogEntry -eq 1){<br \/>\nwrite-eventlog -logname Application -source \"SMS Client\" -eventID 3001 -entrytype Information -message \"Computer $ComputerName will be removed from Collection $Collection\" -category 1 -rawdata 10,20<br \/>\n}<\/code><br \/>\n<code><br \/>\n#Remove Client from collection<br \/>\nRemove-CMDeviceCollectionDirectMembershipRule -CollectionId $Collection -ResourceName $ComputerName -Force<\/code><\/p>\n<p><code>#Clear PXE Flag<br \/>\nIf($bClearPXE -eq 1){<br \/>\nClear-CMPxeDeployment -DeviceName $ComputerName<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n<\/code><br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>When you have\u00a0copied the script, then it&#8217;s time to create the Status Filter Rule:<\/p>\n<p style=\"text-align:center;\"><a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection02.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-647\" alt=\"Create Status Filter Rule\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection02.png?w=255\" width=\"255\" height=\"300\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection02.png 517w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection02-256x300.png 256w\" sizes=\"auto, (max-width: 255px) 100vw, 255px\" \/><\/a><\/p>\n<p>Fill up the following values in the Wizard:<br \/>\nName: <strong>Remove Client when OSD finished<\/strong><br \/>\nComponent: <strong>Task Sequence Manager<\/strong><br \/>\nMessage ID: <strong>11171<\/strong><\/p>\n<p style=\"text-align:center;\"><a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection03.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-648\" alt=\"Status Filter Rule wizard\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection03.png?w=300\" width=\"300\" height=\"256\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection03.png 762w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection03-300x256.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p style=\"text-align:left;\">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 &#8220;Run a program&#8221; option as following, <strong>make sure that you are using the full Path of the powershell executable<\/strong>:<br \/>\nC:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy ByPass &#8220;&lt;path to script&gt;RemoveFromCollection_1.0.ps1&#8221; %msgsc %msgsys<\/p>\n<p style=\"text-align:left;\">As written above, Powershell x86 is needed, probably you will have to define the execution policy and don&#8217;t forget the variables &#8220;%msgsc %msgsys&#8221;. The picture:<br \/>\n<a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection04.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-649 aligncenter\" alt=\"Stauts Filter Rule Action\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/05\/removefromcollection04.png?w=300\" width=\"300\" height=\"253\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection04.png 767w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/05\/removefromcollection04-300x253.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p style=\"text-align:left;\">Finish the wizard and enjoy the automatic removing of your clients from your OSD Collections.<\/p>\n<p style=\"text-align:left;\">Hope this helps, Martin \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Times are changing, so are our scripts. I can&#8217;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\/\u00a0, but I think now, the new cmdlets for powershell are more comfortable. So I decided to change my auto-remove to a powershell script.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,40],"tags":[],"class_list":["post-635","post","type-post","status-publish","format-standard","hentry","category-collection","category-tools"],"_links":{"self":[{"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=\/wp\/v2\/posts\/635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=635"}],"version-history":[{"count":0,"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=\/wp\/v2\/posts\/635\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}