Skip to content

Commit 20474bd

Browse files
Roth, Jason (IHG)Roth, Jason (IHG)
Roth, Jason (IHG)
authored and
Roth, Jason (IHG)
committed
cleanup
2 parents f9debd8 + 4c239ed commit 20474bd

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

Set-CloudAttribute10.ps1

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Set earliest date for user creation
2+
3+
$Date = (Get-Date).AddDays(-7)
4+
5+
# Search for enabled accounts created since $Date
6+
7+
$Users = Get-IHGUser * -Enabled | where Created -GT $Date
8+
9+
# Configure Logging
10+
11+
$LogPath = "$env:SystemDrive\logs\Set-CloudAttribute10"
12+
$LogFile = (Get-Date -Format yyyy_MM_dd)+"_Set-CloudAttribute10.csv"
13+
$ErrorLog = (Get-Date -Format yyyy_MM_dd)+"_Set-CloudAttribute10_Errors.log"
14+
15+
# Create logging directory
16+
17+
if (-not (Test-Path $LogPath)) {
18+
New-Item -ItemType Directory -Path $LogPath -Force | Out-Null
19+
}
20+
21+
# Iterate through user accounts
22+
23+
foreach ($User in $Users) {
24+
25+
# Create variables to use with Set-ADUser commandlet
26+
27+
$Domain = ($User.UserPrincipalName).split('@')[1]
28+
try {
29+
$NewAttrib10 = 'CN='+$User.SamAccountName+'/O='+($User.UserPrincipalName.Split('@')[1]).split('.')[0]
30+
}
31+
catch {
32+
33+
# Write Failures to error log
34+
35+
$Message = (Get-Date -Format HH:mm:ss).ToString()+" : Unable to determine new msDS-cloudExtensionAttribute10 for $User"
36+
Write-Verbose $Message
37+
$Message | Out-File -Append -FilePath $LogPath\$ErrorLog
38+
}
39+
40+
# Change the msDS-cloudExtensionAttribute10 if it is not correctly set
41+
42+
if ($User.'msDS-cloudExtensionAttribute10' -notlike $NewAttrib10) {
43+
try {
44+
Set-ADUser -Server $Domain -Identity $User.SamAccountname -Replace @{'MSDS-CloudExtensionAttribute10'=$NewAttrib10} -Verbose
45+
}
46+
catch {
47+
48+
# Write Failures to error log
49+
50+
$Message = (Get-Date -Format HH:mm:ss).ToString()+" : Failed to set MSDS-CloudExtensionAttribute10 for $($User.UserPrincipalName)"
51+
Write-Verbose $Message
52+
$Message | Out-File -Append -FilePath $LogPath\$ErrorLog
53+
}
54+
55+
#Create custom object to export to log
56+
57+
$Object = [PSCustomObject] @{
58+
UserPrincipalName = $User.UserPrincipalName
59+
'PreviousMSDS-CloudExtensionAttribute10' = $User.'msDS-cloudExtensionAttribute10'
60+
'NewMSDS-CloudExtensionAttribute10' = $NewAttrib10
61+
Created = $User.Created
62+
}
63+
64+
# Export custom object to log, then clear object variable
65+
66+
$Object | Export-Csv -NoTypeInformation -Append -Path $LogPath\$LogFile
67+
}
68+
if ($User) {Remove-Variable User}
69+
if ($Domain) {Remove-Variable Domain}
70+
if ($NewAttrib10) {Remove-Variable NewAttrib10}
71+
if ($Message) {Remove-Variable Message}
72+
if ($Object) {Remove-Variable Object}
73+
}

0 commit comments

Comments
 (0)