<#
Copyright (c) 2015, Adam Beardwood
All rights reserved.
 
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: 
 
1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution. 
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#>

#Requires -version 2

<#
.SYNOPSIS
	Cleans up users & groups post-migration
.DESCRIPTION
    
.PARAMETER userlist
	Text file containing user samaccountnames
.EXAMPLE
    C:\>Cleanup-Migration.ps1 -csv "newusers.csv"
.NOTES
    Author: Adam Beardwood
    Date: March 16, 2015
	v1.0: Initial Release
#>

[CmdletBinding()]

param
(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, HelpMessage="Text file with user details")][ValidateScript({Test-Path $_})]$userlist,
[Parameter(Mandatory=$false, Position=1, ValueFromPipeline=$true, HelpMessage="Don't rename homedrive folder")][switch]$norename
)

write-verbose "Import AD Module"
$Env:ADPS_LoadDefaultDrive = 0
if($(Get-Module -Name ActiveDirectory) -eq $null){try{Ipmo ActiveDirectory -ea Stop}catch{write-host "Error! Active Directory Powershell Module is required for this script to function" -foregroundcolor red;exit 1}}

if($env:userdomain -ne "CDS"){write-host "Error: This script must be run using a CDS administrative account";exit 1}

$AECUKCreds = Get-Credential -Message "Please enter AECUK ADM credentials"

$users = gc $userlist

foreach($user in $users){

	$user = Get-ADUser $user -properties homedirectory,memberof,userworkstations
	
	if($user.homedirectory -ne $null){
		write-verbose "Setting homedrive and homedirectory in CDS"
		Set-ADUser $user -Homedirectory "\\controlsdata.com\fs\homes\$($($user.samaccountname).substring(0,1))\$($user.samaccountname)" -HomeDrive "H:" -Server controlsdata.com 
		
		write-verbose "Setting homedrive and homedirectory in AECUK"
		Set-ADUser $user.samaccountname -Homedirectory "\\controlsdata.com\fs\homes\$($($user.samaccountname).substring(0,1))\$($user.samaccountname)" -HomeDrive "H:" -Server aecuk.aec.local -Credential $AECUKCreds
	}

	write-verbose "Remove AECUK Account from RSA_Ignore Group"
	If($user.memberof -match "RSA_Ignore"){
		Remove-ADGroupMember -Identity "RSA_Ignore" -Members $user.samaccountname -Server aecuk.aec.local -Credential $AECUKCreds -Confirm:$False
	}
	
	write-verbose "Add CDS Account to MIGRATION_Login Cleanup group"
	Add-ADGroupMember -Identity "MIGRATION_Login Cleanup" -Members $user -Server controlsdata.com -Confirm:$False
	
	write-verbose "Add CDS account to Wireless Users Group"
	Add-ADGroupMember -Identity "Wireless Users" -Members $user -Server controlsdata.com -Confirm:$False
	
	write-verbose "Add AECUK Account to 'migrated' group"
	Add-ADGroupMember -Identity "MIGRATION_Migrated Users" -Members $user.samaccountname -Server aecuk.aec.local -Credential $AECUKCreds -Confirm:$False
	
	write-verbose "Remove CDS account from GR_Domain Users where applicable"
	If($user.memberof -match "GR_Domain Users"){
		Remove-ADGroupMember -Identity "GR_Domain Users" -Members $user -Server controlsdata.com -Confirm:$False
	}
	
	#ITSD Groups
	write-verbose "Remove AECUK Account from Infra Customers General"
	If($user.memberof -match "Infra Customers General"){
		Remove-ADGroupMember -Identity "Infra Customers General" -Members $user.samaccountname -Server aecuk.aec.local -Credential $AECUKCreds -Confirm:$False
	}

	write-verbose "Add CDS Account to ITSD_Customers group"
	Add-ADGroupMember -Identity "ITSD_Customers" -Members $user -Server controlsdata.com -Confirm:$False

	write-verbose "Remove AECUK account from Infra Officers & add CDS account to ITSD_Officers"
	If($user.memberof -match "Infra Officers"){
		Remove-ADGroupMember -Identity "Infra Officers" -Members $user.samaccountname -Server aecuk.aec.local -Credential $AECUKCreds -Confirm:$False
		Add-ADGroupMember -Identity "ITSD_Officers" -Members $user -Server controlsdata.com -Confirm:$False
	}
	
	write-verbose "Remove AECUK account from Infra Admins & add CDS account to ITSD_Admins"
	If($user.memberof -match "Infra Admins"){
		Remove-ADGroupMember -Identity "ITSD_Admins" -Members $user.samaccountname -Server aecuk.aec.local -Credential $AECUKCreds -Confirm:$False
	}
	
	write-verbose "Remove AECUK account from Infra Officer Managers & add CDS account to ITSD_Managers"
	If($user.memberof -match "Infra Officer Managers"){
		Remove-ADGroupMember -Identity "ITSD_Managers" -Members $user.samaccountname -Server aecuk.aec.local -Credential $AECUKCreds -Confirm:$False
	}
	
	write-verbose "Remove logon restrictions for Integrity users"
	If($user.userworkstations -match "BHM1DC01"){
		Set-ADUser $user -Logonworkstations $null
	}
	
	if(!$norename){
		write-verbose "Rename old homedrive folder"
		Rename-Item	"\\bhx1sv-dm1\wa1a_cif0\home\$($user.samaccountname.tolower())" "zz_MIGRATED.$($user.samaccountname.tolower())" -force
	}
}

