User Tools

Site Tools


ersten_teil
firstrun.ps1
<#
 
.SYNOPSIS
This is Powershell Script 1/3 to automate the Users QuitProcess
 
.DESCRIPTION
The script will run every Day and will Check specified Path for a CSV File with User and Informations
It will copy the Users Homedrive and the Users exported Mailbox to a seperate Networkdrive
Account will be moved to a special OU and will be disabled
After one Day, the Process will continue with Part 2/3
 
.EXAMPLE
./backupuserexport_firstrun.ps1
 
.NOTES
CSV file has to be located here: \\$Servername\UserDataBackup$\source\user.csv
 
.LINK
http://www.clemens-bayer.de/
 
#>
 
 
 
#Getting Date
 
$Servername = "Name eures Backupservers"
$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
 
if (Test-Path "\\$Servername\UserDataBackup$\source\user.csv") {
 
$users = Import-CSV "\\$Servername\UserDataBackup$\source\user.csv"
 
#Copyjob
 
ForEach ($User in $Users) {
 
$UserID = $User.UserID
$ChangeID = $User.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"
 
}}
 
else {"No File existent, so no Users need to be proceded" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append}
 
 
#Getting Date
 
$date = (Get-Date).ToString("_yyyyMMdd")
 
#Getting Users
 
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
 
if (Test-Path "\\$Servername\UserDataBackup$\source\user.csv") {
 
$users = Import-CSV "\\$Servername\UserDataBackup$\source\user.csv"
#Copyjob
 
ForEach ($User in $Users) {
 
$UserID = $User.UserID
$Change = $User.Change
 
#Defining Source and Destination path
 
$DestPath = "\\$Servername\UserDataBackup$\Userbackups\$date\$UserID"
 
#Creating new folder for storing backup
 
Write-Host "Set Permissions on Destinationfolder" -foreground "yellow"
$ACL = Get-Acl "$DestPath"
$ACL.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule("deutscheleasing.de\Exchange Trusted Subsystem","FullControl","ContainerInherit, ObjectInherit", "None", "Allow")))
Set-Acl "$DestPath" $ACL
 
#Creating Mailboxexportjob
 
Write-Host "Start MailboxexportRequest" -foreground "yellow"
New-MailboxExportRequest -Mailbox $UserID -FilePath "$DestPath\mailboxexport_$UserID.pst"
"Mailboxexport is for User $UserID is queued now" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append
Write-Host "Mailboxexport is getting done to File: "$DestPath\mailboxexport_$UserID.pst""
 
}}
 
else {"No File existent, so no Users need to be proceded" | Out-File -FilePath "\\$Servername\UserDataBackup$\source\Logs\$Date\clear$date.log" -Append}
 
$Done = "\\$Servername\UserDataBackup$\source\Done\$Date"
if(Test-Path $Done) {
if (Test-Path "\\$Servername\UserDataBackup$\source\user.csv") {
$users = Import-CSV "\\$Servername\UserDataBackup$\source\user.csv"
ForEach ($User in $Users) {
$UserID = $User.UserID
$UserID | out-file -FilePath "\\$Servername\UserDataBackup$\source\user_second.txt" -Append
}
Move-Item -Path "\\$Servername\UserDataBackup$\source\user.csv" -Destination "$Done\user.csv"}
}
else {
New-Item -Path $Done -ItemType directory
if (Test-Path "\\$Servername\UserDataBackup$\source\user.csv") {
$users = Import-CSV "\\$Servername\UserDataBackup$\source\user.csv"
ForEach ($User in $Users) {
$UserID = $User.UserID
$UserID | out-file -FilePath "\\$Servername\UserDataBackup$\source\user_second.txt" -Append
}
Move-Item -Path "\\$Servername\UserDataBackup$\source\user.csv" -Destination "$Done\user.csv"}
}
 
if ($Time) {clear-variable -name Time}
$Time = (Get-Date).ToLongTimeString()
"$Time backupuserexport_firstrun.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
ersten_teil.txt · Last modified: 2017/01/17 10:17 by clemens