|
# Get login of an account administrator [account with mailbox] |
|
# Assign that login to $admin variable |
|
Param ([string]$admin, [string[]]$arrName) |
|
# Important: Please use your login name associated wiht your outlook. |
|
# You must have Outlook installed in order to run this script. |
|
# If you use full name make sure you enter it with quotes. |
|
|
|
# Example: |
|
# teminate.ps1 ashatnyy "User1 Name" |
|
# or |
|
# teminate.ps1 ashatnyy "User1 Name", user2login, [email protected], "User4 Name" |
|
# |
|
# |
|
|
|
|
|
# Array of names to be checked and moved |
|
# $arrName = @("Intel Events") |
|
# Location of the archive |
|
[string]$localPath = "C:\PSTFiles\" # <<--- replase this with your local storage of PST and file archives |
|
# all possible paths for home data and email backup |
|
$paths = @("\\somehost\folder\home", "\\somehost\otherfolder\data"); # <-- user's data location put as many as you want, script will itterate through |
|
|
|
#Move folders if exists |
|
Function MoveFolder ($userName, $remotePath){ |
|
# concat target folder name for each user |
|
[string]$target = $localPath + $userName; |
|
|
|
#get different names depending on what location it comes from |
|
$remotePathArray = $remotePath.split("\"); |
|
|
|
#create folder inside local storage |
|
|
|
if ((Test-Path $target) -and (Test-Path $remotePath)){ |
|
#Copy remote folder to renamed folder inside NAME |
|
$localBackup = $target+"\"+$remotePathArray[-2] |
|
write-host "Copying" $remotePath "---->>>>" $localBackup |
|
Copy-Item -Recurse $remotePath $localBackup; |
|
Write-Host "Removing" $remotePath; |
|
Remove-Item -Recurse $remotePath; |
|
} |
|
} |
|
Function buildPathArray($userPaths, $uName) { |
|
# takes username and array of path and returns concatanated array |
|
# create empty array every time function called |
|
$pathsArray = @(); |
|
# iterrate throught the Paths |
|
foreach($path in $userPaths){ |
|
# concatenate path with username |
|
$userFolder = $path + $uName; |
|
# push new element [ user path ] to an array |
|
$pathsArray += $userFolder; |
|
} |
|
return $pathsArray; |
|
} |
|
Function CreateFolder($alias){ |
|
$target = $localPath+$alias; |
|
if (!(Test-Path $target)){ |
|
Write-Host "Creating the folder for user data in" $target; |
|
[IO.Directory]::CreateDirectory($target); |
|
} else { |
|
Write-Host "Folder" $target "in on place, no need to create!" |
|
} |
|
} |
|
|
|
Function ExportMailbox($alias, $email){ |
|
# Mailbox export part |
|
$exPath = $localPath+$alias+"\"+$alias+".pst" |
|
Add-MailboxPermission -Identity $alias -User $admin -AccessRights FullAccess |
|
Write-Host "Found a user "$name" with email: "$email"." |
|
Write-Host "Will export pst to "$exPath |
|
Export-Mailbox -Identity $alias -PSTFolderPath $exPath |
|
} |
|
|
|
# main body |
|
# you can write your own validation if maibox actually exist |
|
# I was lazy sorry |
|
|
|
# Check if user to remove and outlok user were presented |
|
if ($arrName -and $admin){ |
|
foreach($name in $arrName ) { |
|
$mBx = Get-Mailbox $name | Select-Object alias, *smtp* |
|
if(($mBx.alias) -and ($mBx.PrimarySmtpAddress)) { |
|
$alias = $mBx.alias |
|
$email = $mBx.PrimarySmtpAddress |
|
# Before move Folder must exist |
|
|
|
CreateFolder $alias; |
|
ExportMailbox $alias $email; |
|
|
|
#Progress bar initialize |
|
$i = 0; |
|
Write-Host "********************" |
|
|
|
Write-Host "Starting with" $name |
|
# build an array of new paths with concatenated names to it |
|
# assigh that array to a var |
|
$pathsArray = buildPathArray $paths $alias; |
|
# itterate through the array of path and check folder by folder |
|
foreach ($folder in $pathsArray){ |
|
# Progress bar |
|
$i++; |
|
$processing = "Processing "+ $name |
|
Write-Progress -activity $processing -status "Forlders moved: " -PercentComplete (($i / $pathsArray.length) * 100) |
|
#end of progress bar |
|
|
|
# check if folder exists |
|
if (Test-Path $folder){ |
|
# move exisiting folders to a local storage |
|
Write-Host "User:" $name "has home folder:" $folder; |
|
MoveFolder $alias $folder; |
|
} else { |
|
Write-Host "Can't find" $folder "for" $name"."; |
|
} |
|
} |
|
|
|
} |
|
} |
|
Write-Host " " |
|
Write-Host "*************** Done ******************" |
|
Write-Host "Please remove Exchange and AD accounts manually" |
|
Write-Host "Make sure Voicemail and Telephone number are repurposed" |
|
Write-Host " " |
|
} else { |
|
Write-Host "How-TO" |
|
Write-Host "*******************************************************************" |
|
Write-Host "Important: Please use your login name associated wiht your outlook." |
|
Write-Host "You must have Outlook installed in order to run this script." |
|
Write-Host "If you use full name make sure you enter it with quotes." |
|
Write-Host "Example:" |
|
Write-Host "teminate.ps1 ashatnyy "User1 Name"" |
|
Write-Host "or" |
|
Write-Host "teminate.ps1 ashatnyy "User1 Name", user2login, [email protected], "User4 Name"" |
|
} |