A previous administrator wrote this script. It created the folders in outlook but now I need to update the URL's and I cannot figure out how to do it, whether I need a new script or change some thing in this one. Please advise. i am not a script-or.
[string]$info = "White" # Color for informational messages[string]$warning = "Yellow" # Color for warning messages
[string]$error = "Red" # Color for error messages
[string]$LogFile = "C:\Temp\Set-NewOutlookFoldersEWSLoop_Log_$(get-date -uformat "%Y%m%d%H%M%S").txt" # Path of the Log File
function CreateFolder($newFolderName, $newURL)
{
#Connect to users MsgFolderRoot and search for $newFolderName
$SearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName, $newFolderName)
$SearchResults = $service.FindFolders($RootFolderID, $SearchFilter, $FolderView)
# If $newFolderName found, say so.
# if ($SearchResults.TotalCount -ne 0) {
# Write-host "Folder $($newFoldername) already exists for " $email -foregroundcolor $info
# $newfolder = $SearchResults.folders[0]
#}
# If $newFolderName not found create it
if ($SearchResults.TotalCount -ne 0) {
Write-host $email ": Folder $($newFolderName) not found... Creating" -foregroundcolor $warning
#Create the folder
$NewFolder = new-object Microsoft.Exchange.WebServices.Data.Folder($service)
$NewFolder.DisplayName = $newFolderName
$NewFolder.Save($RootFolderID)
Write-host "Created for" $email -foregroundcolor $info
}
# Set homepage URL of folder
Write-host "Setting homepage URL for" $newfoldername -foregroundcolor $info
# Generate HEX of URL
$CharArray = $newUrl.ToCharArray();
$homepagebytes = $null
foreach ($Char in $CharArray) {
$homepagebytes = $homepagebytes + [System.String]::Format("{0:X}", [System.Convert]::ToUInt32($Char)) + "00"
}
# String together the value for PR_FOLDER_WEBVIEWINFO
$dwversion = "02"
$dwType = "00000001"
$dwFlags = "00000001"
$dwUnused = "00000000000000000000000000000000000000000000000000000000"
$cbDataSizePadding = "000000"
$cbDataSize = (($homepagebytes.length / 2) + 2).ToString("X")
$cbDataSizePadding2 = "000000"
$homepagebytesPadding = "0000"
$HexURL = $dwversion + $dwType + $dwFlags + $dwUnused + $cbDataSizePadding + $cbDataSize + $cbDataSizePadding2 + $homepagebytes + $homepagebytesPadding
# Convert $HexURL to a Bytearray then Base64
$bytes2 = @($HexURL -split '([a-f0-9]{2})' | foreach-object { if ($_) {[System.Convert]::ToByte($_, 16)}})
$Base64URL = [System.Convert]::ToBase64String($bytes2);
$bytes = [System.Convert]::FromBase64String($Base64URL);
$NewFolder.SetExtendedProperty($PR_FOLDER_WEBVIEWINFO, $bytes)
# Save
$NewFolder.update()
}
$htNewOutlookFolders = @{
"## Email Archive Search ##" = "https://new_URL1";
"## Support Center ##" = "https://new_URL2"
}
#Import Modules
Import-Module -Name "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll"
$PR_FOLDER_WEBVIEWINFO = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(14047, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
#get mailboxes
$mailboxes = get-mailbox -filter {office -ne $null}
#mailboxes = get-mailbox byrdt
"Found {0} mailboxes..." -f $mailboxes.count
foreach ($mailbox in $mailboxes) {
if ($mailbox.exchangeversion.exchangebuild.major -eq 8) {
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$service.Url = new-object Uri("https://webmail.company.com/ews/exchange.asmx")
}
else {
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
$service.Url = new-object Uri("https://webmail.company.com/ews/exchange.asmx")
}
#Set the Credentials to logged in user
$service.UseDefaultCredentials = $true
$email = $mailbox.primarysmtpaddress.tostring()
$ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $email)
$service.ImpersonatedUserId = $ImpersonatedUserId
$RootFolderID = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot, $email)
$RootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service, $RootFolderID)
$FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1)
$htNewOutlookFolders.getenumerator() | % {
CreateFolder $_.key $_.value
}
}
Write-host "Finished" -foregroundcolor $info