Script - Add Computer to SCCM Collection - V

Another update to my ACTSC Script. I have added more error checking to the scrip and I have modified the Connect to Site GUI so you can filter your Collection results from there. I have also replaced the Cancel button with a clear button to make it easier to enter more then one device. Original Script and Description can be found HERE

This script is used to add a computer to a SCCM Collection

The Script will launch a GUI that will allow you to choose a collection then type the computer name you want to add to that collection

Filters the SCCM collections that are shown

Specifyes the Site Server to connect to

Specifies the Site Code of the SCCM enviroment

Tells the script to load the SCCM Module from the local computer or the server 

ACTSC.ps1 -SiteServer ServerName -SiteCode MSN
will run the GUI with out prompting for the Site Server and Site Code in order to not get the SiteGUI both parameters must be specified 

ACTSC.ps1 -Filter "Win7*"
Filters all collection that start with Win7

ACTSC.ps1 -LoadLocal
loades the SCCM Module from the local system

Created By: Kris Gross
Twitter: @kmgamd
Script Version:



#Generates the form to enter site info
function Generate-SITEform
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$SiteForm = New-Object System.Windows.Forms.Form 
$SiteForm.Text = "Site Info"
$SiteForm.Size = New-Object System.Drawing.Size(300,280) 
$SiteForm.StartPosition = "CenterScreen"

#Create the ok button
$SiteOKButton = New-Object System.Windows.Forms.Button
$SiteOKButton.Location = New-Object System.Drawing.Point(25,190)
$SiteOKButton.Size = New-Object System.Drawing.Size(75,23)
$SiteOKButton.Text = "OK"
$SiteForm.AcceptButton = $OKButton

#Create the cancel button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,190)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$Siteform.CancelButton = $CancelButton

#label for the site code
$Sitelabel = New-Object System.Windows.Forms.Label
$Sitelabel.Location = New-Object System.Drawing.Point(10,10) 
$Sitelabel.Size = New-Object System.Drawing.Size(280,20) 
$Sitelabel.Text = "Please enter the Site Code:"

#Box to enter site code
$SitetextBox = New-Object System.Windows.Forms.TextBox 
$SitetextBox.Location = New-Object System.Drawing.Point(15,32) 
$SitetextBox.Size = New-Object System.Drawing.Size(50,20)
$SitetextBox.ReadOnly = $False
$SitetextBox.Text = ""

#Site server label
$Serverlabel = New-Object System.Windows.Forms.Label
$Serverlabel.Location = New-Object System.Drawing.Point(10,60) 
$Serverlabel.Size = New-Object System.Drawing.Size(280,20) 
$Serverlabel.Text = "Please enter Site Server name:"

#Box to enter site server
$ServertextBox = New-Object System.Windows.Forms.TextBox 
$ServertextBox.Location = New-Object System.Drawing.Point(15,80) 
$ServertextBox.Size = New-Object System.Drawing.Size(250,20)
$ServertextBox.ReadOnly = $False
$ServertextBox.text = ""

$FilterLabel = New-Object System.Windows.Forms.Label
$FilterLabel.Location = New-Object System.Drawing.Point(10,110) 
$Filterlabel.Size = New-Object System.Drawing.Size(280,25)
$FilterLabel.Text = "Filter the Colletions you can choose from using * as a wildcard. * shows all Collections"

#Box to Filtr
$FiltertextBox = New-Object System.Windows.Forms.TextBox 
$FiltertextBox.Location = New-Object System.Drawing.Point(15,150) 
$FiltertextBox.Size = New-Object System.Drawing.Size(250,20)
$FiltertextBox.text = "*"

#Warrning lable that the script is working but way take some time to run the next section
$RunningLabel = New-Object System.Windows.Forms.Label
$RunningLabel.Location = New-Object System.Drawing.Point(10,220)
$RunningLabel.Size = New-Object System.Drawing.Size(280,20)
$RunningLabel.ForeColor = "Red"
$RunningLabel.Text = "Gathering Collection Info, This may take some time"
$RunningLabel.Visible = $false

#adds all items to the form 
$SiteForm.Topmost = $True

#shows the form

