PowerShell Script Display Form Used to Enter Workstations Into SCCM Collection(s) Easily

I was tasked with deploying Windows 7 to an enterprise using SCCM 2007 OSD.  At first I was building my deployment collections using a query to read AD Security Group.  This was working fine until the customer started asking for machines to be removed from that nights deployment.  It was always last minute, so removing the machine name from the AD Security Group was not the ideal process for last minute exclusions.  I needed to be able to add machines into the collection quickly and allowed me to be able to edit the collection easily as needed.  I built this PowerShell script to display a simple form allowing me to choose which collection I want to add machines to, and then copy and paste the machine names into the text box and submit.  The machines names are entered into the collection as Direct Membership.  Since they are now Direct Membership objects, I could remove the name(s) fast before deploying the Task Sequence to the collection.

I ran this script on the SCCM Primary server; however, it could be modified to run remotely.   

Code:

$ErrorActionPreference = "Continue"
$SiteCode = "PRI"
$SCCMServerName = "localhost"

Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(400,500)
$Form.Text = "Add Members to Collection"

$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Select Collection:"
$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Size(20,20)
$Label.Size = New-Object System.Drawing.Size(10,200)
$Form.Controls.Add($Label)

$DropBox = New-Object System.Windows.Forms.ComboBox
$DropBox.Location = New-Object System.Drawing.Size(20,40)
$DropBox.Size = New-Object System.Drawing.Size(200,30)
$Form.Controls.Add($DropBox)

$Collections = Get-WmiObject -ComputerName $SCCMServerName -Namespace root\sms\site_$SiteCode -Query "Select * From SMS_Collection"
ForEach($Coll in $Collections){
$DropBox.Items.Add($Coll.Name) | Out-Null
}

$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = "Enter member names below:"
$Label2.Location = New-Object System.Drawing.Size(20,100)
$Label2.Size = New-Object System.Drawing.Size(100,20)
$Label2.AutoSize = $true
$Form.Controls.Add($Label2)

$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Multiline = $true
$TextBox.Scrollbars = 'both'
$TextBox.AcceptsReturn = $true
$TextBox.AcceptsTab = $false
$TextBox.Location = New-Object System.Drawing.Size(20,120)
$TextBox.Size = New-Object System.Drawing.Size(350,250)
$Form.Controls.Add($TextBox)

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(140,400)
$OKButton.Size = New-Object System.Drawing.Size(100,40)
$OKButton.Text = "Add"
$OKButton.Add_Click({$TBArray=$TextBox.Text;$Form.Close()})
$Form.Controls.Add($OKButton)

$Form.Add_Shown({$Form.Activate()})
[Void]$Form.ShowDialog()

Function AddMember{
$ResourceQuery = Get-WmiObject -Namespace "Root\SMS\Site_$SiteCode" -Class SMS_R_System -Filter "Name = '$PC'"
$CollectionQuery = Get-WmiObject -Namespace "Root\SMS\Site_$SiteCode" -Class SMS_Collection -Filter "Name = '$CollectionName'"
$CollectionQuery.Get()

$NewRule = ([WMIClass]"\\$SCCMServerName\root\SMS\Site_$SiteCode:SMS_CollectionRuleDirect").CreateInstance()
$NewRule.ResourceClassName = "SMS_R_System"
$NewRule.ResourceID = $ResourceQuery.ResourceID
$NewRule.RuleName = $ResourceQuery.Name

$CollectionQuery.CollectionRules += $NewRule.psobject.baseobject
$CollectionQuery.Put()
$CollectionQuery.RequestRefresh()
}

$CollectionName = $DropBox.SelectedItem.ToString()

Write-Host $CollectionName

$TBArray | Out-File C:\Temp\$CollectionName.txt
$List = Get-Content C:\Temp\$CollectionName.txt

ForEach($PC in $List){
  AddMember
}