User Tools

Site Tools


dritten_teil
thirdrun.ps1
<#
 
.SYNOPSIS
This is Powershell Script 3/3 to automate the Users Quit Process
 
.DESCRIPTION
The script will run every Day and will Check specified Path for a TXT File with User and Informations
The Script will finally delete all Accountdependencies and the account itself
With running this Script, the automated Users Quit Process is finished and can not be Rolled Back
 
.EXAMPLE
./backupuserexport_thirdrun.ps1
 
.NOTES
TXT file has to be located here: \DLFS1001\UserDataBackup$\source\Done\$oldDate\user_third.txt
 
.LINK
http://www.clemens-bayer.de/
 
#>
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$ScriptPath = Get-ScriptDirectory
 
#This is a script for finally delete all Accountdependencies and the AD Account itself
 
$Servername = "Name eures Backupservers" #Pfade sollten so vorhanden sein, wie im Script angegeben.
 
#Getting Date
 
$date = (Get-Date).ToString("_yyyyMMdd")
# aktuell 7 Tage
$oldDate = (get-date).AddDays(-7).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
$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://ExchangeServer.domain.de/PowerShell/ -Authentication Kerberos
Import-PSSession $Session
Write-Host "Exchange Session now loaded" -foreground "yellow"}
#Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
 
Start-Transcript -Path "\\$Servername\UserDataBackup$\source\Logs\$Date\03$date.log" -Append
"$Time backupuserexport_thirdrun.ps1 started" | Out-File -FilePath "\\dlfs1001\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append
 
#Getting Users
 
if (Test-Path "\\$Servername\UserDataBackup$\source\Done\$oldDate\user_third.txt") {
 
$users = Get-Content "\\$Servername\UserDataBackup$\source\Done\$oldDate\user_third.txt"
 
#Copyjob
 
$users | ForEach-Object {
 
if ($check) {clear-variable -name check}
$check = Get-ADUser $_ -properties * | select -ExpandProperty sn
 
if ($check) {
 
Get-ADUser $_ -properties * | select -ExpandProperty homeDirectory | out-file $ScriptPath\Info\olddirs.txt -append
 
#Defining Paths
 
$DelPath1 = "\\$Servername\UserDataBackup$\Userbackups\$oldDate\$_"
 
Write-Host "Finally deleting Users Backup from Netdrive for userid $_" -foreground "yellow"
"Finally deleting Users Backup from Netdrive for userid $_" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append
 
if ($DelPath1) {cmd /q /x /c rmdir /q /s "$DelPath1"
Write-Host "Users Backup from Netdrive $DelPath1 deleted for userid $_" -foreground "yellow"
"Users Backup from Netdrive $DelPath1 deleted for userid $_" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append}
else {Write-Host "No Users Backup on Netdrive, so nothing to delete for userid $_" -foreground "yellow"
"No Users Backup on Netdrive, so nothing to delete for userid $_" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append}
 
 
if ($DelPath2) {clear-variable -name DelPath2}
$DelPath2 = Get-ADUser $_ -properties * | select -ExpandProperty homeDirectory
 
 
Write-Host "Finally deleting Users Homedirectory for userid $_" -foreground "yellow"
"Finally deleting Users Homedirectory for userid $_" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append
 
if ($DelPath2) {cmd /q /x /c rmdir /q /s "$DelPath2"
Write-Host "Homedirectory $DelPath2 deleted for userid $_" -foreground "yellow"
"Homedirectory $DelPath2 deleted for userid $_" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append}
else {Write-Host "No Homedirectory, so nothing to delete for userid $_" -foreground "yellow"
"No Homedirectory, so nothing to delete for userid $_" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append}
#cmd /q /x /c rd /q /s "$DelPath2"
 
#Deleting Users AD Account
 
Write-Host "Finally deleting UserAccount $_" -foreground "yellow"
 
$userdel = Get-ADUser $_
 
try {
 
   Remove-ADUser $userdel -confirm:$false -ErrorAction Stop
 
} catch {
 
    Remove-ADObject -Identity $userdel.DistinguishedName -confirm:$false -Recursive
 
}}
 
"If existent, Useraccount and Homeshare for User $_ finally deleted" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append
 
}}
 
else {"No File existent, so no Users need to be proceded" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append}
 
Get-MailboxExportRequest -status Completed | Remove-MailboxExportRequest -Confirm:$False
 
$Done = "\\$Servername\UserDataBackup$\source\Done\$oldDate"
if(Test-Path $Done) {
if (Test-Path "$Done\user_third.txt") {Move-Item -Path "$Done\user_third.txt" -Destination "$Done\user_third_done.txt"}
}
else {
New-Item -Path $Done -ItemType directory
if (Test-Path "$Done\user_third.txt") {Move-Item -Path "$Done\user_third.txt" -Destination "$Done\user_third_done.txt"}
}
 
if ($Time) {clear-variable -name Time}
$Time = (Get-Date).ToLongTimeString()
"$Time backupuserexport_thirdrun.ps1 finished" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append
 
 
Stop-Transcript
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
dritten_teil.txt · Last modified: 2018/12/14 11:47 by 192.168.1.1