Script - Add Computer to SCCM Collection - V 2.1.1.3

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

<#
.SYNOPSIS
This script is used to add a computer to a SCCM Collection

.DESCRIPTION
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

.PARAMETER Filter
Filters the SCCM collections that are shown

.PARAMETER SiteServer
Specifyes the Site Server to connect to

.PARAMETER SiteCode
Specifies the Site Code of the SCCM enviroment

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

.EXAMPLE
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 

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

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

.NOTES
Created By: Kris Gross
Contact: Krisgross@jackofalltech.org
Twitter: @kmgamd
Script Version: 2.1.1.3

.LINK
http://www.sccmtst.com/
#>


Param(
    $SiteServer,
    $SiteCode,
    $Filter,
    [switch]$LoadLocal
    )

#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
$SiteOKButton.Add_Click({click-okSite})

#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 = ""

#Filter
$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
$SiteForm.Controls.Add($SiteOKButton)
$SiteForm.Controls.Add($CancelButton)
$SiteForm.Controls.Add($Sitelabel)
$SiteForm.Controls.Add($SitetextBox)
$SiteForm.Controls.Add($Serverlabel)
$SiteForm.Controls.Add($ServertextBox)
$SiteForm.Controls.Add($RunningLabel)
$SiteForm.Controls.Add($FilterLabel)
$SiteForm.Controls.Add($FiltertextBox)

#shows the form
[void]$SiteForm.ShowDialog()
}

#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   
    }
    else 
    {
        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
    $SiteForm.Dispose()
    $SiteForm.Close()

    $form = New-Object System.Windows.Forms.Form 
    $form.Text = "ACTSC - V 2.1.1.3"
    $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
    $OKButton.Add_Click({click-ok})

    #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"
    $ClearButton.Add_Click({Click_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
    }
    $objCollection.add_SelectedIndexChanged($handler_objCollectionBox_SelectedIndexChanged)

    #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
    $form.Controls.Add($OKButton)
    $form.Controls.Add($ClearButton)
    $form.Controls.Add($Collectionlabel) 
    $form.Controls.Add($objCollection)
    $form.Controls.Add($Computerlabel)
    $form.Controls.Add($textBox1)
    $form.Controls.Add($Statuslabel)

    #Shows the form
    [void]$form.ShowDialog()
}

#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
    if(!($Collection))
    {
        $Statuslabel.ForeColor = "Red"
        $Statuslabel.Text = "ERROR: Must Choose a Collection"
    }
    if(!($Computer))
    {
        $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"                       
    }
    else 
    {   
        #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"
        }
        else 
        {
            $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
    Generate-Form
}
#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

Office 365 Deployment - Office 365 Upgrade Task Sequence

SCCM Task Sequence with GUI - How to install more then one image with a single Task Sequence

Windows 10 Setup Script - V 2.0.0.0