PST Find,Rename and Move

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
dwight.brookland
Posts: 15
Joined: Sun Mar 03, 2013 12:43 pm

Re: PST Find,Rename and Move

Post by dwight.brookland » Thu Aug 23, 2018 7:39 am

So I have added a Try Catch to help with error handling on AD user find Its not 100% clean but its working as intended. What I am also needing is during the steps to output each pst file with the user it belongs to as well as some user information to a global variable that is a sum of all the pst's that is processed through the try / catch and moved.

Not sure if that makes sense or not.

At the end of the Foreach where its processing the user folders i need it to gather data.
User's UserPrincipalName, the Name of the PST and a Folder Column that is the pst name minus the .pst. Add that to a global array so we can copy and past the output to a csv, manipulate table headings and use it to as the user mapping file for pst uploads into o365/exchange online.

Not sure how to get the array to do that.

jvierra
Posts: 13901
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PST Find,Rename and Move

Post by jvierra » Thu Aug 23, 2018 9:44 am

Unfortunately you are not asking a question. You are making a lot of statements about what you would like to do. Most of what you want is quite vague. What is it that you don't understand abut arrays? What do you mean by copy and paste to a CSV?

User avatar
dwight.brookland
Posts: 15
Joined: Sun Mar 03, 2013 12:43 pm

Re: PST Find,Rename and Move

Post by dwight.brookland » Thu Aug 23, 2018 11:07 am

I will sort it out myself.

User avatar
dwight.brookland
Posts: 15
Joined: Sun Mar 03, 2013 12:43 pm

Re: PST Find,Rename and Move

Post by dwight.brookland » Thu Aug 23, 2018 12:41 pm

jvierra,

