Hier findet ihr ein Script welches die Gruppenmitliedschaften und das Homeshare auf einen Netzlaufwerk sichert. Der Account wird aus allen Gruppen gelöscht und in eine OU zum löschen verschoben. Ebenso wird das Feld “Beschreibung” mit Informationen gefüllt.
<# .SYNOPSIS This is a Powershell Script to automate the Users QuitProcess .DESCRIPTION The Script should be staret manually by an requested User Quit in ISR .EXAMPLE ./only_disable.ps1 .NOTES .LINK http://www.clemens-bayer.de/ #> #Getting Date $Servername = "Euer Backup Server" $date = (Get-Date).ToString("_yyyyMMdd") $Time = (Get-Date).ToLongTimeString() $LogPath = Test-Path "\\$Servername\UserDataBackup$\source\Logs\$Date" if ($LogPath -eq $True) {"Logpath is ok, no new creation needed" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$Date.log" -Append} else {New-Item -Path "\\$Servername\UserDataBackup$\source\Logs\$Date" -ItemType directory} Import-module ActiveDirectory Start-Transcript -Path "\\$Servername\UserDataBackup$\source\Logs\$Date\01$date.log" -Append "$Time backupuserexport_firstrun.ps1 started" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append #Getting Users $UserID = Read-Host "Please enter UserID to disble" $ChangeID = Read-Host "Please enter ISR Request Numer or ChangeID" #Defining Source and Destination path $DestPath = "\\$Servername\UserDataBackup$\Userbackups\$date\$UserID" if ($SourcePath) {clear-variable -name SourcePath} $SourcePath = Get-ADUser $UserID -properties * | select -ExpandProperty homeDirectory $string = "User disabled cause of ISR Request, will be deleted soon! $ChangeID" #Creating new folder for storing backup Write-Host "Creating Destination Directory" -foreground "yellow" New-Item -Path $DestPath -ItemType directory #Copying folder if (!$SourcePath) { Write-Host Write-Host "No Path to Export for User $UserID" -foreground "yellow" "No Path to Export for User $UserID" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append Write-Host} else { Write-Host "Copy Userhome to Destination Directory" -foreground "yellow" Write-Host Copy-Item -Recurse -Path $SourcePath -destination $DestPath Write-Host Write-Host "Userhome was copied from $SourcePath" -foreground "yellow" Write-Host "to $DestPath" -foreground "yellow" "Userhome was copied from $SourcePath to $DestPath" | Out-File "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append Write-Host} #Export UserInfo Write-Host "Exporting Users Groupmembership and copy to Destination Directory" -foreground "yellow" Get-ADPrincipalGroupMembership $UserID | select name | export-csv $DestPath\groupexport_$UserID.csv -notype "Users Groupmembership exported for User $UserID" | Out-File "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append Write-Host "Exporting User Details and copy to Destination Directory" -foreground "yellow" Get-ADUser $UserID -properties * | out-file $DestPath\userdetails_$UserID.txt "Users Details exported for User $UserID" | Out-File "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append #Move User to temporary OU Write-Host "Set User Descrition String" -foreground "yellow" Get-ADUser $UserID | Set-ADUser -Description $String Write-Host "Copy Account to temporary OU" -foreground "yellow" Get-ADUser $UserID | Move-ADObject -TargetPath 'OU=DisabledUsersforDeletion,OU=Users,OU=DL,DC=deutscheleasing,DC=de' "User $UserID moved to temp OU and is ready for deletion" | Out-File "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append Write-Host "Disable UserAccount" -foreground "yellow" Disable-ADAccount -Identity $UserID "User $UserID disabled" | Out-File "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append Write-Host "Deleting Users Groupmembership" -foreground "yellow" $Groups = Get-ADPrincipalGroupMembership $UserID | select name (Get-ADUser $UserID -properties memberof).memberof | Remove-ADGroupMember -Members $UserID -Confirm:$false #Remove-ADGroupMember -Identity $Groups -Member $UserID -Confirm:$false "Users Groupmembership deleted for User $UserID" | Out-File "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append Write-Host "$UserID is disabled" -foreground "yellow" Write-Host "$UserID is moved" -foreground "yellow"