 |
|
|
Variable Count not going up
Last Post 04 Aug 2011 01:42 AM by James. 4 Replies.
|
Sort:
|
|
Prev Next |
You are not authorized to post a reply. |
|
James
 Basic Member Posts:398

 |
| 03 Aug 2011 02:36 AM |
|
Hello,
I have some code which seems to work fine I have added a count in it and does not work but it dows outside of the function... Also if I run the routine which calls it then it does not work....
My function is this:
Function DisabledUsers {</p>
<p>$Global:DisabledUsersRun = 1 </p>
<p>$Global:CountDisabled = 0</p>
<p>Write-Host " Removing Disabled Users.... " -ForegroundColor Yellow</p>
<p>$Global:DisabledUsersStart = Get-Date</p>
<p>Write-Host " "</p>
<p>Get-QADUser -Title "student", "teacher","staff","parent", "governor" `<br> -SizeLimit 0 `<br> -Disabled |<br>ForEach-Object{</p>
<p>$SamAccountName = $_.SamAccountName</p>
<p>Write-Host "Deleting User: " $SamAccountName</p>
<p>Delete-ADUser $SamAccountName</p>
<p>$CountDisabled = $CountDisabled + 1</p>
<p>}</p>
<p>Write-Host " "</p>
<p>Write-Host " Disabled Users Successfully Removed. " -ForegroundColor Yellow</p>
<p>$Global:DisabledUsersFinish = Get-Date</p>
<p>Write-Host " "<br>Write-Host " "</p>
<p>}I can run the above and it gives the correct output apart from the $CountDisabled variable does not count up... so if its run through say 3 people the count stays the same although they have been removed and its written out... However if the function above is called via the big script as a whole which is here:
# New Intake Script for new school years.</p>
<p># Set Parameters</p>
<p>Function New-Year {</p>
<p>param <br> (<br> [switch]$All,<br> [switch]$DisabledUsers,<br> [switch]$RemoveSpaces,<br> [switch]$SetTargetProxyAddress,<br> [switch]$CityCompanyInfo,<br> [switch]$NoPrep,<br> [switch]$Help <br> )</p>
<p># Set Variables</p>
<p>$Global:S = $Null</p>
<p>$Global:StartRoutine = $null<br>$Global:FinishRoutine = $null</p>
<p>$Global:DisabledUsersRun = $null <br>$Global:RemoveSpacesRun = $null <br>$Global:SetTargetProxyAddressRun = $null <br>$Global:CityCompanyInfoRun = $null </p>
<p>$Global:DisabledUsersStart = $null <br>$Global:RemoveSpacesStart = $null <br>$Global:SetTargetProxyAddressStart = $null <br>$Global:CityCompanyInfoStart = $null </p>
<p>$Global:DisabledUsersFinish = $null <br>$Global:RemoveSpacesFinish = $null <br>$Global:SetTargetProxyAddressFinish = $null <br>$Global:CityCompanyInfoFinish = $null </p>
<p>$Global:TimeRoutine = $null<br>$Global:PrepFinish = $null<br>$Global:TimeDisabledUsers = $null<br>$Global:TimeRemoveSpaces = $null<br>$Global:TimeSetTargetProxyAddress = $null<br>$Global:TimeCityCompanyInfo = $null</p>
<p>$Global:CountAddress = $Null<br>$Global:CountCity = $Null<br>$Global:CountDisabled = $Null</p>
<p>$Global:Switches = $null</p>
<p>$Global:SchoolList = Import-Csv "C:\Powershell\CSV\SchoolsList.csv"</p>
<p>Function Usage {</p>
<p>Write-Host ""<br>Write-Host "FUNCTION NAME: New-Year" -ForegroundColor Yellow<br>Write-Host ""<br>Write-Host "USAGE" -ForegroundColor Green<br>Write-Host " New-Year -All " -ForegroundColor White<br>Write-Host " New-Year -DisabledUsers" -ForegroundColor White<br>Write-Host " New-Year -DisabledUsers -RemoveSpaces" -ForegroundColor White<br>Write-Host " New-Year -DisabledUsers -RemoveSpaces -SetTargetProxyAddress" -ForegroundColor White<br>Write-Host ""<br>Write-Host "DESCRIPTION:" -ForegroundColor Green<br>Write-Host " Designed for the use of the schools team for a new intake of students, parents etc." -ForegroundColor White<br>Write-Host " It can be used for one particular operation or all operations required" -ForegroundColor White<br>Write-Host " for the new intake. It will also give you the statistics of time taken at the end." -ForegroundColor White<br>Write-Host ""<br>Write-Host "AVAILABLE SWITCHES:" -ForegroundColor Green<br>Write-Host " -All : Runs All of the stages in order" -ForegroundColor White<br>Write-Host " -DisabledUsers : Runs Disabled Users - Removing Of Disabled Users" -ForegroundColor White<br>Write-Host " -RemoveSpaces : Runs Removal Of Spaces - Removing Of Spaces In The SAMAccountName,UPN, Mail Attributes" -ForegroundColor White<br>Write-Host " -SetTargetProxyAddress : Runs Set Target And Proxy Addresses - Sets Student Target And Proxy Addresses " -ForegroundColor White<br>Write-Host " -CityCompanyInfo : Runs City And Company Info - Sets City And Company Information For Users" -ForegroundColor White<br>Write-Host " -NoPrep : Does Not Do Any Routine Prep (Used For Testing) " -ForegroundColor White<br>Write-Host ""<br>Write-Host "REQUIREMENTS:" -ForegroundColor Green<br>Write-Host " You must provide at lease one switch" -ForegroundColor White<br>Write-Host ""</p>
<p>break<br>}</p>
<p># This will run all the stages for a new school year.</p>
<p>Function All {</p>
<p>DisabledUsers<br>RemoveSpaces<br>SetTargetProxyAddress<br>CityCompanyInfo</p>
<p>} </p>
<p># This is the start of the routine before anything else is run.</p>
<p>Function RoutineStart {</p>
<p>Write-Host " "</p>
<p>Write-Host " ROUTINE START: " -ForegroundColor White</p>
<p>Write-Host " "</p>
<p>$Global:StartRoutine = Get-Date<br>" Day: " + $Global:StartRoutine.DayOfWeek<br>" Date: " + $Global:StartRoutine.ToShortDateString()<br>" Time: " + $Global:StartRoutine.ToShortTimeString()</p>
<p>Write-Host " "</p>
<p>Write-Host " Preparing Accounts... "</p>
<p>Write-Host " "</p>
<p>Get-QADUser -Title "student","teacher","staff","parent","governor" `<br> -SizeLimit 0 | <br>Set-QADUser -PasswordNeverExpires $false</p>
<p>$Global:PrepFinish = Get-Date</p>
<p>Write-Host " "<br>Write-Host " "</p>
<p>}</p>
<p># This will remove the disabled users.</p>
<p>Function DisabledUsers {</p>
<p>$Global:DisabledUsersRun = 1 </p>
<p>$Global:CountDisabled = 0</p>
<p>Write-Host " Removing Disabled Users.... " -ForegroundColor Yellow</p>
<p>$Global:DisabledUsersStart = Get-Date</p>
<p>Write-Host " "</p>
<p>Get-QADUser -Title "student", "teacher","staff","parent", "governor" `<br> -SizeLimit 0 `<br> -Disabled |<br>ForEach-Object{</p>
<p>$SamAccountName = $_.SamAccountName</p>
<p>Write-Host "Deleting User: " $SamAccountName</p>
<p>Delete-ADUser $SamAccountName</p>
<p>$CountDisabled = $CountDisabled + 1</p>
<p>}</p>
<p>Write-Host " "</p>
<p>Write-Host " Disabled Users Successfully Removed. " -ForegroundColor Yellow</p>
<p>$Global:DisabledUsersFinish = Get-Date</p>
<p>Write-Host " "<br>Write-Host " "</p>
<p>}</p>
<p># This will remove the spaces in the accounts.</p>
<p>Function RemoveSpaces {</p>
<p>$Global:RemoveSpacesRun = 1 </p>
<p># Remove Spaces</p>
<p>Write-Host " Removing Spaces From Accounts.... " -ForegroundColor Yellow</p>
<p>$Global:RemoveSpacesStart = Get-Date</p>
<p>Write-Host " "</p>
<p>$SchoolList |<br>ForEach-Object{</p>
<p>$School = $_.School<br>$ou = 'OU=users,OU=' + $School + ',OU=schools,DC=domain,DC=dot,DC=com '</p>
<p>Get-QADUser -Title "student", "teacher","staff","parent" `<br> -SearchRoot $OU `<br> -SizeLimit 0 `<br> -includeAllProperties |</p>
<p>Where-Object {$_.UserPrincipalName -match "\s" -or $_.samAccountName -match "\s" -or $_.mail -match "\s" -or $_.UserPrincipalName -match "'" -or $_.samAccountName -match "'" -or $_.mail -match "'"}|</p>
<p>ForEach-Object{</p>
<p>$Sam = $_.SamAccountName<br>$Sam1 = $Sam -replace " ",""<br>$NewSam = $Sam1 -replace "'",""</p>
<p>$UPN = $_.UserPrincipalName<br>$UPN1 = $UPN -replace " ",""<br>$NewUPN = $UPN1 -replace "'",""</p>
<p>$Mail = $_.Mail<br>$Mail1 = $Mail -replace " ",""<br>$NewMail = $Mail1 -replace "'",""</p>
<p>Set-QADUser -Identity $Sam `<br> -ObjectAttributes @{Mail = $NewMail} `<br> -UserPrincipalName $NewUPN `<br> -SamAccountName $NewSam<br>}<br>}</p>
<p>Write-Host " "</p>
<p>Write-Host " Spaces Successfully Removed. " -ForegroundColor Yellow</p>
<p>$Global:RemoveSpacesFinish = Get-Date</p>
<p>Write-Host " "<br>Write-Host " "</p>
<p>}</p>
<p># This will set the TargetAddress and ProxyAddresses Attributes of the students.</p>
<p>Function SetTargetProxyAddress {</p>
<p>$Global:SetTargetProxyAddressRun = 1 </p>
<p>Write-Host " Setting Student Proxy and Target Addresses.... " -ForegroundColor Yellow</p>
<p>$Global:SetTargetProxyAddressStart = Get-Date<br>$Global:CountAddress = 0</p>
<p>Write-Host " "</p>
<p># Assign Target Address and Proxy Address to Students</p>
<p>$SchoolList |<br>ForEach-Object{</p>
<p>$School = $_.School<br>$ou = 'OU=users,OU=' + $School + ',OU=schools,DC=domain,DC=dot,DC=com'</p>
<p>Get-QADUser -Title "student" `<br> -SearchRoot $OU `<br> -IncludedProperties TargetAddress, ProxyAddresses `<br> -SizeLimit 0 | <br>where{$_.TargetAddress -or $_.ProxyAddresses -eq $Null} | <br>ForEach-Object{</p>
<p>$TargetAddress = 'smtp:' + $_.samAccountName + <a href="mailto:'@outlooklive.sseln.org.uk'">'@outlooklive.sseln.org.uk'</a><br>$ProxyAddress = 'SMTP:' + $_.samAccountName + <a href="mailto:'@'">'@'</a> + $School + '.domain.com'</p>
<p>Set-QADUser -Identity $_.Name `<br> -PasswordNeverExpires $false `<br> -ObjectAttributes @{TargetAddress=$TargetAddress ; ProxyAddresses=$ProxyAddress }</p>
<p>$CountAddress = $CountAddress + 1</p>
<p>}<br>}</p>
<p>Write-Host " "</p>
<p>Write-Host " Target and Proxy Addresses Successfully Written. " -ForegroundColor Yellow</p>
<p>$Global:SetTargetProxyAddressFinish = Get-Date</p>
<p>Write-Host " "<br>Write-Host " "</p>
<p>}</p>
<p># This will set the City and Company Information for the users.</p>
<p>Function CityCompanyInfo {</p>
<p>$Global:CityCompanyInfoRun = 1 </p>
<p>Write-Host " Setting City And Company Information.... " -ForegroundColor Yellow</p>
<p>$Global:CityCompanyInfoStart = Get-Date<br>$Global:CountCity = 0</p>
<p>Write-Host " "</p>
<p># Write City And Company Information</p>
<p>$SchoolList|<br>ForEach-Object{</p>
<p>$School = $_.School<br>$ou = 'OU=users,OU=' + $School + ',OU=schools,DC=domain,DC=dot,DC=com '</p>
<p>Get-QADUser -SearchRoot $OU `<br> -IncludedProperties company `<br> -sizeLimit 0 |<br>where{$_.Company -eq $Null} | </p>
<p>ForEach-Object{</p>
<p>$Sam = $_.SamAccountName</p>
<p>Set-QADUser -Identity $Sam `<br> -Company $School `<br> -City "Stockport"</p>
<p>$CountCity = $CountCity + 1</p>
<p>}<br>}</p>
<p>Write-Host " "</p>
<p>Write-Host " City And Company Information Successfully Written. " -ForegroundColor Yellow</p>
<p>$Global:CityCompanyInfoFinish = Get-Date</p>
<p>Write-Host " "<br>Write-Host " "</p>
<p>}</p>
<p># This will finish the routine and give out statistics for how long each part run for.</p>
<p>Function RoutineFinish {<br>Write-Host " Routine Complete! " -ForegroundColor White</p>
<p>Write-Host " "<br>Write-Host " "</p>
<p>Write-Host " ROUTINE END: " -ForegroundColor White</p>
<p>Write-Host " "</p>
<p>$Global:FinishRoutine = Get-Date<br>" Day: " + $Global:FinishRoutine.DayOfWeek<br>" Date: " + $Global:FinishRoutine.ToShortDateString()<br>" Time: " + $Global:FinishRoutine.ToShortTimeString()</p>
<p>Write-Host " "<br>Write-Host " "</p>
<p>Write-Host " Routine Statistics: "<br>Write-Host " ------------------- "</p>
<p>Write-Host " "<br>Write-Host " "</p>
<p> # Work Out Time Taken<br> <br>$TimeRoutine = New-TimeSpan -Start $StartRoutine -End $FinishRoutine<br>$TimePrep = New-TimeSpan -Start $StartRoutine -End $PrepFinish<br>if ($DisabledUsers -or $All) { $TimeDisabledUsers = New-TimeSpan -Start $DisabledUsersStart -End $DisabledUsersFinish }<br>if ($RemoveSpaces -or $All) { $TimeRemoveSpaces = New-TimeSpan -Start $RemoveSpacesStart -End $RemoveSpacesFinish }<br>if ($SetTargetProxyAddress -or $All) { $TimeSetTargetProxyAddress = New-TimeSpan -Start $SetTargetProxyAddressStart -End $SetTargetProxyAddressFinish }<br>if ($CityCompanyInfo -or $All) { $TimeCityCompanyInfo = New-TimeSpan -Start $CityCompanyInfoStart -End $CityCompanyInfoFinish }</p>
<p># Give Statistics</p>
<p>Write-Host " Routine Start: $($StartRoutine.DayOfWeek) $($StartRoutine)"</p>
<p>Write-Host " "<br>Write-Host " "</p>
<p>Write-Host " User Preperation Complete Time: $([string]$TimePrep.Days) Days $([string]$TimePrep.Hours) Hours $([string]$TimePrep.Minutes) Minutes $([string]$TimePrep.Seconds) Seconds"</p>
<p>Write-Host " "</p>
<p>If($DisabledUsersRun -ne "1") { </p>
<p>Write-Host " "<br>Write-Host " Statistics For Disabled Users Not Available (Disabled Users Switch Was Not Selected)" <br>Write-Host " "<br>} else { </p>
<p>Write-Host " "<br>Write-Host " Disabled Users Start: $($DisabledUsersStart.DayOfWeek) $($DisabledUsersStart)"<br>Write-Host " Disabled Users Finish: $($DisabledUsersFinish.DayOfWeek) $($DisabledUsersFinish)"<br>Write-Host " Disabled Users Complete Time: $([string]$TimeDisabledUsers.Days) Days $([string]$TimeDisabledUsers.Hours) Hours $([string]$TimeDisabledUsers.Minutes) Minutes $([string]$TimeDisabledUsers.Seconds) Seconds"<br>Write-Host " "</p>
<p>Write-Host " No Of Users Deleted: $([string]$CountDisabled)"<br>Write-Host " "</p>
<p>} </p>
<p>If($RemoveSpacesRun -ne "1") { </p>
<p>Write-Host " "<br>Write-Host " Statistics For Remove Spaces Not Available (Removal Of Spaces Was Not Selected)" <br>Write-Host " "<br>} else { </p>
<p>Write-Host " "<br>Write-Host " Removal Of Spaces Start: $($RemoveSpacesStart.DayOfWeek) $($RemoveSpacesStart)"<br>Write-Host " Removal Of Spaces Finish: $($RemoveSpacesFinish.DayOfWeek) $($RemoveSpacesFinish)"<br>Write-Host " Removal Of Spaces Complete Time: $([string]$TimeRemoveSpaces.Days) Days $([string]$TimeRemoveSpaces.Hours) Hours $([string]$TimeRemoveSpaces.Minutes) Minutes $([string]$TimeRemoveSpaces.Seconds) Seconds"<br>Write-Host " "</p>
<p>}</p>
<p>If($SetTargetProxyAddressRun -ne "1") { </p>
<p>Write-Host " "<br>Write-Host " Statistics For Proxy And Target Addresses Not Available (Proxy And Target Addresses Was Not Selected)" <br>Write-Host " "<br>} else { </p>
<p>Write-Host " "<br>Write-Host " Set Target And Proxy Addresses Start: $($SetTargetProxyAddressStart.DayOfWeek) $($SetTargetProxyAddressStart)"<br>Write-Host " Set Target And Proxy Addresses Finish: $($SetTargetProxyAddressFinish.DayOfWeek) $($SetTargetProxyAddressFinish)"<br>Write-Host " Set Target And Proxy Addresses Complete Time: $([string]$TimeSetTargetProxyAddress.Days) Days $([string]$TimeSetTargetProxyAddress.Hours) Hours $([string]$TimeSetTargetProxyAddress.Minutes) Minutes $([string]$TimeSetTargetProxyAddress.Seconds) Seconds"<br>Write-Host " "</p>
<p>Write-Host " No Of Users Modified: $([string]$CountAddress)"<br>Write-Host " "<br>}</p>
<p>If($CityCompanyInfoRun -ne "1") { </p>
<p>Write-Host " "<br>Write-Host " Statistics For City And Company Info Not Available (City And Company Info Was Not Selected)" <br>Write-Host " "<br>} else { </p>
<p>Write-Host " "<br>Write-Host " Set City And Company Info Start: $($CityCompanyInfoStart.DayOfWeek) $($CityCompanyInfoStart)"<br>Write-Host " Set City And Company Info Finish: $($CityCompanyInfoFinish.DayOfWeek) $($CityCompanyInfoFinish)"<br>Write-Host " Set City And Company Info Complete Time: $([string]$TimeCityCompanyInfo.Days) Days $([string]$TimeCityCompanyInfo.Hours) Hours $([string]$TimeCityCompanyInfo.Minutes) Minutes $([string]$TimeCityCompanyInfo.Seconds) Seconds"<br>Write-Host " "</p>
<p>Write-Host " No Of Users Modified: $([string]$CountCity)"<br>Write-Host " "</p>
<p>}</p>
<p>Write-Host " "<br>Write-Host " Routine Finish: $($FinishRoutine.DayOfWeek) $($FinishRoutine)"<br>Write-Host " "<br>Write-Host " "<br>Write-Host " Routine Complete Time: $([string]$TimeRoutine.Days) Days $([string]$TimeRoutine.Hours) Hours $([string]$TimeRoutine.Minutes) Minutes $([string]$TimeRoutine.Seconds) Seconds"<br>Write-Host " "</p>
<p>Write-Host " "</p>
<p>Write-Host " End Of Routine Statistics"</p>
<p>Write-Host " "<br>Write-Host " "</p>
<p>}</p>
<p># These are some traps to get the user to use a switch and check which switch has been used.</p>
<p>if (!($All -or $DisabledUsers -or $RemoveSpaces -or $SetTargetProxyAddress -or $CityCompanyInfo -or $Help)) <br>{<br>Write-Host " "<br>Write-Host "Please Select A Switch"<br>Write-Host " " ; break} else {<br>if($All) {$Global:S += "All "} <br>if($DisabledUsers) {$Global:S += "DisabledUsers "}<br>if($RemoveSpaces) {$Global:S += "RemoveSpaces "}<br>if($SetTargetProxyAddress) {$Global:S += "SetTargetProxyAddress "}<br>if($CityCompanyInfo) {$Global:S += "CityCompanyInfo "}<br>if($Help) { Usage break }<br>}</p>
<p># These run the switches that have been selected.</p>
<p>RoutineStart</p>
<p>if($S -match "All") {All} <br>if($S -match "DisabledUsers") {DisabledUsers}<br>if($S -match "RemoveSpaces") {RemoveSpaces}<br>if($S -match "SetTargetProxyAddress") {SetTargetProxyAddress}<br>if($S -match "CityCompanyInfo") {CityCompanyInfo}</p>
<p>RoutineFinish</p>
<p>}<br>So the start routine to prep the accounts runs and then calls disabled users (once selected) and then it plods through. It then gets to the function writes out that its removing disabled users and then says its complete and doesnt actually delete anything for some odd reason does anyone know why? As a side note I am putting a new switch which is noprep which currently does not work as I have only added the switch and help so far so is a work in progress but it didnt work before that was put in. Any help given would be much appreciated. Many Thanks James |
|
|
|
|
James
 Basic Member Posts:398

 |