This is what I was referring to.
I added line 25 to initiate the array and then line 51 to add the current file being processed to the array and then lines 62-68 to produce a way of visually verifying that the files that were supposed to be copied are what was copied. That both source files and destination files match in size. I could write a compare step. My question was or should have been "How do i create an array that will capture the file and its properties that is to copied when it is processing through an a foreach loop." I think I figured it out. If you would like to take a look and tell me if it looks about right that is cool.
  1. <# 
  2.  
  3.     .NOTES
  4.  
  5.     ===========================================================================
  6.  
  7.      Created with:  SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.153
  8.  
  9.      Created on:    8/15/2018 3:12 PM
  10.  
  11.      Created by:    admdbr
  12.  
  13.      Organization:  
  14.  
  15.      Filename:      
  16.  
  17.     ===========================================================================
  18.  
  19.     .DESCRIPTION
  20.  
  21.         PST Migration Script.
  22.  
  23. #>
  24.  
  25. $searchClasses = "UNC Search", "File Search"
  26.  
  27. $c = 1
  28.  
  29. $searchClasses | Foreach {
  30.  
  31.     Write-Host $c.ToString() -NoNewLine
  32.  
  33.     Write-Host " --- " -NoNewLine
  34.  
  35.     Write-Host $_ -ForegroundColor Green
  36.  
  37.     $c++
  38.  
  39. }
  40.  
  41.  
  42.  
  43. [int]$searchClass = Read-Host "Select the search class"
  44.  
  45. $choice = $searchClasses[$searchClass - 1]
  46.  
  47. Write-Host $choice -ForegroundColor Yellow
  48.  
  49. $pstData = @()
  50.  
  51. ## BUILD USER FOLDER LIST ##
  52.  
  53. If ($choice -eq "UNC Search")
  54.  
  55. {
  56.  
  57.     $userFilePath = Read-Host "Enter UNC Path to User Folders: (Example: \\Thermon.local\USSM\HOME)"
  58.  
  59.     $destination = Read-Host "Enter Destination Folder Path: (Excample: \\Thermon.local\USSM\HOME\PSTs)"
  60.  
  61.     $UserFolders = Get-ChildItem -Path $userFilePath -Directory
  62.  
  63.     $UserFolders | foreach {
  64.  
  65.         Try
  66.  
  67.         {
  68.  
  69.             $user = Get-ADUser -Identity $_.Name -Properties *
  70.  
  71.             $homeDir = $userFilePath + "\" + $_
  72.  
  73.             $filter = "*.pst"
  74.  
  75.             $n = 1
  76.  
  77.             $userPSTs = Get-ChildItem -Path $homeDir -Filter $filter -Recurse
  78.  
  79.             $userPSTs | foreach {
  80.  
  81.                 $fullName = $_.FullName
  82.  
  83.                 $oldName = $_.Name
  84.  
  85.                 $newName = $_.Name
  86.  
  87.                 $newName = $newName.Replace(".pst", "$n.pst")
  88.  
  89.                 $moveName = $newName
  90.  
  91.                 Rename-Item -NewName $newName -Path $fullName
  92.  
  93.                 $fullName = $fullName.Replace("$oldName", "$newName")
  94.  
  95.                 $newName = $user.UserPrincipalName + "_" + $newName
  96.  
  97.                 Rename-Item -NewName $newName -Path $fullName
  98.  
  99.                 $fullName = $fullName.Replace("$moveName","$newName")
  100.  
  101.                 $pstData += Get-Item -Path $fullName
  102.  
  103.                 Copy-Item -Path $fullname -Filter $filter -Destination $destination
  104.  
  105.                 $n++
  106.  
  107.             }
  108.  
  109.         }
  110.  
  111.         CATCH
  112.  
  113.         {
  114.  
  115.         Write-Host "User $_.Name Not Found in ActiveDirectory" -ForegroundColor Yellow
  116.  
  117.         }
  118.  
  119.     }
  120.  
  121.     ## Create Output for PST Mapping File for O365 Import Job ##
  122.  
  123.     $pstData | select Name,Length,FullName | Out-GridView
  124.  
  125.     $desData = Get-ChildItem -Path $destination -Filter $filter
  126.  
  127.     $desData | select Name,Length | Out-GridView
  128.  
  129.     Write-Host "Total PST Files in Destination: " ($desData).count
  130.  
  131.     Write-Host "Total PST Files in Source of Active Users: " ($pstData).Count
  132.  
  133.     ## Verification Check ##
  134.  
  135.     $choicDelete = Read-Host "Verification Completed Successful? (Yes/No)"
  136.  
  137.     If ($choicDelete -eq "Yes")
  138.  
  139.     {
  140.  
  141.         $pstData | foreach {
  142.  
  143.         Remove-Item -Path $_.FullName -Confirm:$false
  144.  
  145.         }
  146.  
  147.     If ($choiceDelete -eq "No")
  148.  
  149.     {
  150.  
  151.         Write-Host "You have Selected NO. Script is Terminating!!!!!!"
  152.  
  153.         }
  154.  
  155.     }
  156.  
  157. }
  158.  
  159.  
  160.  
  161. If ($choice -eq "File Search")
  162.  
  163. {
  164.  
  165.     $File = Read-Host "Enter File Name and Path to Search: (Example: \\Thermon\USSM\ITData\PSTMigration\User_Group1.csv"
  166.  
  167.     $destination = Read-Host "Enter Destination Folder Path: (Excample: \\Thermon.local\USSM\HOME\PSTs)"
  168.  
  169.     $users = Import-Csv -Path $File
  170.  
  171.     $users | foreach {
  172.  
  173.         Try
  174.  
  175.         {
  176.  
  177.             $user = Get-ADUser -Identity $_.samAccountName -Properties *
  178.  
  179.             $homeDir = $user.HomeDirectory
  180.  
  181.             $filter = "*.pst"
  182.  
  183.             $n = 1
  184.  
  185.             $userPSTs = Get-ChildItem -Path $homeDir -Filter $filter -Recurse
  186.  
  187.             $userPSTs | foreach {
  188.  
  189.                 $fullName = $_.FullName
  190.  
  191.                 $oldName = $_.Name
  192.  
  193.                 $newName = $_.Name
  194.  
  195.                 $newName = $newName.Replace(".pst", "$n.pst")
  196.  
  197.                 $moveName = $newName
  198.  
  199.                 Rename-Item -NewName $newName -Path $fullpath
  200.  
  201.                 $fullName = $fullName.Replace("$oldName", "$newName")
  202.  
  203.                 $newName = $user.UserPrincipalName + "_" + $newName
  204.  
  205.                 Rename-Item -NewName $newName -Path $fullName
  206.  
  207.                 $fullName = $fullName.Replace("$moveName", "$newName")
  208.  
  209.                 $pstData += Get-Item -Path $fullName
  210.  
  211.                 Copy-Item -Path $fullName -Filter $filter -Destination $destination
  212.  
  213.                 $n++
  214.  
  215.             }
  216.  
  217.         }
  218.  
  219.         CATCH
  220.  
  221.         {
  222.  
  223.             Write-Host "User $_.Name Not Found in ActiveDirectory" -ForegroundColor Yellow
  224.  
  225.         }
  226.  
  227.     }
  228.  
  229.     ## Create Output for PST Mapping File for O365 Import Job ##
  230.  
  231.     $pstData | select Name, Length, FullName | Out-GridView
  232.  
  233.     $desData = Get-ChildItem -Path $destination -Filter $filter
  234.  
  235.     $desData | select Name, Length | Out-GridView
  236.  
  237.     Write-Host "Total PST Files in Destination: " ($desData).count
  238.  
  239.     Write-Host "Total PST Files in Source of Active Users: " ($pstData).Count
  240.  
  241.     ## Verification Check ##
  242.  
  243.     $choicDelete = Read-Host "Verification Completed Successful? (Yes/No)"
  244.  
  245.     If ($choicDelete -eq "Yes")
  246.  
  247.     {
  248.  
  249.         $pstData | foreach {
  250.  
  251.             Remove-Item -Path $_.FullName -Confirm:$false
  252.  
  253.         }
  254.  
  255.         If ($choiceDelete -eq "No")
  256.  
  257.         {
  258.  
  259.             Write-Host "You have Selected NO. Script is Terminating!!!!!!"
  260.  
  261.         }
  262.  
  263.     }
  264.  
  265. }

jvierra
Posts: 13901
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PST Find,Rename and Move

Post by jvierra » Thu Aug 23, 2018 12:58 pm

What array are you asking about? It sounds more like you are trying to create custom objects.

To create a collection of objects use:
$myobjects = @()

Add to the collection like this:
$myobjects += $mycustomobject

User avatar
dwight.brookland
Posts: 15
Joined: Sun Mar 03, 2013 12:43 pm

Re: PST Find,Rename and Move

Post by dwight.brookland » Thu Aug 23, 2018 1:11 pm

Okay that is what I wanted to know.
Now they want me to add a self comparison to it. I am not sure how to accomplish this. I have done some reading but not sure how to do it.
They would like to compare the source files that were added to the $pstData array with the capture of the $destData array and verify file sizes (length)
What would be the right way to do that? Would compare-object? Any reference / direction would be helpful.

jvierra
Posts: 13901
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PST Find,Rename and Move

Post by jvierra » Thu Aug 23, 2018 1:16 pm

Compare-Object would be the method.

User avatar
dwight.brookland
Posts: 15
Joined: Sun Mar 03, 2013 12:43 pm

Re: PST Find,Rename and Move

Post by dwight.brookland » Mon Aug 27, 2018 8:48 am

Okay here is I hope to be my last question.

How do you wrap a variable in quotes.
I am trying to have an input requirement of the SAS URL for pst upload and then kick off an AzCopy command but the command requires that the SAS URL be wrapped in quotes. I get various errors relating to that variable and i believe its related to the missing quotes.

Thanks,
Dwight

User avatar
dwight.brookland
Posts: 15
Joined: Sun Mar 03, 2013 12:43 pm

Re: PST Find,Rename and Move

Post by dwight.brookland » Mon Aug 27, 2018 9:07 am

Before you ask, I wanted to provide the code.
  1.         $sasURL = Read-Host "Please enter the SAS URL:"
  2.  
  3.         $pstServer = Read-Host "Please enter server PSTs reside on. "
  4.  
  5.         $sasURL = $sasURL.Replace("ingestiondata", "ingestiondata/$pstServer")
  6.  
  7.         Write-Host $sasURL
  8.  
  9.         Pause
  10.  
  11.         Invoke-Command -ComputerName $pstServer -ScriptBlock {
  12.  
  13.             CD  "C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy"
  14.  
  15.             Write-Host
  16.  
  17.             .\AzCopy.exe /Source:"$destination" /Dest:"$sasURL" /V:"$Log" /Y
  18.  
  19.             Return
  20.  
  21.         }

jvierra
Posts: 13901
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PST Find,Rename and Move

Post by jvierra » Mon Aug 27, 2018 10:13 am

When passing variables it is not necessary to use quotes. You also need to pass the variable to the remote either as an argument or with the "using " scope identifier.

$using:sasUrl

Locked