Advertisement
joedigital

ps-ex of transcript + email

May 23rd, 2025
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. Create a file and folder on the root of the Drive and set permissions how you want them on the target items.
  3.  
  4. #>
  5.  
  6. Send-MailMessage -SmtpServer smtp.mydomain.com -To user@mydomain.com -From noreply@mydomain.com -Subject "Permissions Update Started"
  7. $TranscriptPath = "C:\Users\me\Documents\Migration.txt"
  8. Start-Transcript -Path $TranscriptPath
  9.  
  10.  
  11. #Specify drive letter (tested on another machine with a different drive).
  12. $DriveLetter = "D:"
  13.  
  14. #Get all file system objects in the folder
  15. $All = Get-ChildItem "$DriveLetter\myfolder"
  16.  
  17. #Create an array of just the files
  18. $Files = $All | ? {$_.PSIsContainer -ne $true}
  19.  
  20. #read permissions from "template" file
  21. $FileACL = Get-Acl "$DriveLetter\FileACL.txt"
  22.  
  23. #set permissions on each file in the root of the Dept share
  24. foreach ($f in $Files){
  25.     Write-Host $f.FullName
  26.     Set-Acl $f.FullName $FileACL
  27. }
  28.  
  29. #read permissions from "template" folder
  30. $FolderACL = Get-acl "$DriveLetter\TargetFolderACL"
  31.  
  32. #Create array of all folders
  33. $Folders = $All | ? {$_.PSIsContainer -eq $true}
  34. #$Folders.Count
  35.  
  36. #Create array of folder names that are not migrating
  37. $ExcludedFolders = @("array members")
  38. #Remove the folders that are not migrating from the array
  39. $Folders = $Folders | ? {$_.Name -notin $ExcludedFolders}
  40. #$Folders.Count
  41.  
  42. #Manually add subfolders that had inheritance disabled
  43. $BuriedFoldersWPerms = @("$DriveLetter\one","$DriveLetter\two","$DriveLetter\three")
  44.  
  45. #Update folder array to add the disabled inheritance sub-folders
  46. $BuriedFoldersWPerms.ForEach({
  47.     $Folders += Get-Item $_
  48. })
  49. #$Folders.Count
  50.  
  51. #process all the folders and update permissions - I put a timestamp between folders, just to see which ones took longest, etc.
  52. foreach ($f in $Folders){
  53.     Write-Host $f.FullName
  54.     Get-Date
  55.     Set-Acl $f.FullName $FolderACL
  56. }
  57.  
  58.  
  59. #stop logging
  60. Stop-Transcript
  61.  
  62. #email myself so I know it finished and attach the logfile
  63. Send-MailMessage -Attachments $TranscriptPath -SmtpServer smtp.mydomain.com -To user@mydomain.com -From noreply@mydomain.com -Subject "Permissions Update Complete"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement