Churchers Posted November 14, 2016 Posted November 14, 2016 I have devised a system in our school that works well on allowing staff to enable / disable exam accounts by time (booking) the attached script runs in a loop looking for new .exam files If someone want to use it, please let me know ! happy to help setup, but if no one wants it then i'm not writing it up for external use. Please also find attached ZIP with all files required $Str_ExamFileLocation="C:\ExamManager\Exams" #Specify exam file location. $Str_EmailServer="relay.domain.com" #Specify SMTP server $Str_EmailFrom="[email protected]" #Specify email address to send from $Str_Emailto="[email protected]" #Specify email address to send report to. Import-Module ActiveDirectory <#This Function completes all tasks#> function FuncCommands($action,$group,$email,$MoveOU){ $FuncLoadedFile = Get-Content "$($Str_ExamFileLocation)\$($Str_Currentfile + $action + $group).exam" #Get content from file. foreach ($FuncLoadedline in $FuncLoadedFile) #Loop per line of content collected. { IF ($MoveOU -ne "") {Get-ADUser $FuncLoadedline | Move-ADObject -TargetPath $MoveOU} #IF Function '$MoveOU' has a value, complete the command to move OU,else leave in current OU. IF ($action -eq "D") {Disable-ADAccount -Identity $FuncLoadedline} #IF Function '$action' is D then disable account(s). IF ($action -eq "E") {Enable-ADAccount -Identity $FuncLoadedline} #IF Function '$action' is E then Enable account(s). $Str_EmailList = $Str_EmailList + $FuncLoadedline + "`r`n" } IF ($email -eq "Y") { IF ($action -eq "D") { $Str_EmailSubject = IF($Group -eq "9") {"Normal Accounts Disabled"} ELSE {"Exam Accounts Disabled"} $Str_EmailBody = "The following accounts have been disabled." + "`r`n" + $Str_EmailList } ELSE { $Str_EmailSubject = IF($Group -eq "9") {"Normal Accounts Enabled"} ELSE {"Exam Accounts Enabled"} $Str_EmailBody = "The following accounts have been enabled." + "`r`n" + $Str_EmailList } Send-MailMessage -SMTPServer $Str_EmailServer -From $Str_EmailFrom -To $Str_Emailto -Subject $Str_EmailSubject -Body $Str_EmailBody } Rename-Item "$($Str_ExamFileLocation)\$($Str_Currentfile + $action + $group).exam" "COMPLETE-$($Str_Currentfile + $action + $group).exam" #Rename the file to show complete. } <#This loop gets the current date/time and looks for the file for processing#> while($true) { #Loop forever. CLS ECHO "Running" $Str_Currentfile = $(Get-Date -format "yyyyMMdd") + "_" + $(Get-Date -format "HHmm") #(Get date) + (add "_") + (Get Time) #$Str_Currentfile = "20160201_0600" #Override for testing ECHO "Looking for stamp $Str_Currentfile" #Disable List IF (Test-Path "$($Str_ExamFileLocation)\$($Str_Currentfile)D1.exam"){FuncCommands -action "D" -group "1" -email "Y" -MoveOU "OU=Controlled Assessment Accounts,OU=Controlled Assessment,OU=SS,OU=Pupils,OU=Users,OU=Churcher's,DC=domain,DC=local"} IF (Test-Path "$($Str_ExamFileLocation)\$($Str_Currentfile)D2.exam"){FuncCommands -action "D" -group "2" -email "Y" -MoveOU "OU=Controlled Assessment Accounts,OU=Controlled Assessment,OU=SS,OU=Pupils,OU=Users,OU=Churcher's,DC=domain,DC=local"} IF (Test-Path "$($Str_ExamFileLocation)\$($Str_Currentfile)D3.exam"){FuncCommands -action "D" -group "3" -email "Y" -MoveOU "OU=Controlled Assessment Accounts,OU=Controlled Assessment,OU=SS,OU=Pupils,OU=Users,OU=Churcher's,DC=domain,DC=local"} IF (Test-Path "$($Str_ExamFileLocation)\$($Str_Currentfile)D9.exam"){FuncCommands -action "D" -group "9" -email "Y" -MoveOU ""} #Enable List IF (Test-Path "$($Str_ExamFileLocation)\$($Str_Currentfile)E1.exam"){FuncCommands -action "E" -group "1" -email "Y" -MoveOU "OU=No Restrictions,OU=Controlled Assessment,OU=SS,OU=Pupils,OU=Users,OU=Churcher's,DC=domain,DC=local"} IF (Test-Path "$($Str_ExamFileLocation)\$($Str_Currentfile)E2.exam"){FuncCommands -action "E" -group "2" -email "Y" -MoveOU "OU=No Internet,OU=Controlled Assessment,OU=SS,OU=Pupils,OU=Users,OU=Churcher's,DC=domain,DC=local"} IF (Test-Path "$($Str_ExamFileLocation)\$($Str_Currentfile)E3.exam"){FuncCommands -action "E" -group "3" -email "Y" -MoveOU "OU=No Internet and No Office,OU=Controlled Assessment,OU=SS,OU=Pupils,OU=Users,OU=Churcher's,DC=domain,DC=local"} IF (Test-Path "$($Str_ExamFileLocation)\$($Str_Currentfile)E9.exam"){FuncCommands -action "E" -group "9" -email "Y" -MoveOU ""} ECHO "Waiting to check again..." ECHO "$Str_Currentfile" | Out-File $Str_ExamFileLocation\LastRun.log #Write log file to monitor script running. start-sleep -seconds 50 } ExamManager.zip
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now