SHARE
TWEET

Untitled

a guest Feb 17th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.  
  3. .DESCRIPTION
  4.     This script will move mailboxes from a mailbox project to a target project
  5.    
  6. .NOTES
  7.     Author          Antonio Vargas
  8.     Date            Jan/2019
  9.     Disclaimer:     This script is provided 'AS IS'. No warrantee is provided either expressed or implied.
  10.     Version: 1.1
  11. #>
  12.  
  13. ### Function to create the working and log directories
  14. Function Create-Working-Directory {    
  15.     param
  16.     (
  17.         [CmdletBinding()]
  18.         [parameter(Mandatory=$true)] [string]$workingDir,
  19.         [parameter(Mandatory=$true)] [string]$logDir
  20.     )
  21.     if ( !(Test-Path -Path $workingDir)) {
  22.         try {
  23.             $suppressOutput = New-Item -ItemType Directory -Path $workingDir -Force -ErrorAction Stop
  24.             $msg = "SUCCESS: Folder '$($workingDir)' for CSV files has been created."
  25.             Write-Host -ForegroundColor Green $msg
  26.         }
  27.         catch {
  28.             $msg = "ERROR: Failed to create '$workingDir'. Script will abort."
  29.             Write-Host -ForegroundColor Red $msg
  30.             Exit
  31.         }
  32.     }
  33.     if ( !(Test-Path -Path $logDir)) {
  34.         try {
  35.             $suppressOutput = New-Item -ItemType Directory -Path $logDir -Force -ErrorAction Stop      
  36.  
  37.             $msg = "SUCCESS: Folder '$($logDir)' for log files has been created."
  38.             Write-Host -ForegroundColor Green $msg
  39.         }
  40.         catch {
  41.             $msg = "ERROR: Failed to create log directory '$($logDir)'. Script will abort."
  42.             Write-Host -ForegroundColor Red $msg
  43.             Exit
  44.         }
  45.     }
  46. }
  47.  
  48. ### Function to write information to the Log File
  49. Function Log-Write
  50. {
  51.     param
  52.     (
  53.         [Parameter(Mandatory=$true)]    [string]$Message
  54.     )
  55.     $lineItem = "[$(Get-Date -Format "dd-MMM-yyyy HH:mm:ss") | PID:$($pid) | $($env:username) ] " + $Message
  56.     Add-Content -Path $logFile -Value $lineItem
  57. }
  58.  
  59. ### Function to display the workgroups created by the user
  60. Function Select-MSPC_Workgroup {
  61.  
  62.     #######################################
  63.     # Display all mailbox workgroups
  64.     #######################################
  65.  
  66.     $workgroupPageSize = 100
  67.     $workgroupOffSet = 0
  68.     $workgroups = $null
  69.  
  70.     Write-Host
  71.     Write-Host -Object  "INFO: Retrieving MSPC workgroups ..."
  72.  
  73.     do
  74.     {
  75.         $workgroupsPage = @(Get-BT_Workgroup -PageOffset $workgroupOffSet -PageSize $workgroupPageSize)
  76.    
  77.         if($workgroupsPage) {
  78.             $workgroups += @($workgroupsPage)
  79.             foreach($Workgroup in $workgroupsPage) {
  80.                 Write-Progress -Activity ("Retrieving workgroups (" + $workgroups.Length + ")") -Status $Workgroup.Id
  81.             }
  82.  
  83.             $workgroupOffset += $workgroupPageSize
  84.         }
  85.  
  86.     } while($workgroupsPage)
  87.  
  88.     if($workgroups -ne $null -and $workgroups.Length -ge 1) {
  89.         Write-Host -ForegroundColor Green -Object ("SUCCESS: "+ $workgroups.Length.ToString() + " Workgroup(s) found.")
  90.     }
  91.     else {
  92.         Write-Host -ForegroundColor Red -Object  "INFO: No workgroups found."
  93.         Exit
  94.     }
  95.  
  96.     #######################################
  97.     # Prompt for the mailbox Workgroup
  98.     #######################################
  99.     if($workgroups -ne $null)
  100.     {
  101.         Write-Host -ForegroundColor Yellow -Object "ACTION: Select a Workgroup:"
  102.         Write-Host -ForegroundColor Gray -Object "INFO: your default workgroup has no name, only Id."
  103.  
  104.         for ($i=0; $i -lt $workgroups.Length; $i++)
  105.         {
  106.             $Workgroup = $workgroups[$i]
  107.             if($Workgroup.Name -eq $null) {
  108.                 Write-Host -Object $i,"-",$Workgroup.Id
  109.             }
  110.             else {
  111.                 Write-Host -Object $i,"-",$Workgroup.Name
  112.             }
  113.         }
  114.         Write-Host -Object "x - Exit"
  115.         Write-Host
  116.  
  117.         do
  118.         {
  119.             if($workgroups.count -eq 1) {
  120.                 $result = Read-Host -Prompt ("Select 0 or x")
  121.             }
  122.             else {
  123.                 $result = Read-Host -Prompt ("Select 0-" + ($workgroups.Length-1) + ", or x")
  124.             }
  125.            
  126.             if($result -eq "x")
  127.             {
  128.                 Exit
  129.             }
  130.             if(($result -match "^\d+$") -and ([int]$result -ge 0) -and ([int]$result -lt $workgroups.Length))
  131.             {
  132.                 $Workgroup=$workgroups[$result]
  133.                 Return $Workgroup.Id
  134.             }
  135.         }
  136.         while($true)
  137.  
  138.     }
  139.  
  140. }
  141.  
  142. ### Function to display all customers
  143. Function Select-MSPC_Customer {
  144.  
  145.     param
  146.     (      
  147.         [parameter(Mandatory=$true)] [String]$WorkgroupId
  148.     )
  149.  
  150.     #######################################
  151.     # Display all mailbox customers
  152.     #######################################
  153.  
  154.     $customerPageSize = 100
  155.     $customerOffSet = 0
  156.     $customers = $null
  157.  
  158.     Write-Host
  159.     Write-Host -Object  "INFO: Retrieving MSPC customers ..."
  160.  
  161.     do
  162.     {
  163.         $customersPage = @(Get-BT_Customer -WorkgroupId $WorkgroupId -IsDeleted False -IsArchived False -PageOffset $customerOffSet -PageSize $customerPageSize)
  164.    
  165.         if($customersPage) {
  166.             $customers += @($customersPage)
  167.             foreach($customer in $customersPage) {
  168.                 Write-Progress -Activity ("Retrieving customers (" + $customers.Length + ")") -Status $customer.CompanyName
  169.             }
  170.  
  171.             $customerOffset += $customerPageSize
  172.         }
  173.  
  174.     } while($customersPage)
  175.  
  176.     if($customers -ne $null -and $customers.Length -ge 1) {
  177.         Write-Host -ForegroundColor Green -Object ("SUCCESS: "+ $customers.Length.ToString() + " customer(s) found.")
  178.     }
  179.     else {
  180.         Write-Host -ForegroundColor Red -Object  "INFO: No customers found."
  181.         Exit
  182.     }
  183.  
  184.     #######################################
  185.     # {Prompt for the mailbox customer
  186.     #######################################
  187.     if($customers -ne $null)
  188.     {
  189.         Write-Host -ForegroundColor Yellow -Object "ACTION: Select a customer:"
  190.  
  191.         for ($i=0; $i -lt $customers.Length; $i++)
  192.         {
  193.             $customer = $customers[$i]
  194.             Write-Host -Object $i,"-",$customer.CompanyName
  195.         }
  196.         Write-Host -Object "x - Exit"
  197.         Write-Host
  198.  
  199.         do
  200.         {
  201.             if($customers.count -eq 1) {
  202.                 $result = Read-Host -Prompt ("Select 0 or x")
  203.             }
  204.             else {
  205.                 $result = Read-Host -Prompt ("Select 0-" + ($customers.Length-1) + ", or x")
  206.             }
  207.  
  208.             if($result -eq "x")
  209.             {
  210.                 Exit
  211.             }
  212.             if(($result -match "^\d+$") -and ([int]$result -ge 0) -and ([int]$result -lt $customers.Length))
  213.             {
  214.                 $customer=$customers[$result]
  215.                 Return $Customer.OrganizationId
  216.             }
  217.         }
  218.         while($true)
  219.  
  220.     }
  221.  
  222. }
  223.  
  224. ### Function to display all mailbox connectors
  225. Function Select-MW_Connector {
  226.  
  227.     param
  228.     (      
  229.         [parameter(Mandatory=$true)] [guid]$customerId
  230.     )
  231.  
  232.     #######################################
  233.     # Display all mailbox connectors
  234.     #######################################
  235.    
  236.     $connectorPageSize = 100
  237.     $connectorOffSet = 0
  238.     $connectors = $null
  239.  
  240.     Write-Host
  241.     Write-Host -Object  "INFO: Retrieving mailbox connectors ..."
  242.    
  243.     do
  244.     {
  245.         $connectorsPage = @(Get-MW_MailboxConnector -ticket $global:mwTicket -OrganizationId $customerId -PageOffset $connectorOffSet -PageSize $connectorPageSize)
  246.    
  247.         if($connectorsPage) {
  248.             $connectors += @($connectorsPage)
  249.             foreach($connector in $connectorsPage) {
  250.                 Write-Progress -Activity ("Retrieving connectors (" + $connectors.Length + ")") -Status $connector.Name
  251.             }
  252.  
  253.             $connectorOffset += $connectorPageSize
  254.         }
  255.  
  256.     } while($connectorsPage)
  257.  
  258.     if($connectors -ne $null -and $connectors.Length -ge 1) {
  259.         Write-Host -ForegroundColor Green -Object ("SUCCESS: "+ $connectors.Length.ToString() + " mailbox connector(s) found.")
  260.     }
  261.     else {
  262.         Write-Host -ForegroundColor Red -Object  "INFO: No mailbox connectors found."
  263.         Exit
  264.     }
  265.  
  266.     #######################################
  267.     # {Prompt for the mailbox connector
  268.     #######################################
  269.     if($connectors -ne $null)
  270.     {
  271.        
  272.  
  273.         for ($i=0; $i -lt $connectors.Length; $i++)
  274.         {
  275.             $connector = $connectors[$i]
  276.             Write-Host -Object $i,"-",$connector.Name
  277.         }
  278.         Write-Host -Object "x - Exit"
  279.         Write-Host
  280.  
  281.         Write-Host -ForegroundColor Yellow -Object "ACTION: Select the source mailbox connector:"
  282.  
  283.         do
  284.         {
  285.             $result = Read-Host -Prompt ("Select 0-" + ($connectors.Length-1) + " o x")
  286.             if($result -eq "x")
  287.             {
  288.                 Exit
  289.             }
  290.             if(($result -match "^\d+$") -and ([int]$result -ge 0) -and ([int]$result -lt $connectors.Length))
  291.             {
  292.                 $global:connector=$connectors[$result]
  293.                 Break
  294.             }
  295.         }
  296.         while($true)
  297.     }
  298.  
  299. }
  300.  
  301. Function Add-MW_Category {
  302.     param
  303.     (      
  304.         [parameter(Mandatory=$true)] [Object]$Connector
  305.     )
  306.  
  307.     # add items to a MigrationWiz project
  308.  
  309.     $count = 0
  310.  
  311.     Write-Host                                                                  
  312.     Write-Host -Object  ("Aplying categories to migration item(s) in the MigrationWiz project " + $connector.Name)
  313.     $importFilename = (Read-Host -prompt "Enter the full path to CSV import file")
  314.  
  315.     # read csv file
  316.     $users = Import-Csv -Path $importFilename
  317.     foreach($user in $users)
  318.     {
  319.         $sourceEmail = $user.'Source Email'
  320.         $flags = $user.'Flags'
  321.  
  322.         if($sourceEmail -ne $null -and $sourceEmail -ne "" -and $flags -in 1..6)
  323.         {
  324.             $count++
  325.             Write-Progress -Activity ("Applying category to migration item (" + $count + ")") -Status $sourceEmail
  326.             $mbx = get-mw_mailbox -ticket $mwTicket -ExportEmailAddress $sourceEmail
  327.             if ($mbx)
  328.             {
  329.                 $Category = ";tag-"+$flags+";"
  330.                 $result = Set-MW_Mailbox -Ticket $mwTicket -ConnectorId $connector.Id -mailbox $mbx -Categories $Category
  331.             }
  332.             else
  333.             {
  334.                 Write-Host "Cannot find MigrationWiz line item with source address: '$($sourceEmail)'" -ForegroundColor Yellow  
  335.             }
  336.         }
  337.         else {
  338.             Write-Host "The line item with the address '$($sourceEmail)' and the flag '$($flags)' is not valid." -ForegroundColor Yellow
  339.         }
  340.     }
  341.    
  342.     if($count -eq 1)
  343.     {
  344.         Write-Host -Object "1 mailbox has been categorized in",$connector.Name -ForegroundColor Green
  345.     }
  346.     if($count -ge 2)
  347.     {
  348.         Write-Host -Object $count," mailboxes have been categorized in",$connector.Name -ForegroundColor Green
  349.     }
  350.  
  351. }
  352.  
  353. #######################################################################################################################
  354. #                                               MAIN PROGRAM
  355. #######################################################################################################################
  356.  
  357. #Working Directory
  358. $workingDir = "C:\scripts"
  359.  
  360. #Logs directory
  361. $logDirName = "LOGS"
  362. $logDir = "$workingDir\$logDirName"
  363.  
  364. #Log file
  365. $logFileName = "$(Get-Date -Format yyyyMMdd)_Move-MW_Mailboxes.log"
  366. $logFile = "$logDir\$logFileName"
  367.  
  368. Create-Working-Directory -workingDir $workingDir -logDir $logDir
  369.  
  370. $msg = "++++++++++++++++++++++++++++++++++++++++ SCRIPT STARTED ++++++++++++++++++++++++++++++++++++++++"
  371. Log-Write -Message $msg
  372.  
  373. # Authenticate
  374. $creds = Get-Credential -Message "Enter BitTitan credentials"
  375. try {
  376.     # Get a ticket and set it as default
  377.     $ticket = Get-BT_Ticket -Credentials $creds -ServiceType BitTitan -SetDefault
  378.     # Get a MW ticket
  379.     $global:mwTicket = Get-MW_Ticket -Credentials $creds
  380. } catch {
  381.     $msg = "ERROR: Failed to create ticket."
  382.     Write-Host -ForegroundColor Red  $msg
  383.     Log-Write -Message $msg
  384.     Write-Host -ForegroundColor Red $_.Exception.Message
  385.     Log-Write -Message $_.Exception.Message    
  386.  
  387.     Exit
  388. }
  389.  
  390. #Select workgroup
  391. $WorkgroupId = Select-MSPC_WorkGroup
  392.  
  393. #Select customer
  394. $customerId = Select-MSPC_Customer -Workgroup $WorkgroupId
  395.  
  396. #Select connector
  397. Select-MW_Connector -customerId $customerId
  398. $result = Add-MW_Category -Connector $connector
  399.  
  400.  
  401. $msg = "++++++++++++++++++++++++++++++++++++++++ SCRIPT FINISHED ++++++++++++++++++++++++++++++++++++++++`n"
  402. Log-Write -Message $msg
  403.  
  404. ##END SCRIPT
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top