#Opens the Main form
Function Generate-Form 
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    #Imports the module for your site server so you dont need to have the sccm console installed to use it
    if ($LoadLocal -eq "$True") 
        Set-Location 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\'
        Import-Module .\ConfigurationManager.psd1 -verbose:$false   
        Import-Module \\$SiteServer\SMS_$SiteCode\AdminConsole\bin\ConfigurationManager.psd1 -verbose:$false

    #sets the site variable to be used for the Set-Location command
    $site = $SiteCode + ":"
    Set-Location $site

    #Gets the collections to add to the drop down list
    $CMCollections = (Get-CMCollection | Where-Object Name -like $Filter).name
    #Closes the Site form

    $form = New-Object System.Windows.Forms.Form 
    $form.Text = "ACTSC - V"
    $form.Size = New-Object System.Drawing.Size(400,230) 
    $form.StartPosition = "CenterScreen"

    #Ok button
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $form.AcceptButton = $OKButton

    #Cancle button
    $ClearButton = New-Object System.Windows.Forms.Button
    $ClearButton.Location = New-Object System.Drawing.Point(225,120)
    $ClearButton.Size = New-Object System.Drawing.Size(75,23)
    $ClearButton.Text = "Clear"

    #Collection dropdown label
    $Collectionlabel = New-Object System.Windows.Forms.Label
    $Collectionlabel.Location = New-Object System.Drawing.Point(10,20) 
    $Collectionlabel.Size = New-Object System.Drawing.Size(280,20) 
    $Collectionlabel.Text = "Please select a collection:"

    #Collection Dropdown list
    $objCollection = New-Object System.Windows.Forms.ComboBox
    $objCollection.Location = New-Object System.Drawing.Point(10,40)
    $objCollection.Size = New-Object System.Drawing.Size(305,20)
    $objCollection.DropDownStyle = "DropDownList"
    $handler_TSTypeBox_SelectedIndexChanged= {
            If (($objCollection.Text) -and ($ComputerNameBox.Text)) {
                $OKButton.Enabled = 1
                Else {
                    $OKButton.Enabled = 0
    Foreach ($item in ($CMCollections))
    $objCollection.Items.Add($item) | Out-Null

    #computer name label
    $Computerlabel = New-Object System.Windows.Forms.Label
    $Computerlabel.Location = New-Object System.Drawing.Point(10,65) 
    $Computerlabel.Size = New-Object System.Drawing.Size(280,20) 
    $Computerlabel.Text = "Please enter a computer name:"

    #Computer name textbox
    $textBox1 = New-Object System.Windows.Forms.TextBox 
    $textBox1.Location = New-Object System.Drawing.Point(10,85) 
    $textBox1.Size = New-Object System.Drawing.Size(260,20) 

    #Status of the script
    $Statuslabel = New-Object System.Windows.Forms.Label
    $Statuslabel.Location = New-Object System.Drawing.Point(10,170) 
    $Statuslabel.Size = New-Object System.Drawing.Size(350,20) 
    $Statuslabel.Text = "Waiting for Input"

    $form.Topmost = $True

    #Adds the objects to the form

    #Shows the form

#Runs the below actions when you click the ok button on the main form
function click-ok
    $Collection = $objCollection.Text
    $Computer = $textBox1.Text
    #Finds the computers ResourceID so it can be added to the collection
    $PCID = $(get-cmdevice -Name $Computer).ResourceID
    #Checks to see if the device is already in the collection
        $Statuslabel.ForeColor = "Red"
        $Statuslabel.Text = "ERROR: Must Choose a Collection"
        $Statuslabel.ForeColor = "Red"
        $Statuslabel.Text = "ERROR: Must Specify a Computer"

    if (Get-CMCollectionMember -CollectionName "$Collection" -ResourceID $PCID) 
          $Statuslabel.ForeColor = "Orange"
          $Statuslabel.Text = "$Computer is already in $Collection"                       
        #Adds the computer to the collection
        Add-CMDeviceCollectionDirectMembershipRule -CollectionName "$Collection" -ResourceID $PCID
        if (Get-CMCollectionMember -CollectionName "$Collection" -ResourceID $PCID)
            $Statuslabel.ForeColor = "Green"
            $Statuslabel.Text = " SUCCESS: $Computer was added to $Collection"
            $Statuslabel.ForeColor = "Red"
            $Statuslabel.Text = "ERROR: $Computer was not added to $Collection"

Function Click_Clear
    $textBox1.Text = ""
    $Statuslabel.ForeColor = "Black"
    $Statuslabel.Text = "Waiting for Input"

#Runs the below actions when you click the ok button on the site info form
Function click-okSite
    $SiteServer = $ServertextBox.text
    $SiteCode = $SitetextBox.text
    $Filter = $FiltertextBox.text
    $RunningLabel.Visible = $True
#looks to see if the $SiteServer and $SiteCode variables are set. If the variables are set it will run the main form if not it will run the Site info form
if ((!$SiteServer) -and (!$SiteCode)) { Generate-SITEform } Else {Generate-Form}

Popular posts from this blog

SCCM Task Sequence GUI - How to set up the TS to work with a GUI

SCCM Applications vs. SCCM Packages: Understanding the Key Differences

How to Deploy a Windows 10 Servicing update as a Application