Change forwarding address using csv file

Welcome Forums General PowerShell Q&A Change forwarding address using csv file

Viewing 1 reply thread
  • Author
    Posts
    • #12911
      Participant
      Topics: 1
      Replies: 1
      Points: 0
      Rank: Member

      Function set-ForwardAddresses
      {
      <#
      .Synopsis
      Changes Forward addresses of mailboxes using detailed from .csv file
      .Description
      The cmdlet uses set-mailbox -ForwardingAdrress to process to modify multiple accounts at once.

      #>
      [CmdletBinding()]
      Param(
      #csv File path and name details
      [parameter(Mandatory=$true,
      Helpmessage="Enter the path of the .csv that holds the USerID and ForwardAddress Information")]
      [Alias('InputFile','FilePath')]
      $FromFile
      )

      # Change SMTP addresses of users to new SMTP addresses defined in inputted csv file and delete the old smpt address
      begin
      {
      $users = Import-CSV -path $fromFile -Header("USERID","ForwardAddress")
      ide

      }
      Process
      {
      $users | foreach-object {
      $ID += $_.UserID
      $ForwardAddress += $_.ForwardAddress

      set-mailbox -identity $ID -ForwardingAddress $ForwardAddress -DeliverToMailboxAndForward $false
      write-host User $ID had forwarding address $ForwardAddress added to their mailbox
      }#close for-lopo
      }
      End
      {
      Write-Output = 'The process is finished'
      }

      }#close function

      I have created a function and added it to my module, imported it.

      [PS] C:\scripts>set-ForwardAddresses -FromFile ‘C:\Users\AWAdmin\Documents\WindowsPowerShell\email mailboxes\EmailForwards.csv’

      add-pssnapin : Cannot add Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.E2010 because it is alrea
      dy added. Verify the name of the snap-in and try again.
      At C:\users\awadmin\Documents\WindowsPowerShell\Modules\CustomAWEmailFunctions\CustomAWEmailFunctions.psm1:23 char:21
      + add-pssnapin <<<< Microsoft.Exchange.Management.PowerShell.E2010
      + CategoryInfo : InvalidArgument: (Microsoft.Excha…owerShell.E2010:String) [Add-PSSnapin], PSArgumentEx
      ception
      + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

      Cannot process argument transformation on parameter ‘Identity’. Cannot convert the “System.Collections.ArrayList” value
      of type “System.Collections.ArrayList” to type “Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter”.
      + CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindin…mationException
      + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
      + PSComputerName : hww-cas01.hogarthww.prv

      Is there any way I can fix this please?

      Thanks

      Alex

    • #12912
      Participant
      Topics: 1
      Replies: 1
      Points: 0
      Rank: Member

      Function set-ForwardAddresses
      {
      <#
      .Synopsis
      Changes Forward addresses of mailboxes using detailed from .csv file
      .Description
      The cmdlet uses set-mailbox -ForwardingAdrress to process to modify multiple accounts at once.

      #>
      [CmdletBinding()]
      Param(
      #csv File path and name details
      [parameter(Mandatory=$true,
      Helpmessage="Enter the path of the .csv that holds the USerID and ForwardAddress Information")]
      [Alias('InputFile','FilePath')]
      $FromFile
      )

      # Change SMTP addresses of users to new SMTP addresses defined in inputted csv file and delete the old smpt address
      begin
      {
      $users = Import-CSV -path $FromFile -Header("USERID","ForwardAddress")

      }
      Process
      {
      $users | foreach-object {

      set-mailbox -identity $_.UserID -ForwardingAddress $_.ForwardAddress -DeliverToMailboxAndForward $false
      write-host User $_.UserID had forwarding address $_.ForwardAddress added to their mailbox
      }#close for-lopo
      }
      End
      {
      Write-Output = 'The process is finished'
      }

      }#close function

      This worked using $_. in the cmdlet directly instead of via a variable

Viewing 1 reply thread
  • The topic ‘Change forwarding address using csv file’ is closed to new replies.