#
# WSUS_Sorting_HAT.PS1
#
#DOCUMENTATION:
#Written by Kevin Trent, kevin.trent@carecentrix.com

#CHANGE LOG: 
	#Version 1.0 6/12/2018 - First functional edition - Kevin Trent 

#UPGRADES: 
	# Planned: 
		# Add auto detection of new OU and auto creation of new function. - KJT 

#DESCRIPTION: 
#WSUS Server-Side targeting chosen to enable flexibility with group assignment. Sorting Hat, takes the place of client-side targeting in a GPO.
#Servers are still added to the unassigned computers group via linked GPO:
	#(Prod Server SUS 2016 - PSUS002 - Server Side Targeting, DEV Server SUS 2016 - PSUS002 - Server Side Targeting, QA Server SUS 2016 - PSUS002 - Server Side Targeting)  
#Each function sorts systems from the WSUS unassigned computers group to an appropriate target group based on the AD OU.

#MAINTENANCE:
#NEW Active Directory OUs require reciprocal functions to be added.
	#New Function Copy/Paste existing function and make these required changes: 
		#Function name = WSUS Group Name, remove spaces - between each word. 
		#Variable name - WSUS Group Name, remove spaces change - to _. TargetGroupName = WSUS Group name. 
		#Canonicalname = OU with wildcard (*) in place of machine name(s).
			#Find Canonicalname of a group by running the following command against a machine in that OU: Get-ADComputer -Identity qbiz111 -Properties *|select CanonicalName
		#Add Group function to the Run function: (If $newservers.count -gt 0) Scriptblock near the bottom, so that the new function is called when the script is executed.
		#Add comment to the ChangeLog remark above 

#Module Import. This Script requires RSAT and PowerShell v3.0 or higher on the executing system.
Import-Module ActiveDirectory
Import-Module WindowsUpdate

#System Collection:
#Building the collections to be sorted from the WSUS unassigned computers group removes the need to validate each member.
	#Each array memeber can only be in the collection if it passed cetificate, protocol, and connectivity requirements.  
$newserversfull = Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -ComputerTargetGroups "Unassigned Computers"|select Fulldomainname -ExpandProperty Fulldomainname

#Shortening the array member names to a useable Get-ADComputer Identity
$newservers = Foreach ($server in $newserversfull) {$server.split(‘.’)[0]}

#SORTING FUNCTIONS:
#Each function runs each memeber of the collection through a loop that contains an AD-OU query. If the member(s) are in the specified AD-OU they are inserted into the WSUS Target Group.
#For simplified locating; functions are grouped under comments with the WSUS group parent name. Example, NCRA QUALITY ASSURANCE SERVERS or CTHA PRODUCTION SERVERS

#CTHA PRODUCTION SERVERS
Function CTHA-Verint-PROD  {
    $CTHA_Verint_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/CTHA/Servers/Production Servers/Verint Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $CTHA_Verint_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "CTHA - Verint - PROD"
    }

}

#KSOP PRODUCTION SERVERS
Function KSOP-Verint-PROD  {
    $KSOP_Verint_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/KSOP/Servers/Production Servers/Verint Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $KSOP_Verint_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "KSOP - Verint - PROD"
    }

}

#AZPH PRODUCTION SERVERS
Function AZPH-Verint-PROD  {
    $AZPH_Verint_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/AZPH/Servers/Production Servers/Verint Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $AZPH_Verint_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "AZPH - Verint - PROD"
    }

}

#FLT2 PRODUCTION SERVERS
Function FLT2-Application-PROD  {
    $FLT2_Application_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/FLT2/Servers/Production Servers/Application Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $FLT2_Application_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "FLT2 - Application - PROD"
    }

}

Function FLT2-Verint-PROD  {
    $FLT2_Verint_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/FLT2/Servers/Production Servers/Verint Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $FLT2_Verint_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "FLT2 - Verint - PROD"
    }

}

Function FLT2-Voice-PROD  {
    $FLT2_Voice_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/FLT2/Servers/Production Servers/Voice Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $FLT2_Voice_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "FLT2 - Voice - PROD"
    }

}

#NCRA DEVELOPMENT SERVERS
Function NCRA-Application-DEV {
    $NCRA_Application_DEV = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Development Servers/Application Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Application_DEV) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Application - DEV"
    }

}

Function NCRA-BizTalk-DEV {
    $NCRA_BizTalk_DEV = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Development Servers/BizTalk Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_BizTalk_DEV) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - BizTalk - DEV"
    }

}

Function NCRA-Edifecs-DEV {
    $NCRA_Edifecs_DEV = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Development Servers/Edifecs Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Edifecs_DEV) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Edifecs - DEV"
    }

}

Function NCRA-Remote-Desktop-DEV{
    $NCRA_Remote_Desktop_DEV = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Development Servers/Remote Desktop Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Remote_Desktop_DEV) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Remote Desktop - DEV"
    }

}

Function NCRA-SQL-DEV {
    $NCRA_SQL_DEV = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Development Servers/SQL Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_SQL_DEV) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - SQL - DEV"
    }

}

Function NCRA-Terminal-Servers-DEV {
    $NCRA_Terminal_Servers_DEV = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Development Servers/Terminal Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Application_DEV) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Terminal Servers - DEV"
    }

}

Function NCRA-Voice-DEV {
    $NCRA_Voice_DEV = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Development Servers/Voice Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Voice_DEV) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Voice - DEV"
    }

}

Function NCRA-Web-DEV {
    $NCRA_Web_DEV = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Development Servers/Web Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Web_DEV) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Web - DEV"
    }

}

#NCRA PRODUCTION SERVERS:
Function NCRA-Application-PROD {
    $NCRA_Application_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/Application Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Application_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Application - PROD"
    }

}

Function NCRA-BizTalk-PROD  {
    $NCRA_BizTalk_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/BizTalk Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_BizTalk_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - BizTalk - PROD"
    }

}

Function NCRA-Edifecs-PROD {
    $NCRA_Edifecs_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/Edifecs Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Edifecs_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Edifecs - PROD"
    }

}

Function NCRA-Exchange-PROD {
    $NCRA_Exchange_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/Exchange Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Exchange_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Exchange - PROD"
    }

}

Function NCRA-Mail-Terminal-Servers-PROD  {
    $NCRA_Mail_Terminal_Servers_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/Mail Terminal Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Mail_Terminal_Servers_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Mail Terminal Servers - PROD"
    }

}

Function NCRA-Remote-Desktop-Prod {
    $NCRA_Remote_Desktop_Prod = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/Remote Desktop Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Remote_Desktop_Prod) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Remote Desktop - PROD"
    }

}

Function NCRA-SQL-PROD  {
    $NCRA_SQL_PROD  = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/SQL Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_SQL_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - SQL - PROD"
    }

}

Function NCRA-Terminal-Servers-PROD {
    $NCRA_Terminal_Servers_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/Terminal Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Terminal_Servers_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Terminal Servers - PROD"
    }

}

Function NCRA-Tidal-PROD {
    $NCRA_Tidal_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/Tidal Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Tidal_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Tidal - PROD"
    }

}

Function NCRA-Verint-PROD {
    $NCRA_Verint_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/Verint Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Verint_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Verint - PROD"
    }

}

Function NCRA-Vistar-Terminal-Servers-PROD {
    $NCRA_Vistar_Terminal_Servers_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/Vistar Terminal Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Vistar_Terminal_Servers_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Vistar Terminal Servers - PROD"
    }

}

Function NCRA-Voice-PROD {
    $NCRA_Voice_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/Voice Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Voice_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Voice - PROD"
    }

}

Function NCRA-Web-PROD {
    $NCRA_Web_PROD = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/Production Servers/Web Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Web_PROD) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Web - PROD"
    }

}

#NCRA QUALITY ASSURANCE SERVERS:
Function NCRA-Application-QA {
    $NCRA_Application_QA = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/QA Servers/Application Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Application_QA) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Application - QA"
    }

}

Function NCRA-BizTalk-QA {
    $NCRA_BizTalk_QA = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/QA Servers/BizTalk Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_BizTalk_QA) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - BizTalk - QA"
    }

}

Function NCRA-Edifecs-QA {
    $NCRA_Edifecs_QA = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/QA Servers/Edifecs Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Edifecs_QA) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Edifecs - QA"
    }

}

