-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSet-ComputerCustomAttributes.ps1
More file actions
206 lines (168 loc) · 9.2 KB
/
Set-ComputerCustomAttributes.ps1
File metadata and controls
206 lines (168 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
###################################################################################################
#
# ScriptName: Set-ComputerCustomAttributes.ps1
# Auther: eshoemaker@lockstepgroup.com
# Last Updated: Q2 2017
# Sets custom Active Directory attributes
# Reference URL: https://lockstepgroup.com/blog/fun-with-ad-custom-attributes/
#
###################################################################################################
###################################################################################################
# REQUIREMENTS
#
# Create custom attributes in AD and attach to the computer class
# Delegate "SELF" permissions to write to the custom attributes created on computer objects
# Set the variables at the beginning of this script to match your custom attributes exactly
# For easy troubleshooting, set the $GenerateLog variable to $True. Be sure to set to $false in
# production
#
###################################################################################################
# MODIFY THESE VARIABLES TO MATCH YOUR CUSTOM ATTRIBUTES
$LastLoggedOnUserAttribute="Custom-LastLoggedOnUser"
$LastLoggedOnUserDateAttribute="Custom-LastLoggedOnUserDate"
$HWVendorAttribute="Custom-HardwareVendor"
$HWModelAttribute="Custom-HardwareModel"
$SerialNumberAttribute="SerialNumber"
# TO ENABLE LOGGING, SET THE $GenerateLog VARIABLE TO $true
$GenerateLog=$false
$LogFile="C:\ADCustomAttributes.log"
###################################################################################################
# CREATING LOGGING FUNCTION
# Having the $GenerateLog variable outside the function does not follow coding best practices,
# but it makes it easy to enable/disable logging for this specific script.
###################################################################################################
Function Write-Log {
param($InputData)
If ($GenerateLog -eq $true){
$LogDate=Get-Date
"$LogDate -- $InputData" | Out-File $LogFile -Append
}
}
Write-Log "---------------------STARTING CUSTOM ATTRIBUTE SCRIPT---------------------"
###################################################################################################
#
# GETTING USER AND HARDWARE INFO.
#
###################################################################################################
# GETTING LAST LOGGED ON USER INFO - INTERACTIVE LOGONS ONLY (TYPE 2); RDP LOGONS = (TYPE 10)
$ComputerName=$env:COMPUTERNAME
Write-Log "Computer Name = $ComputerName"
Write-Log "GATHERING LAST LOGGED ON USER INFO"
$30Days=(Get-Date).adddays(-30)
function LogOnSuccess {
Try {
$Events = Get-WinEvent -MaxEvents 1 -LogName "Security" -FilterXPath "*[System[(EventID='4624')] and EventData[Data[@Name='LogonType'] and (Data='2' or Data='10' or Data='11')]`
and EventData[Data[@Name='TargetDomainName']!='Window Manager'] and EventData[Data[@Name='TargetDomainName']!='Font Driver Host']and EventData[Data[@Name='TargetUserName']!='Administrator'] and EventData[Data[@Name='TargetUserName']!='SYSTEM'] and EventData[Data[@Name='TargetUserName']!='SYSTEM'] and EventData[Data[@Name='TargetUserName']!='LOCAL SERVICE'] and EventData[Data[@Name='TargetUserName']!='SCCM_Svc'] and EventData[Data[@Name='TargetUserName']!='NETWORK SERVICE']]" -ErrorAction Stop
ForEach ($Event in $Events) {
$eventXML = [xml]$Event.ToXml()
Add-Member -InputObject $Event -MemberType NoteProperty -Force -Name "TimeCreate" -Value $Event.TimeCreated
FOREACH ($j in $eventXML.Event.System.ChildNodes) {
Add-Member -InputObject $Event -MemberType NoteProperty -Force -Name $j.ToString() -Value $eventXML.Event.System.($j.ToString())
}
For ($i=0; $i -lt $eventXML.Event.EventData.Data.Count; $i++) {
Add-Member -InputObject $Event -MemberType NoteProperty -Force -Name $eventXML.Event.EventData.Data[$i].name -Value $eventXML.Event.EventData.Data[$i].'#text'
}
}
$global:LoggedOnUser=($Events | select -expand TargetUserName)
$global:LoggedOnDomain=($Events | select -expand TargetDomainName)
$global:Date=($Events | select -expand TimeCreate)
$global:LoggedOnUser=($global:LoggedOnDomain)+'\'+($global:LoggedOnUser)
}
Catch { return 'Result: Null'}
}
LogOnSuccess
Write-Log "Last Logged On User = $LoggedOnUser"
# GETTING COMPUTER HARDWARE INFO
Write-Log "GATHERING HARDWARE INFO"
$HWInfo=Get-WmiObject win32_computerSystem
$HWVendor=$HWInfo.Manufacturer
$HWModel=$HWInfo.Model
$SerialNumber=Get-WmiObject win32_bios | Select -ExpandProperty SerialNumber
Write-Log "Hardware Info = $HWVendor $HWModel"
Write-Log "Serial Number = $SerialNumber"
###################################################################################################
#
# GETTING CURRENT AD COMPUTER OBJECT
#
###################################################################################################
Write-Log "SEARCHING FOR COMPUTER OBJECT $ComputerName"
$Searcher = New-Object adsisearcher
$Searcher.Filter = "(&(name=$ComputerName)(objectcategory=computer))"
$Searcher.PropertiesToLoad.Add("$LastLoggedOnUserAttribute")
$Searcher.PropertiesToLoad.Add("$LastLoggedOnUserDateAttribute")
$Searcher.PropertiesToLoad.Add("$HWVendorAttribute")
$Searcher.PropertiesToLoad.Add("$HWModelAttribute")
$Searcher.PropertiesToLoad.Add("$SerialNumberAttribute")
$Domain=$Searcher.SearchRoot.distinguishedName
$ComputerObj=$Searcher.FindOne()
$ADPath=$ComputerObj.Path
If ($ComputerObj -ne $null){
Write-Log "$ComputerName FOUND SUCCESSFULLY IN DOMAIN $Domain"
}
Else {
Write-Log "$ComputerName NOT FOUND IN DOMAIN $Domain"
Write-Log "---------------------ENDING CUSTOM ATTRIBUTE SCRIPT---------------------"
Exit
}
# GATHERING CUSTOM ATTRIBUTE VALUES ON THE COMPUTER OBJECT
$LastLoggedOnUserValue = $ComputerObj.Properties.Item($LastLoggedOnUserAttribute)
$LastLoggedOnUserDateValue = $ComputerObj.Properties.Item($LastLoggedOnUserDateAttribute)
$HWVendorValue = $ComputerObj.Properties.Item($HWVendorAttribute)
$HWModelValue = $ComputerObj.Properties.Item($HWModelAttribute)
$SerialNumberValue = $ComputerObj.Properties.Item($SerialNumberAttribute)
###################################################################################################
#
# SETTING AD CUSTOM ATTRIBUTE VALUES WITH USER AND HARDWARE DATA COLLECTED IN PREVIOUS COMMANDS
# THE ADSI OBJECT IS RECREATED FOR EACH WRITE. THIS PREVENTS AN ERROR ON ONE ATTRIBUTE CAUSING ALL ATTRIBUTE WRITES TO FAIL
# ADDITIONALLY, THE SCRIPT READS THE COMPUTER OBJECT ATTRIBUTE VALUES AND DOESN'T UPDATE THEM IF THEY ARE THE SAME TO PREVENT UNNECESSARY AD REPLICATION
#
###################################################################################################
Write-Log "SETTING CUSTOM ATTRIBUTES ON $ComputerName"
# SPECIFYING FUNCTION FOR SETTING ATTRIBUTES
Function Update-ADAttribute{
param(
$ADSIObjectPath,
$AttributeName,
$AttributeValue
)
Write-Log "ATTEMPTING TO SET $AttributeName ATTRIBUTE TO $AttributeValue"
$ADObject = [ADSI]”$ADSIObjectPath”
$ADObject.Put(“$AttributeName”, “$AttributeValue”)
Try{
$ADObject.SetInfo()
Write-Log "$AttributeName SET SUCCESSFULLY"
} Catch{
Write-Log "$AttributeName WRITE FAILED"
Write-Log $Error[0]
}
}
# SETTING LOGGED ON USER ATTRIBUTES ONLY IF A USER LOGON FOUND IN THE LAST 30 DAYS. THIS PREVENTS OVER-WRITING THE ATTRIBUTE IF NO USER HAS LOGGED ON IN 30 DAYS
If ($LoggedOnUser -ne $null -and $Date -ne $Null){
# MODIFYING LOGGED ON USER ATTRIBUTE
If ($LastLoggedOnUserValue -eq $LoggedOnUser){
Write-Log "$LastLoggedOnUserAttribute ALREADY SET TO $LoggedOnUser. NOT UPDATING ATTRIBUTE."
}
Else {Update-ADAttribute -ADSIObjectPath $ADPath -AttributeName $LastLoggedOnUserAttribute -AttributeValue $LoggedOnUser}
# MODIFYING LOGGED ON USER DATE ATTRIBUTE
If ($LastLoggedOnUserDateValue -eq $Date){
Write-Log "$LastLoggedOnUserDateAttribute ALREADY SET TO $Date. NOT UPDATING ATTRIBUTE."
}
Else {Update-ADAttribute -ADSIObjectPath $ADPath -AttributeName $LastLoggedOnUserDateAttribute -AttributeValue $Date}
}
# MODIFYING HARDWARE VENDOR ATTRIBUTE
If ($HWVendorValue -eq $HWVendor){
Write-Log "$HWVendorAttribute ALREADY SET TO $HWVendor. NOT UPDATING ATTRIBUTE."
}
Else {Update-ADAttribute -ADSIObjectPath $ADPath -AttributeName $HWVendorAttribute -AttributeValue $HWVendor}
# MODIFYING HARDWARE MODEL ATTRIBUTE
If ($HWModelValue -eq $HWModel){
Write-Log "$HWModelAttribute ALREADY SET TO $HWModel. NOT UPDATING ATTRIBUTE."
}
Else {Update-ADAttribute -ADSIObjectPath $ADPath -AttributeName $HWModelAttribute -AttributeValue $HWModel}
# MODIFYING SERIAL NUMBER ATTRIBUTE
If ($SerialNumberValue -eq $SerialNumber){
Write-Log "$SerialNumberAttribute ALREADY SET TO $SerialNumber. NOT UPDATING ATTRIBUTE."
}
Else {Update-ADAttribute -ADSIObjectPath $ADPath -AttributeName $SerialNumberAttribute -AttributeValue $SerialNumber}
# ENDING LOG
Write-Log "---------------------ENDING CUSTOM ATTRIBUTE SCRIPT---------------------"