|
James
 Basic Member Posts:398

 |
| 03 Aug 2011 02:39 AM |
|
Sorry about that the code screwed up and I pressed submit! The function:
<br /> <br /> Here is the full script: <br /> <br /> [CODE]# New Intake Script for new school years.
# Set Parameters
Function New-Year {
param
(
[switch]$All,
[switch]$DisabledUsers,
[switch]$RemoveSpaces,
[switch]$SetTargetProxyAddress,
[switch]$CityCompanyInfo,
[switch]$NoPrep,
[switch]$Help
)
# Set Variables
$Global:S = $Null
$Global:StartRoutine = $null
$Global:FinishRoutine = $null
$Global:DisabledUsersRun = $null
$Global:RemoveSpacesRun = $null
$Global:SetTargetProxyAddressRun = $null
$Global:CityCompanyInfoRun = $null
$Global:DisabledUsersStart = $null
$Global:RemoveSpacesStart = $null
$Global:SetTargetProxyAddressStart = $null
$Global:CityCompanyInfoStart = $null
$Global:DisabledUsersFinish = $null
$Global:RemoveSpacesFinish = $null
$Global:SetTargetProxyAddressFinish = $null
$Global:CityCompanyInfoFinish = $null
$Global:TimeRoutine = $null
$Global:PrepFinish = $null
$Global:TimeDisabledUsers = $null
$Global:TimeRemoveSpaces = $null
$Global:TimeSetTargetProxyAddress = $null
$Global:TimeCityCompanyInfo = $null
$Global:CountAddress = $Null
$Global:CountCity = $Null
$Global:CountDisabled = $Null
$Global:Switches = $null
$Global:SchoolList = Import-Csv "C:\Powershell\CSV\SchoolsList.csv"
Function Usage {
Write-Host ""
Write-Host "FUNCTION NAME: New-Year" -ForegroundColor Yellow
Write-Host ""
Write-Host "USAGE" -ForegroundColor Green
Write-Host " New-Year -All " -ForegroundColor White
Write-Host " New-Year -DisabledUsers" -ForegroundColor White
Write-Host " New-Year -DisabledUsers -RemoveSpaces" -ForegroundColor White
Write-Host " New-Year -DisabledUsers -RemoveSpaces -SetTargetProxyAddress" -ForegroundColor White
Write-Host ""
Write-Host "DESCRIPTION:" -ForegroundColor Green
Write-Host " Designed for the use of the schools team for a new intake of students, parents etc." -ForegroundColor White
Write-Host " It can be used for one particular operation or all operations required" -ForegroundColor White
Write-Host " for the new intake. It will also give you the statistics of time taken at the end." -ForegroundColor White
Write-Host ""
Write-Host "AVAILABLE SWITCHES:" -ForegroundColor Green
Write-Host " -All : Runs All of the stages in order" -ForegroundColor White
Write-Host " -DisabledUsers : Runs Disabled Users - Removing Of Disabled Users" -ForegroundColor White
Write-Host " -RemoveSpaces : Runs Removal Of Spaces - Removing Of Spaces In The SAMAccountName,UPN, Mail Attributes" -ForegroundColor White
Write-Host " -SetTargetProxyAddress : Runs Set Target And Proxy Addresses - Sets Student Target And Proxy Addresses " -ForegroundColor White
Write-Host " -CityCompanyInfo : Runs City And Company Info - Sets City And Company Information For Users" -ForegroundColor White
Write-Host " -NoPrep : Does Not Do Any Routine Prep (Used For Testing) " -ForegroundColor White
Write-Host ""
Write-Host "REQUIREMENTS:" -ForegroundColor Green
Write-Host " You must provide at lease one switch" -ForegroundColor White
Write-Host ""
break
}
# This will run all the stages for a new school year.
Function All {
DisabledUsers
RemoveSpaces
SetTargetProxyAddress
CityCompanyInfo
}
# This is the start of the routine before anything else is run.
Function RoutineStart {
Write-Host " "
Write-Host " ROUTINE START: " -ForegroundColor White
Write-Host " "
$Global:StartRoutine = Get-Date
" Day: " + $Global:StartRoutine.DayOfWeek
" Date: " + $Global:StartRoutine.ToShortDateString()
" Time: " + $Global:StartRoutine.ToShortTimeString()
Write-Host " "
Write-Host " Preparing Accounts... "
Write-Host " "
Get-QADUser -Title "student","teacher","staff","parent","governor" `
-SizeLimit 0 |
Set-QADUser -PasswordNeverExpires $false
$Global:PrepFinish = Get-Date
Write-Host " "
Write-Host " "
}
# This will remove the disabled users.
Function DisabledUsers {
$Global:DisabledUsersRun = 1
$Global:CountDisabled = 0
Write-Host " Removing Disabled Users.... " -ForegroundColor Yellow
$Global:DisabledUsersStart = Get-Date
Write-Host " "
Get-QADUser -Title "student", "teacher","staff","parent", "governor" `
-SizeLimit 0 `
-Disabled |
ForEach-Object{
$SamAccountName = $_.SamAccountName
Write-Host "Deleting User: " $SamAccountName
Delete-ADUser $SamAccountName
$CountDisabled = $CountDisabled + 1
}
Write-Host " "
Write-Host " Disabled Users Successfully Removed. " -ForegroundColor Yellow
$Global:DisabledUsersFinish = Get-Date
Write-Host " "
Write-Host " "
}
# This will remove the spaces in the accounts.
Function RemoveSpaces {
$Global:RemoveSpacesRun = 1
# Remove Spaces
Write-Host " Removing Spaces From Accounts.... " -ForegroundColor Yellow
$Global:RemoveSpacesStart = Get-Date
Write-Host " "
$SchoolList |
ForEach-Object{
$School = $_.School
$ou = 'OU=users,OU=' + $School + ',OU=schools,DC=domain,DC=dot,DC=com '
Get-QADUser -Title "student", "teacher","staff","parent" `
-SearchRoot $OU `
-SizeLimit 0 `
-includeAllProperties |
Where-Object {$_.UserPrincipalName -match "\s" -or $_.samAccountName -match "\s" -or $_.mail -match "\s" -or $_.UserPrincipalName -match "'" -or $_.samAccountName -match "'" -or $_.mail -match "'"}|
ForEach-Object{
$Sam = $_.SamAccountName
$Sam1 = $Sam -replace " ",""
$NewSam = $Sam1 -replace "'",""
$UPN = $_.UserPrincipalName
$UPN1 = $UPN -replace " ",""
$NewUPN = $UPN1 -replace "'",""
$Mail = $_.Mail
$Mail1 = $Mail -replace " ",""
$NewMail = $Mail1 -replace "'",""
Set-QADUser -Identity $Sam `
-ObjectAttributes @{Mail = $NewMail} `
-UserPrincipalName $NewUPN `
-SamAccountName $NewSam
}
}
Write-Host " "
Write-Host " Spaces Successfully Removed. " -ForegroundColor Yellow
$Global:RemoveSpacesFinish = Get-Date
Write-Host " "
Write-Host " "
}
# This will set the TargetAddress and ProxyAddresses Attributes of the students.
Function SetTargetProxyAddress {
$Global:SetTargetProxyAddressRun = 1
Write-Host " Setting Student Proxy and Target Addresses.... " -ForegroundColor Yellow
$Global:SetTargetProxyAddressStart = Get-Date
$Global:CountAddress = 0
Write-Host " "
# Assign Target Address and Proxy Address to Students
$SchoolList |
ForEach-Object{
$School = $_.School
$ou = 'OU=users,OU=' + $School + ',OU=schools,DC=domain,DC=dot,DC=com'
Get-QADUser -Title "student" `
-SearchRoot $OU `
-IncludedProperties TargetAddress, ProxyAddresses `
-SizeLimit 0 |
where{$_.TargetAddress -or $_.ProxyAddresses -eq $Null} |
ForEach-Object{
$TargetAddress = 'smtp:' + $_.samAccountName + '@outlooklive.sseln.org.uk'
$ProxyAddress = 'SMTP:' + $_.samAccountName + '@' + $School + '.domain.com'
Set-QADUser -Identity $_.Name `
-PasswordNeverExpires $false `
-ObjectAttributes @{TargetAddress=$TargetAddress ; ProxyAddresses=$ProxyAddress }
$CountAddress = $CountAddress + 1
}
}
Write-Host " "
Write-Host " Target and Proxy Addresses Successfully Written. " -ForegroundColor Yellow
$Global:SetTargetProxyAddressFinish = Get-Date
Write-Host " "
Write-Host " "
}
# This will set the City and Company Information for the users.
Function CityCompanyInfo {
$Global:CityCompanyInfoRun = 1
Write-Host " Setting City And Company Information.... " -ForegroundColor Yellow
$Global:CityCompanyInfoStart = Get-Date
$Global:CountCity = 0
Write-Host " "
# Write City And Company Information
$SchoolList|
ForEach-Object{
$School = $_.School
$ou = 'OU=users,OU=' + $School + ',OU=schools,DC=domain,DC=dot,DC=com '
Get-QADUser -SearchRoot $OU `
-IncludedProperties company `
-sizeLimit 0 |
where{$_.Company -eq $Null} |
ForEach-Object{
$Sam = $_.SamAccountName
Set-QADUser -Identity $Sam `
-Company $School `
-City "Stockport"
$CountCity = $CountCity + 1
}
}
Write-Host " "
Write-Host " City And Company Information Successfully Written. " -ForegroundColor Yellow
$Global:CityCompanyInfoFinish = Get-Date
Write-Host " "
Write-Host " "
}
# This will finish the routine and give out statistics for how long each part run for.
Function RoutineFinish {
Write-Host " Routine Complete! " -ForegroundColor White
Write-Host " "
Write-Host " "
Write-Host " ROUTINE END: " -ForegroundColor White
Write-Host " "
$Global:FinishRoutine = Get-Date
" Day: " + $Global:FinishRoutine.DayOfWeek
" Date: " + $Global:FinishRoutine.ToShortDateString()
" Time: " + $Global:FinishRoutine.ToShortTimeString()
Write-Host " "
Write-Host " "
Write-Host " Routine Statistics: "
Write-Host " ------------------- "
Write-Host " "
Write-Host " "
# Work Out Time Taken
$TimeRoutine = New-TimeSpan -Start $StartRoutine -End $FinishRoutine
$TimePrep = New-TimeSpan -Start $StartRoutine -End $PrepFinish
if ($DisabledUsers -or $All) { $TimeDisabledUsers = New-TimeSpan -Start $DisabledUsersStart -End $DisabledUsersFinish }
if ($RemoveSpaces -or $All) { $TimeRemoveSpaces = New-TimeSpan -Start $RemoveSpacesStart -End $RemoveSpacesFinish }
if ($SetTargetProxyAddress -or $All) { $TimeSetTargetProxyAddress = New-TimeSpan -Start $SetTargetProxyAddressStart -End $SetTargetProxyAddressFinish }
if ($CityCompanyInfo -or $All) { $TimeCityCompanyInfo = New-TimeSpan -Start $CityCompanyInfoStart -End $CityCompanyInfoFinish }
# Give Statistics
Write-Host " Routine Start: $($StartRoutine.DayOfWeek) $($StartRoutine)"
Write-Host " "
Write-Host " "
Write-Host " User Preperation Complete Time: $([string]$TimePrep.Days) Days $([string]$TimePrep.Hours) Hours $([string]$TimePrep.Minutes) Minutes $([string]$TimePrep.Seconds) Seconds"
Write-Host " "
If($DisabledUsersRun -ne "1") {
Write-Host " "
Write-Host " Statistics For Disabled Users Not Available (Disabled Users Switch Was Not Selected)"
Write-Host " "
} else {
Write-Host " "
Write-Host " Disabled Users Start: $($DisabledUsersStart.DayOfWeek) $($DisabledUsersStart)"
Write-Host " Disabled Users Finish: $($DisabledUsersFinish.DayOfWeek) $($DisabledUsersFinish)"
Write-Host " Disabled Users Complete Time: $([string]$TimeDisabledUsers.Days) Days $([string]$TimeDisabledUsers.Hours) Hours $([string]$TimeDisabledUsers.Minutes) Minutes $([string]$TimeDisabledUsers.Seconds) Seconds"
Write-Host " "
Write-Host " No Of Users Deleted: $([string]$CountDisabled)"
Write-Host " "
}
If($RemoveSpacesRun -ne "1") {
Write-Host " "
Write-Host " Statistics For Remove Spaces Not Available (Removal Of Spaces Was Not Selected)"
Write-Host " "
} else {
Write-Host " "
Write-Host " Removal Of Spaces Start: $($RemoveSpacesStart.DayOfWeek) $($RemoveSpacesStart)"
Write-Host " Removal Of Spaces Finish: $($RemoveSpacesFinish.DayOfWeek) $($RemoveSpacesFinish)"
Write-Host " Removal Of Spaces Complete Time: $([string]$TimeRemoveSpaces.Days) Days $([string]$TimeRemoveSpaces.Hours) Hours $([string]$TimeRemoveSpaces.Minutes) Minutes $([string]$TimeRemoveSpaces.Seconds) Seconds"
Write-Host " "
}
If($SetTargetProxyAddressRun -ne "1") {
Write-Host " "
Write-Host " Statistics For Proxy And Target Addresses Not Available (Proxy And Target Addresses Was Not Selected)"
Write-Host " "
} else {
Write-Host " "
Write-Host " Set Target And Proxy Addresses Start: $($SetTargetProxyAddressStart.DayOfWeek) $($SetTargetProxyAddressStart)"
Write-Host " Set Target And Proxy Addresses Finish: $($SetTargetProxyAddressFinish.DayOfWeek) $($SetTargetProxyAddressFinish)"
Write-Host " Set Target And Proxy Addresses Complete Time: $([string]$TimeSetTargetProxyAddress.Days) Days $([string]$TimeSetTargetProxyAddress.Hours) Hours $([string]$TimeSetTargetProxyAddress.Minutes) Minutes $([string]$TimeSetTargetProxyAddress.Seconds) Seconds"
Write-Host " "
Write-Host " No Of Users Modified: $([string]$CountAddress)"
Write-Host " "
}
If($CityCompanyInfoRun -ne "1") {
Write-Host " "
Write-Host " Statistics For City And Company Info Not Available (City And Company Info Was Not Selected)"
Write-Host " "
} else {
Write-Host " "
Write-Host " Set City And Company Info Start: $($CityCompanyInfoStart.DayOfWeek) $($CityCompanyInfoStart)"
Write-Host " Set City And Company Info Finish: $($CityCompanyInfoFinish.DayOfWeek) $($CityCompanyInfoFinish)"
Write-Host " Set City And Company Info Complete Time: $([string]$TimeCityCompanyInfo.Days) Days $([string]$TimeCityCompanyInfo.Hours) Hours $([string]$TimeCityCompanyInfo.Minutes) Minutes $([string]$TimeCityCompanyInfo.Seconds) Seconds"
Write-Host " "
Write-Host " No Of Users Modified: $([string]$CountCity)"
Write-Host " "
}
Write-Host " "
Write-Host " Routine Finish: $($FinishRoutine.DayOfWeek) $($FinishRoutine)"
Write-Host " "
Write-Host " "
Write-Host " Routine Complete Time: $([string]$TimeRoutine.Days) Days $([string]$TimeRoutine.Hours) Hours $([string]$TimeRoutine.Minutes) Minutes $([string]$TimeRoutine.Seconds) Seconds"
Write-Host " "
Write-Host " "
Write-Host " End Of Routine Statistics"
Write-Host " "
Write-Host " "
}
# These are some traps to get the user to use a switch and check which switch has been used.
if (!($All -or $DisabledUsers -or $RemoveSpaces -or $SetTargetProxyAddress -or $CityCompanyInfo -or $Help))
{
Write-Host " "
Write-Host "Please Select A Switch"
Write-Host " " ; break} else {
if($All) {$Global:S += "All "}
if($DisabledUsers) {$Global:S += "DisabledUsers "}
if($RemoveSpaces) {$Global:S += "RemoveSpaces "}
if($SetTargetProxyAddress) {$Global:S += "SetTargetProxyAddress "}
if($CityCompanyInfo) {$Global:S += "CityCompanyInfo "}
if($Help) { Usage break }
}
# These run the switches that have been selected.
RoutineStart
if($S -match "All") {All}
if($S -match "DisabledUsers") {DisabledUsers}
if($S -match "RemoveSpaces") {RemoveSpaces}
if($S -match "SetTargetProxyAddress") {SetTargetProxyAddress}
if($S -match "CityCompanyInfo") {CityCompanyInfo}
RoutineFinish
}
Any help is greatly appreciated. Many Thanks James |
|
|
|
|
Marco Shaw
 Veteran Member Posts:1684

 |
| 03 Aug 2011 03:38 AM |
|
Sorry, there's a lot of code there. If you define your variable counter like this: $Global:CountDisabled = 0 You need to refer to it *every time* like this: $Global:CountDisabled = $Global:CountDisabled + 1 or simply: $Global:CountDisabled++ Use global with care. Sometimes it isn't actually required because of how child and parent scopes work with variables... |
|
|
|
|
James
 Basic Member Posts:398

 |
| 04 Aug 2011 01:42 AM |
|
Hello, Thats works perfectly for both the function and when running in the routine! I have also put in my no prep switch which is working fine too! Just binned just over 5000 accounts! So all good! Many Thanks for your help James |
|
|
|
|
| You are not authorized to post a reply. |
|
Active Forums 4.3
|
|
 |