Function NCRA-Remote-Desktop-QA {
    $NCRA_Remote_Desktop_QA = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/QA Servers/Remote Desktop Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Remote_Desktop_QA) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Remote Desktop - QA"
    }

}

Function NCRA-SQL-QA {
    $NCRA_SQL_QA = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/QA Servers/Remote Desktop Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_SQL_QA) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - SQL - QA"
    }

}

Function NCRA-Terminal-Servers-QA {
    $NCRA_Terminal_Servers_QA = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/QA Servers/Remote Desktop Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Terminal_Servers_QA) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Terminal Servers - QA"
    }

}

Function NCRA-Tidal-QA {
    $NCRA_Tidal_QA = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/QA Servers/Tidal Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Tidal_QA) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - Tidal - QA"
    }

}

Function NCRA-Web-QA {
    $NCRA_Web_QA = Foreach ($server in $newservers) {
        Get-ADComputer -Identity $server -Properties * |
        Select dnshostname,canonicalname -ExpandProperty canonicalname|
        Where canonicalname -Like "ccx.carecentrix.com/NCRA/Servers/QA Servers/Web Servers/*"|
        Select dnshostname -ExpandProperty dnshostname
            }
    
    Foreach ($server in  $NCRA_Web_QA) {
    Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -NameIncludes $server|Add-WsusComputer -TargetGroupName "NCRA - WEb - QA"
    }

}

#RUN: 
# This function executes the script if there are new/unsorted servers in the Unassigned Computers group on the WSUS server
Function Run {

If ($newservers.Count -gt 0)
	{
		CTHA-Verint-PROD
		KSOP-Verint-PROD
		AZPH-Verint-PROD
		FLT2-Application-PROD
		FLT2-Verint-PROD
		FLT2-Voice-PROD
		NCRA-Application-DEV
		NCRA-BizTalk-DEV
		NCRA-Edifecs-DEV
		NCRA-Remote-Desktop-DEV
		NCRA-SQL-DEV
		NCRA-Terminal-Servers-DEV
		NCRA-Voice-DEV
		NCRA-Web-DEV
		NCRA-Application-PROD
		NCRA-BizTalk-PROD
		NCRA-Edifecs-PROD
		NCRA-Exchange-PROD
		NCRA-Mail-Terminal-Servers-PROD
		NCRA-Remote-Desktop-Prod
		NCRA-SQL-PROD
		NCRA-Terminal-Servers-PROD
		NCRA-Tidal-PROD
		NCRA-Verint-PROD
		NCRA-Vistar-Terminal-Servers-PROD
		NCRA-Voice-PROD
		NCRA-Web-PROD
		NCRA-Application-QA
		NCRA-BizTalk-QA
		NCRA-Edifecs-QA
		NCRA-Remote-Desktop-QA
		NCRA-SQL-QA
		NCRA-Terminal-Servers-QA
		NCRA-Tidal-QA
		NCRA-Web-QA
		
		
	}
	Else {Send-MailMessage -SmtpServer smtp.ccx.carecentrix.com -From WSUS-Sorting-Hat@carecentrix.com -To kevin.trent@carecentrix.com -Subject "No new unassigned computers" 
			-Body "Sorting Hat did not dectect any new servers in the WSUS unassigned computers group. No action was taken."}
}

Run

#REPORTING:
	#Still working on this section. Sudo-code below. 

#Servers Not Registered with WSUS:
	#$wsus = get-wsusserver -name psus002 -portnumber 8530|get-wsuscomputer|select -ExpandProperty Fulldomainname
	#$windows = Get-ADComputer -Properties * -Filter {(OperatingSystem -like "*Windows Server*")}|Select DNSHostName -ExpandProperty DNSHostName
	#$nonreg = Compare-Object $wsus $windows|ForEach-Object {$_.InputObject}

#Servers still in the Unassigned Group
	#$unassed = Get-WsusServer -Name psus002 -PortNumber 8530|Get-WsusComputer -ComputerTargetGroups "Unassigned Computers"|select Fulldomainname -ExpandProperty Fulldomainname

