Slackbot
03/28/2022, 7:33 PMOsiris Amanpour
03/28/2022, 8:58 PMCraig Anderson
03/28/2022, 9:00 PMDennis Parker
03/28/2022, 9:14 PMfunction ConvertFrom-Hexa($hexstring)
{
($hexstring.Split(",",[System.StringSplitOptions]::RemoveEmptyEntries) | ?{$_ -gt '0'} | ForEach{[char][int]"$($_)"}) -join ''
}
Function ConvertTo-Hexa($str)
{
$ans = ''
`[System.Text.Encoding]::Unicode.GetBytes($str + "`0")`
}
add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms
$CompanyKey = "HKCU:\Software\CompanyName"
$CompanyName = "OutlookMigration"
$hasbeenrun = (Get-ItemProperty -path $CompanyKey -name $CompanyName -ErrorAction SilentlyContinue).OutlookMigration
if($hasbeenrun) {
Write-Host "Already been run --- exiting."
break
}
#Set the been run registry entry (could do this later in the script, but this seems safe.)
if(!(Test-Path -path $CompanyKey)) {
New-Item -Path $CompanyKey -Force | Out-Null
New-ItemProperty -Path $CompanyKey -Name $CompanyName -Value $true -PropertyType DWORD -Force | Out-Null
write-host "Setting registry key..."
} else {
New-ItemProperty -Path $CompanyKey -Name $CompanyName -Value $true -PropertyType DWORD -Force | Out-Null
write-host "Setting registry key..."
}
Start-Sleep -Seconds 60
$searcher = [adsisearcher]"(samaccountname=$env:USERNAME)"
$OLusername = $searcher.FindOne().Properties.mail
#$ADUserKey = "HKCU:\Software\Microsoft\Office\15.0\Common\Identity"
#$ADUserName = "ADUserName"
#$OLusername = (Get-ItemProperty -path $ADUserKey -name $ADUserName -ErrorAction SilentlyContinue).ADUserName
if(!($OLusername)) {
$OLusername = ($env:username).Replace("_","@")
}
$process = get-process | where { $_.mainWindowTitle -like "*$OLusername*" -and $_.ProcessName -like "*outlook*" }
$OutlookRunning = $false
if($process) {
$Notepad = Start-Process Notepad -PassThru
start-sleep -Milliseconds 500
[Microsoft.VisualBasic.Interaction]::AppActivate("Notepad")
`[System.Windows.Forms.SendKeys]::SendWait("Please wait while your Outlook OST file is migrated. Outlook has been closed for this process and will reopen when complete.`r`rPlease wait...this may take a few minutes....")`
$OutlookRunning = $true
$process | Stop-Process -Force
Start-Sleep -Seconds 2
} else {
$Notepad = Start-Process Notepad -PassThru
start-sleep -Milliseconds 500
[Microsoft.VisualBasic.Interaction]::AppActivate("Notepad")
`[System.Windows.Forms.SendKeys]::SendWait("Please wait while your Outlook OST file is migrated. Please do not attempt to open Outlook during this process.`r`rPlease wait...this may take a few minutes....")`
Start-Sleep -Seconds 2
}
$newBasePath = "<<<NEW PATH TO OST>>>"
#### $newBasePath = "C:\Users\ptitestaccount_sieg\AppData\Local\Microsoft\Outlook" #For testing only
$username = [Environment]::UserName
#find key that has the 001f6610 property that holds the OST file path - one key per outlook profile.
$name = '001f6610'
$keys = @( (Get-ChildItem "HKCU:\Software\Microsoft\Office\16.0\Outlook\Profiles\" -Recurse | Where-Object {$_.Property -eq $name}).name )
$keys
foreach($key in $keys)
{
write-host $key -foregroundcolor green
$key = $key.Replace("HKEY_CURRENT_USER\","HKCU:\")
write-host $key -foregroundcolor green
$value = (get-itemproperty -path $key -name $name).$name
$value2 = (Get-ItemProperty -path $key | Select -ExpandProperty $name) -join ','
$oldValue = ConvertFrom-Hexa $value2
write-host "Old Value for OST File was: $oldValue" -foregroundcolor Yellow
#make sure it is an OST in this field
if($oldValue.substring($oldValue.length - 4,4) -eq ".ost" -and $oldValue.substring(0,1) -ne "U")
{
$oldFilenameParts = $oldValue.split("\")
$oldFilename = $oldFilenameParts[$oldFilenameParts.Count - 1]
$new = "$newBasePath\$oldFilename"
$newBin = ConvertTo-Hexa($new)
$newBin2 = $newBin -join ','
$newBin2Str = ConvertFrom-Hexa $newBin2
write-host "New Value for OST file (encoded and decoded) is: $newBin2Str" -foregroundcolor Yellow
If($oldValue -ne $new) {
write-host "About to change value in registry..." -foregroundcolor RED
set-ItemProperty -path $key -name $name -value $newBin
Move-Item -Path $oldValue -Destination $newBasePath -Force
}
}
}
#if($OutlookRunning) {
Start-Sleep -Seconds 1
$Notepad | Stop-Process -Force
start-process 'C:\Program Files (x86)\Microsoft Office\Office16\OUTLOOK.EXE'
#}Dennis Parker
03/28/2022, 9:15 PMCraig Anderson
03/28/2022, 9:16 PMCraig Anderson
03/28/2022, 9:49 PMDennis Parker
03/28/2022, 10:12 PM