function Archive-MWUser {
<#
.Synopsis
Clean up an user from Active directory and Exchange
.DESCRIPTION
Script to process the users documents and email. Archives them to a location on the network and disables the users network account.
.PARAMETER MWUsername
Login name from the company user that will be archived.
.EXAMPLE
Archive-MWUser -MWUserName avanboerum
#>
[CmdletBinding()]
Param(
[Parameter(
Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='What username would you like to target?')]
$MWUsername
)
Begin {
$archivefolder = "\\TARGETSERVER\Archief`$\$MWUsername"
$sourcehomefolder = "\\SOURCESERVER\h`$\users\$MWUsername"
$sourceprofilefolder = "\\SOURCESERVER\h`$\profiles\$MWUsername.v2"
$DisabledOU = "OU=Disabled Users,OU=Company,DC=company,DC=NL"
$ExchSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://CASSERVER/PowerShell/ -Authentication Kerberos
Import-PSSession $ExchSession
}
Process {
#Test if Users Archive folder already exists
$folderexists = Test-Path $archivefolder
if ($folderexists -eq $true)
{Read-Host "Folder $folder is already present. Press any key to continue or ctrl-c to quit"}
else {New-Item $archivefolder -ItemType directory}
#Saves user information
Get-ADUser $MWUsername -Properties memberof | Select-Object -ExpandProperty memberof | Out-File $archivefolder\memberships.txt
Get-ADUser $MWUsername -Properties * | Out-File $archivefolder\UserFullInfo.txt
#Archive users Exchange mailbox and archive
New-MailboxExportRequest -Mailbox $MWUsername -FilePath "$archivefolder\$MWUsername.pst" ;while ((Get-MailboxExportRequest -mailbox $MWUsername | ? {$_.Status -eq “Queued” -or $_.Status -eq “InProgress”})) { sleep 15 }
New-MailboxExportRequest -Mailbox $MWUsername -FilePath "$archivefolder\$MWUsername-archive.pst" -isarchive ;while ((Get-MailboxExportRequest -mailbox $MWUsername | ? {$_.Status -eq “Queued” -or $_.Status -eq “InProgress”})) { sleep 15 }
Disable-Mailbox -Identity $MWUsername -Confirm
#Move homefolder to archive location
$folderexists = Test-Path $archivefolder\home
if ($folderexists -eq $true) {Write-Host "Folder $archivefolder\home is present"}
else {New-Item $archivefolder\home -ItemType directory}
Move-Item $sourcehomefolder -Destination $archivefolder\home -Force
#Move profilefolder to archive location
$folderexists = Test-Path $archivefolder\profile
if ($folderexists -eq $true) {Write-Host 'Folder $archivefolder\profile is present'}
else {New-Item $archivefolder\profile -ItemType directory}
Move-Item $sourceprofilefolder -Destination $archivefolder\profile -Force
#Cleanup group membership. Disables the users account for logon.
Disable-ADAccount -Identity $MWUsername -Confirm
Get-ADUser $MWUsername | Move-ADObject -TargetPath $DisabledOU
#tricky command, must test before use..... get-adgroup -Filter * | Remove-ADGroupMember -Members $mwusername
}
}