Need to change folder homepages in outlook

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 


August 7th, 2015 7:52pm

Satajit321,

Thank your for your help and response.  I made the changes as I understood from your reply but I think I have some more work to do.  The new script is below.  In an effort to help un-complicate things, can you please help me remove all the extraneous stuff that is not need - if it does not serve the purpose to help in updating the URL.  I have posted error messages below the script that are adding to my confusion.  Thanks

___________________________

 

[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 "Folder $($newFoldername) already exists for " $email -foregroundcolor $info 
   $newfolder = $SearchResults.folders[0]
 }

# 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://newURL"
}

#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 

_____________________________________

Found  mailboxes...
You cannot call a method on a null-valued expression.
At C:\Program Files\Microsoft\Exchange Server\V14\Scripts\Set-OutlookFoldersMimecast.ps1:82 char:46
+ $email = $mailbox.primarysmtpaddress.tostring <<<< ()
    + CategoryInfo          : InvalidOperation: (tostring:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling "Bind" with "2" argument(s): "The Id property must be set."
At C:\Program Files\Microsoft\Exchange Server\V14\Scripts\Set-OutlookFoldersMimecast.ps1:86 char:65
+ $RootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind <<<< ($Service, $RootFolderID)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Exception calling "FindFolders" with "3" argument(s): "The Id property must be set."
At C:\Program Files\Microsoft\Exchange Server\V14\Scripts\Set-OutlookFoldersMimecast.ps1:10 char:38
+ $SearchResults = $service.FindFolders <<<< ($RootFolderID, $SearchFilter, $FolderView)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Folder ##   Email Archive Search   ## already exists for 
Cannot index into a null array.
At C:\Program Files\Microsoft\Exchange Server\V14\Scripts\Set-OutlookFoldersMimecast.ps1:15 char:38
+  $newfolder = $SearchResults.folders[ <<<< 0]
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Folder ##   Email Archive Search   ## already exists for 
Cannot index into a null array.
At C:\Program Files\Microsoft\Exchange Server\V14\Scripts\Set-OutlookFoldersMimecast.ps1:20 char:40
+    $newfolder = $SearchResults.folders[ <<<< 0]
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Setting homepage URL for ##   Email Archive Search   ##
You cannot call a method on a null-valued expression.
At C:\Program Files\Microsoft\Exchange Server\V14\Scripts\Set-OutlookFoldersMimecast.ps1:50 char:31
+ $NewFolder.SetExtendedProperty <<<< ($PR_FOLDER_WEBVIEWINFO, $bytes)
    + CategoryInfo          : InvalidOperation: (SetExtendedProperty:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Program Files\Microsoft\Exchange Server\V14\Scripts\Set-OutlookFoldersMimecast.ps1:53 char:18
+ $NewFolder.update <<<< () 
    + CategoryInfo          : InvalidOperation: (update:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Free Windows Admin Tool Kit Click here and download it now
August 11th, 2015 8:55am

Satyaijt321,

Thank your for your help.  Got the script working but had give my account the right to impersonate. That was a dealbreaker.  I appreciate your response.

https://msdn.microsoft.com/en-us/library/office/dn722376(v=exchg.150).aspx


  • Marked as answer by wspinatl 13 hours 41 minutes ago
August 11th, 2015 1:48pm

Satyaijt321,

Thank your for your help.  Got the script working but had give my account the right to impersonate. That was a dealbreaker.  I appreciate your response.

https://msdn.microsoft.com/en-us/library/office/dn722376(v=exchg.150).aspx


  • Marked as answer by wspinatl Tuesday, August 11, 2015 5:44 PM
Free Windows Admin Tool Kit Click here and download it now
August 11th, 2015 5:43pm

Hi Wspinatl,

Read the script using ISE PowerShell, it will be more clearer.

Change this part(line62) to update the url

$htNewOutlookFolders = @{
     "##   Email Archive Search   ##" = "https://new_URL1";
     "##   Support Center   ##"  = "https://new_URL2"
 }

Note this part(line14) is commented: Hence if folder exists it will not be created or updated.

# If $newFolderName found, say so.  
 # if ($SearchResults.TotalCount -ne 0) {
 #  Write-host "Folder $($newFoldername) already exists for " $email -foregroundcolor $info 
 #  $newfolder = $SearchResults.folders[0]
 #}


#----------------------Uncomment to use this

 if ($SearchResults.TotalCount -ne 0) {
 Write-host "Folder $($newFoldername) already exists for " $email -foregroundcolor $info 
   $newfolder = $SearchResults.folders[0]
 }

More important, this script makes changes to large no. of mailboxes, to test and limit it uncomment

Line74

#get mailboxes
 #$mailboxes = get-mailbox -filter {office -ne $null} 
 $mailboxes = get-mailbox byrdt<testmailboxname>		
August 12th, 2015 1:42am

Hi,

First of all you should not be running scripts written by someone else, which you are not able to understand what it does. PowerShell is powerfull enough to messup your environment.

Secondly you don't seem to be making the suggested changes on the url names or using single test mailbox.

$mailboxes = get-mailbox byrdt

Best would be figuring out first what this script does and how it works, or getting some alternate solution to get things going.

You can start by dividing the script into individual commands. Learn and test what does

Free Windows Admin Tool Kit Click here and download it now
August 12th, 2015 2:33am

Hi wspinatl,

Good finding. Thanks for coming back and updating us.

August 12th, 2015 8:45am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics