Skip to content

Instantly share code, notes, and snippets.

@General-Gouda
Created July 31, 2017 16:10
Show Gist options
  • Save General-Gouda/f9d1ff3b27c241123187a31167c3d8eb to your computer and use it in GitHub Desktop.
Save General-Gouda/f9d1ff3b27c241123187a31167c3d8eb to your computer and use it in GitHub Desktop.
Individual PST Upload Script
function AzCopy_PSTs {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$Environment,
[Parameter(Mandatory=$false)]
[string]$IndividualUserName,
[Parameter(Mandatory=$true)]
[string]$AdminEmailAddress,
[Parameter(Mandatory=$false)]
[switch]$NoCopy,
[Parameter(Mandatory=$false)]
[switch]$NoFile
)
Function Get-FileMetaData {
#Requires -Version 2.0
Param (
[string[]]$folder
)
foreach($sFolder in $folder) {
$a = 0
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.namespace($sFolder)
foreach ($File in $objFolder.items()) {
$FileMetaData = New-Object PSOBJECT
for ($a ; $a -le 266; $a++)
{
if($objFolder.getDetailsOf($File, $a))
{
$hash += @{$($objFolder.getDetailsOf($objFolder.items, $a)) = $($objFolder.getDetailsOf($File, $a))
}
$FileMetaData | Add-Member $hash
$hash.clear()
}
}
$a=0
$FileMetaData
}
}
}
Function Get-FileOwner {
param (
[string]$File,
[string]$Domain
)
$filePermissions = Get-Acl $File
if ($filePermissions.Owner -like "*$Domain*") {
$owner = $filePermissions.Owner.Replace("$domain\","")
} else {
$accesses = $filePermissions.Access.IdentityReference.Value
$accesses | ForEach-Object {
$access = $_
if ($access -like "*$Domain*" -and $access -notlike "domain\Domain Admins" -and $access -notlike "domain\admin" -and $access -notlike "domain\AnotherAdmin") {
$owner = $access
}
}
}
if (![string]::IsNullOrWhiteSpace($owner)) {
[PSCustomObject]@{
"FileLocation" = $File
"FileOwner" = $owner
}
} else {
[PSCustomObject]@{
"FileLocation" = $File
"FileOwner" = "Owner not found"
}
}
}
function New-InputBox {
[CmdletBinding()]
param (
[string]$TextBoxTitle,
[string]$TextBoxMessage,
[int]$MaxLength = 100
)
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = $TextBoxTitle
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = 'CenterScreen'
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq 'Enter')
{$output=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq 'Escape')
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(100,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.Add_Click({$output=$objTextBox.Text;$objForm.Close()})
$OKButton.tabindex = 1
$objForm.Controls.Add($OKButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = $TextBoxMessage
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(250,20)
$objTextBox.MaxLength = $MaxLength
$objTextBox.tabindex = 0
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
return $objTextBox.Text
}
$currentLocation = (Get-Location).path
Set-Location 'C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy'
if ($Environment -eq "Production") {
$secureURL = "GET THIS FROM OFFICE 365"
$fileServer = "fileServerNameGoesHere"
$PSTLocation = "FolderLocationGoesHere"
$domain = "domain"
$emailDomain = "@domain.com"
} elseif ($Environment -eq "Test") {
$secureURL = "GET THIS FROM OFFICE 365"
$fileServer = "testFileServerNameGoesHere"
$PSTLocation = "FolderLocationGoesHere"
$domain = "domainTEST"
$emailDomain = "@domaintest.com"
}
if ([string]::IsNullOrWhiteSpace($IndividualUserName)) {
$IndividualUsername = New-InputBox -TextBoxTitle "Input Username" -TextBoxMessage "Please input the PST owner's username."
}
$CSVFileName = "{0}PSTList" -f $IndividualUsername
$PSTLocation = "\\{0}\D$\" -f $fileServer
$PSTInfo = Get-ChildItem $PSTLocation -File -Include *.pst -Recurse
$directoryInfo = $PSTInfo.DirectoryName | Get-Unique
if (($PSTInfo).count -gt 0)
{
if (Test-Path C:\PSTUpload\$CSVFileName.csv)
{
Remove-Item C:\PSTUpload\$CSVFileName.csv
}
$directoryInfo | ForEach-Object {
$directoryFullName = $_
$directoryFriendlyFolderName = $directoryFullName.Replace("\","/").Replace("//","").Replace(" ","")
$destination = "{0}" -f $secureURL,$directoryFriendlyFolderName
Get-FileMetaData -folder $directoryFullName | Where-Object {$_."file extension" -eq ".pst"} | ForEach-Object {
$fileFullPath = $_."Folder Path" + "\" + $_.name
$fileName = $_.Name
if ($fileName.Contains(" ")) {
$fileName = $fileName.replace(" ","").replace("'","")
Rename-Item -Path $fileFullPath -NewName $fileName
$fileFullPath = $_."Folder Path" + "\" + $fileName
}
$fileInfo = [PSCustomObject]@{
"Workload" = "Exchange"
"FilePath" = ""
"Name" = $fileName
"Mailbox" = "{0}{1}" -f $IndividualUsername.Replace("$domain\",""), $emailDomain
"IsArchive" = "FALSE"
"TargetRootFolder" = "{0}_{1}" -f ($IndividualUsername.Replace("$domain\","")), $fileName.replace(".pst","")
"SPFileContainer" = ""
"SPManifestContainer" = ""
"SPSiteURL" = ""
}
if ($NoFile -eq $false)
{
$fileInfo | Export-Csv C:\PSTUpload\$CSVFileName.csv -NoTypeInformation -Append
}
else
{
Write-Host "NoFile switch is true."
}
}
if ($NoCopy -eq $false)
{
Write-Host "Uploading PSTs in directory $directoryFullName to $destination" -ForegroundColor Cyan
.\AzCopy.exe /Source:$directoryFullName /Dest:$destination /V:C:\PSTUpload\Uploadlog.log /Z:C:\PSTUpload\JournalFiles /Y /Pattern:*.pst
}
else
{
Write-Host "NoCopy switch is true..."
}
}
}
Set-Location $currentLocation
if ($NoCopy -eq $false)
{
Send-MailMessage -From "[email protected]" -To $AdminEmailAddress -Body "PST Upload complete for $IndividualUserName." -Subject "PST Upload Complete!" -SmtpServer mailrelay.domain.com
}
}
AzCopy_PSTs -Environment "Production" -IndividualUserName 'ediscovery' -AdminEmailAddress "[email protected]" #-NoCopy -NoFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment