PST Find,Rename and Move

Ask your 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.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
dwight.brookland
Posts: 17
Last visit: Tue Mar 26, 2024 11:24 am

Re: PST Find,Rename and Move

Post by dwight.brookland »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PST Find,Rename and Move

Post by jvierra »

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: 17
Last visit: Tue Mar 26, 2024 11:24 am

Re: PST Find,Rename and Move

Post by dwight.brookland »

I will sort it out myself.
User avatar
dwight.brookland
Posts: 17
Last visit: Tue Mar 26, 2024 11:24 am

Re: PST Find,Rename and Move

Post by dwight.brookland »

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.     .NOTES
  3.     ===========================================================================
  4.      Created with:  SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.153
  5.      Created on:    8/15/2018 3:12 PM
  6.      Created by:    admdbr
  7.      Organization:  
  8.      Filename:      
  9.     ===========================================================================
  10.     .DESCRIPTION
  11.         PST Migration Script.
  12. #>
  13. $searchClasses = "UNC Search", "File Search"
  14. $c = 1
  15. $searchClasses | Foreach {
  16.     Write-Host $c.ToString() -NoNewLine
  17.     Write-Host " --- " -NoNewLine
  18.     Write-Host $_ -ForegroundColor Green
  19.     $c++
  20. }
  21.  
  22. [int]$searchClass = Read-Host "Select the search class"
  23. $choice = $searchClasses[$searchClass - 1]
  24. Write-Host $choice -ForegroundColor Yellow
  25. $pstData = @()
  26. ## BUILD USER FOLDER LIST ##
  27. If ($choice -eq "UNC Search")
  28. {
  29.     $userFilePath = Read-Host "Enter UNC Path to User Folders: (Example: \\Thermon.local\USSM\HOME)"
  30.     $destination = Read-Host "Enter Destination Folder Path: (Excample: \\Thermon.local\USSM\HOME\PSTs)"
  31.     $UserFolders = Get-ChildItem -Path $userFilePath -Directory
  32.     $UserFolders | foreach {
  33.         Try
  34.         {
  35.             $user = Get-ADUser -Identity $_.Name -Properties *
  36.             $homeDir = $userFilePath + "\" + $_
  37.             $filter = "*.pst"
  38.             $n = 1
  39.             $userPSTs = Get-ChildItem -Path $homeDir -Filter $filter -Recurse
  40.             $userPSTs | foreach {
  41.                 $fullName = $_.FullName
  42.                 $oldName = $_.Name
  43.                 $newName = $_.Name
  44.                 $newName = $newName.Replace(".pst", "$n.pst")
  45.                 $moveName = $newName
  46.                 Rename-Item -NewName $newName -Path $fullName
  47.                 $fullName = $fullName.Replace("$oldName", "$newName")
  48.                 $newName = $user.UserPrincipalName + "_" + $newName
  49.                 Rename-Item -NewName $newName -Path $fullName
  50.                 $fullName = $fullName.Replace("$moveName","$newName")
  51.                 $pstData += Get-Item -Path $fullName
  52.                 Copy-Item -Path $fullname -Filter $filter -Destination $destination
  53.                 $n++
  54.             }
  55.         }
  56.         CATCH
  57.         {
  58.         Write-Host "User $_.Name Not Found in ActiveDirectory" -ForegroundColor Yellow
  59.         }
  60.     }
  61.     ## Create Output for PST Mapping File for O365 Import Job ##
  62.     $pstData | select Name,Length,FullName | Out-GridView
  63.     $desData = Get-ChildItem -Path $destination -Filter $filter
  64.     $desData | select Name,Length | Out-GridView
  65.     Write-Host "Total PST Files in Destination: " ($desData).count
  66.     Write-Host "Total PST Files in Source of Active Users: " ($pstData).Count
  67.     ## Verification Check ##
  68.     $choicDelete = Read-Host "Verification Completed Successful? (Yes/No)"
  69.     If ($choicDelete -eq "Yes")
  70.     {
  71.         $pstData | foreach {
  72.         Remove-Item -Path $_.FullName -Confirm:$false
  73.         }
  74.     If ($choiceDelete -eq "No")
  75.     {
  76.         Write-Host "You have Selected NO. Script is Terminating!!!!!!"
  77.         }
  78.     }
  79. }
  80.  
  81. If ($choice -eq "File Search")
  82. {
  83.     $File = Read-Host "Enter File Name and Path to Search: (Example: \\Thermon\USSM\ITData\PSTMigration\User_Group1.csv"
  84.     $destination = Read-Host "Enter Destination Folder Path: (Excample: \\Thermon.local\USSM\HOME\PSTs)"
  85.     $users = Import-Csv -Path $File
  86.     $users | foreach {
  87.         Try
  88.         {
  89.             $user = Get-ADUser -Identity $_.samAccountName -Properties *
  90.             $homeDir = $user.HomeDirectory
  91.             $filter = "*.pst"
  92.             $n = 1
  93.             $userPSTs = Get-ChildItem -Path $homeDir -Filter $filter -Recurse
  94.             $userPSTs | foreach {
  95.                 $fullName = $_.FullName
  96.                 $oldName = $_.Name
  97.                 $newName = $_.Name
  98.                 $newName = $newName.Replace(".pst", "$n.pst")
  99.                 $moveName = $newName
  100.                 Rename-Item -NewName $newName -Path $fullpath
  101.                 $fullName = $fullName.Replace("$oldName", "$newName")
  102.                 $newName = $user.UserPrincipalName + "_" + $newName
  103.                 Rename-Item -NewName $newName -Path $fullName
  104.                 $fullName = $fullName.Replace("$moveName", "$newName")
  105.                 $pstData += Get-Item -Path $fullName
  106.                 Copy-Item -Path $fullName -Filter $filter -Destination $destination
  107.                 $n++
  108.             }
  109.         }
  110.         CATCH
  111.         {
  112.             Write-Host "User $_.Name Not Found in ActiveDirectory" -ForegroundColor Yellow
  113.         }
  114.     }
  115.     ## Create Output for PST Mapping File for O365 Import Job ##
  116.     $pstData | select Name, Length, FullName | Out-GridView
  117.     $desData = Get-ChildItem -Path $destination -Filter $filter
  118.     $desData | select Name, Length | Out-GridView
  119.     Write-Host "Total PST Files in Destination: " ($desData).count
  120.     Write-Host "Total PST Files in Source of Active Users: " ($pstData).Count
  121.     ## Verification Check ##
  122.     $choicDelete = Read-Host "Verification Completed Successful? (Yes/No)"
  123.     If ($choicDelete -eq "Yes")
  124.     {
  125.         $pstData | foreach {
  126.             Remove-Item -Path $_.FullName -Confirm:$false
  127.         }
  128.         If ($choiceDelete -eq "No")
  129.         {
  130.             Write-Host "You have Selected NO. Script is Terminating!!!!!!"
  131.         }
  132.     }
  133. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PST Find,Rename and Move

Post by jvierra »

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: 17
Last visit: Tue Mar 26, 2024 11:24 am

Re: PST Find,Rename and Move

Post by dwight.brookland »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PST Find,Rename and Move

Post by jvierra »

Compare-Object would be the method.
User avatar
dwight.brookland
Posts: 17
Last visit: Tue Mar 26, 2024 11:24 am

Re: PST Find,Rename and Move

Post by dwight.brookland »

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: 17
Last visit: Tue Mar 26, 2024 11:24 am

Re: PST Find,Rename and Move

Post by dwight.brookland »

Before you ask, I wanted to provide the code.
  1.         $sasURL = Read-Host "Please enter the SAS URL:"
  2.         $pstServer = Read-Host "Please enter server PSTs reside on. "
  3.         $sasURL = $sasURL.Replace("ingestiondata", "ingestiondata/$pstServer")
  4.         Write-Host $sasURL
  5.         Pause
  6.         Invoke-Command -ComputerName $pstServer -ScriptBlock {
  7.             CD  "C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy"
  8.             Write-Host
  9.             .\AzCopy.exe /Source:"$destination" /Dest:"$sasURL" /V:"$Log" /Y
  10.             Return
  11.         }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PST Find,Rename and Move

Post by jvierra »

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
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked