Re: Cleanup Script

By Gruff Water Vole, 7 Years ago, written in PowerShell, viewed 172 times. This paste is a reply to by
URL http://pb.stoleyour.com/view/ea7a8cd5 Embed
Download Paste or View RawExpand paste to full width of browser
  1. #------------------------------------------------------------------------------------------------------------------------#
  2. # Title:       Cleanup Script                                                                                            #
  3. # Author:      Nicholas Raposo                                                                                           #
  4. # Version:     1.0                                                                                                       #
  5. # Updated:     10/31/2018                                                                                                #
  6. # Description: This script strips groups from users and stores 1 log file and 1 working file. The log file saves the     #
  7. #              the groups that were stripped from a user and the working file is used to determine when to delete a      #
  8. #              users files from the home directory.                                                                      #
  9. #------------------------------------------------------------------------------------------------------------------------#
  10.  
  11. Import-Module ActiveDirectory
  12. #Create Exchange PowerShell PSSession
  13. $s=New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://va-ex01/powershell -Authentication Kerberos
  14. #Import Exchange PowerShell Session
  15. Import-PSSession -Session $s -AllowClobber
  16.  
  17. #Data initialization: get the xml file and ingest it, import the data we need, get exchange recipients.
  18. $dt = Get-Date
  19. $homeFolderPath = "\\va-fp03\home$\"
  20. $logStripPath = "c:\logs\groupsstrip\"
  21. $OUPath = "OU=DISABLED USERS,DC=vanaqua,DC=local"
  22. $xmlfile = "\\va-dc02\c$\logs\cleanup\users.xml"
  23. [xml]$xmlObj = [System.Xml.XmlDocument](Get-Content $xmlfile)
  24. $users = $xmlObj.SelectNodes("//*[@Name]")
  25. $recipients = Get-Recipient "*@ocean.org"
  26.  
  27. foreach ($username in (Get-ADUser -SearchBase $OUPath -filter *)) {
  28.  
  29.     #If the user hasn't been added to the list and doesn't have their home
  30.     #folder cleaned up already then add them to the list.
  31.     $homeDirectory = $homeFolderPath + $username.SamAccountName
  32.     if($username.Name -notin $users){
  33.         if(Test-Path $homeDirectory){    
  34.             $child = $xmlObj.CreateElement("User")
  35.             $child.SetAttribute("Name", $username.Name)
  36.  
  37.             $childDay = $xmlObj.CreateElement("Day")
  38.             $childDay.InnerText = $dt.Day
  39.  
  40.             $childMonth = $xmlObj.CreateElement("Month")
  41.             $childMonth.InnerText = $dt.Month
  42.  
  43.             $child.AppendChild($childDay)
  44.             $child.AppendChild($childMonth)
  45.             $xmlObj.Users.AppendChild($child)
  46.         }
  47.     }
  48.  
  49.         # Get all group memberships
  50.         $groups = get-adprincipalgroupmembership $username;
  51.  
  52.         # Loop through each group
  53.         foreach ($group in $groups) {
  54.  
  55.                 # Exclude Domain Users group
  56.                 if ($group.name -ne "domain users") {
  57.                
  58.                         # Write progress to screen
  59.                         write-host "removed" $username "from" $group.samaccountname -Foreground Yellow;
  60.                        
  61.                         # Remove user from group
  62.                         remove-adgroupmember -Identity $group.SamAccountName -Member $username.SamAccountName -Confirm:$false;
  63.                        
  64.                         # Define and save group names into filename in c:\logs\groupsstrip\username
  65.                         $grouplogfile = $logStripPath + $username.SamAccountName + ".txt";
  66.                         $group.name >> $grouplogfile
  67.                 }
  68.  
  69.         }
  70. }
  71.  
  72. #Loop through the users in our working file and disable the mailbox then delete the files as needed.
  73. ForEach ($usr in $users){
  74.     if($usr.attributes['Name'].value -in $recipients.Name){
  75.         #Disable users mailbox if it exists.
  76.         Get-Mailbox -identity $usr.attributes['Name'].value | Disable-Mailbox -confirm:$false
  77.     }
  78.  
  79.     #Do some date & time wizardry to not have discrepancies between years, alternative would be to implement absolute time.
  80.     $tempMonthCurrent = $dt.Month
  81.     $tempMonthUsr = [int]$usr.Month + 3
  82.     if($tempMonthUsr -gt 12){
  83.         $tempMonthUsr = $tempMonthUsr - 12
  84.         if([int]$dt.Month + 3 -gt 12){
  85.             $tempMonthCurrent = $tempMonthCurrent - 12
  86.         }
  87.     }
  88.  
  89.     if($tempMonthCurrent -gt $tempMonthUsr){
  90.         $tempUsr = $usr.attributes['Name'].value -replace ' ', '.'
  91.         $tempPath = $homeFolderPath + $tempUsr
  92.         Remove-Item -path $tempPath
  93.  
  94.         #Remove the user node as we are now finished all cleanup.
  95.         $usr.ParentNode.RemoveChild($usr)
  96.     }
  97. }
  98.  
  99. $xmlObj.Save($xmlfile)
  100. Remove-PSSession $s

Reply to "Re: Cleanup Script"

Here you can reply to the paste above