Site Tools


useraustritt_inkl._mailboxbackup

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, und die Mailbox des Accounts wird exportiert und deaktiviert.

only_disable.ps1
<# 
.SYNOPSIS
This is a Powershell Script to automate the Users QuitProcess
 
.DESCRIPTION
The Script should be staret manually by an requested 
 
.EXAMPLE
./only_disable_and_backup_ExchTest.ps1
 
.NOTES
 
 
.LINK
http://www.clemens-bayer.de/
 
#>
 
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$ScriptPath = Get-ScriptDirectory
 
#Getting Date
 
$Targetpath = "OUforDisabledusershere" #like "OU=DisabledUsersforDeletion,OU=Users,OU=Somewhere"
$OU = "PathtoOUforDisabledUsershere" #like "Somewhere/Users/DisabledUsersforDeletion"
$Topelevel = "yourToplevelhere
$Domain = "DomainwithoutToplevelhere"
$Domainname = "yourDomain.Toplevelhere"
$Servername = "yourServerhere"
$exchangeservername "yourExchanegServernamehere"
$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}
 
$testsession = get-pssession
 
if ($testsession) {Write-Host "Exchange Session loaded, no new creation needed" -foreground "yellow"}
else {$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$exchangeservername.de/PowerShell/ -Authentication Kerberos
Import-PSSession $Session
Write-Host "Exchange Session now loaded" -foreground "yellow"}
 
 
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 Number 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
		robocopy $SourcePath "$DestPath\$UserID" /R:1 /W:2 /NP /TEE /V /E /sec >>"$ScriptPath\robocopy_$UserID.log"
		#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
Get-ADPrincipalGroupMembership $UserID | select name | export-csv $ScriptPath\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
Get-ADUser $UserID -properties * | out-file $ScriptPath\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 '$Targetpath,DC=$Domain,DC=$Toplevel'
"User $UserID moved to temp OU and is ready for deletion" | Out-File "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append
 
start-sleep -seconds 5
 
#$ErrorActionPreference = "Stop"
 
$ErrorActionPreference
 
#Start Mailbox Export
 
Write-Host "Start MailboxexportRequest" -foreground "yellow"
 
New-MailboxExportRequest -Mailbox $UserID -FilePath "$DestPath\mailboxexport_$UserID.pst" -ErrorAction Stop
# hinzufügen wenn gewünscht -ExcludeFolders "#JunkEmail#","#DeletedItems#"
 
"Mailboxexport is for User $UserID is queued now" | Out-File -FilePath "\\dlfs1001\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append
Write-Host "Mailboxexport is getting done to File: "$DestPath\mailboxexport_$UserID.pst""
 
 
$UserID | ForEach-Object {
 
do {
 
$Complete = Get-MailboxExportRequest -Mailbox "$Domainname/$OU/$UserID" | select Status
 
 
if ($Complete -match 'Completed') {
        Write-Host
	    Write-Host "Deleting Completed ExportRequest" -foreground "yellow"
	    Get-MailboxExportRequest -Mailbox "$Domainname/$OU/$UserID" | Remove-MailboxExportRequest -Confirm:$False
	    Write-Host "Completed ExportRequest deleted" -foreground "yellow"
	    Write-Host "Disable Users Mailbox" -foreground "yellow"
		Disable-Mailbox -Identity $userID -Confirm:$False
		"Mailbox for User $UserID disabled" | Out-File "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append} 
 
elseif ($Complete -match 'Failed') { 
        Write-Host
        Write-Host "Mailbox Exportjob Failed for Account $UserID" -foreground "yellow"
		"Mailbox Exportjob Failed for Account $UserID" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append
        Write-Host} 
 
elseif (!$Complete) {$Complete = 'Failed'
					Write-Host "No Mailbox to Export, or Mailbox disconnected by someone else" -foreground "yellow"}
 
 
else { 
        Write-Host
        Write-Host "Exportjob still running, waiting for completition" -foreground "yellow"
		} 
 
		start-sleep -seconds 10
		Write-Host "Momentaner Status: $Complete" -foreground "yellow"
 
		}
				until ($Complete -match 'Completed' -or $Complete -match'Failed')
 
}
 
Get-MailboxExportRequest -status Completed | Remove-MailboxExportRequest -Confirm:$False
 
#$ErrorActionPreference = "silentlycontinue"
 
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 "Setting Users Manager to Dummymanager" -foreground "yellow"
Set-ADUser $UserID -manager "DummyManager"
"Manager set to Dummymanager for $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"
 
$LogDate = (Get-Date).ToString("yyyy.MM.dd")
$LogTime = (Get-Date).ToLongTimeString()
$LogPath = Test-Path "C:\Scripts\_AD_ISR\ISRLogging"
 
if ($LogPath -eq $True) {"----------------------------------------------------" | Out-File -FilePath "C:\Scripts\_AD_ISR\ISRLogging\Log_$LogDate.log" -Append}
else {New-Item -Path "C:\Scripts\_AD_ISR\ISRLogging" -ItemType directory}
 
"$LogDate $LogTime Done! Account $UserID removed from Groups, disabled and moved in Temp OU for deleting (ISR/Change $ChangeID)" | Out-File -FilePath "C:\Scripts\_AD_ISR\ISRLogging\Log_$LogDate.log" -Append
 
Stop-Transcript
useraustritt_inkl._mailboxbackup.txt · Last modified: 2017/12/29 10:47 by 212.204.110.133