-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-ADComputers.ps1
More file actions
53 lines (43 loc) · 2.09 KB
/
get-ADComputers.ps1
File metadata and controls
53 lines (43 loc) · 2.09 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
function Get-ADComputers{
trap [Exception] {
# trapped "AD:$Filter:" $($_.Exception.Message)
}
$searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"") #Leaving the ADSI statement empty = attach to your root domain
$searcher.filter = "(&(objectClass=computer)(operatingSystem=Windows*))"
$searcher.CacheResults = $true
$searcher.SearchScope = “Subtree”
$searcher.PageSize = 1000
$accounts = $searcher.FindAll()
if ($accounts.Count -gt 0) {
foreach($account in $accounts){
$cn = $account.Properties["cn"][0]
#$useraccountcontrol = $account.Properties["userAccountControl"][0]; # didn't work
$fixme = [adsi]$account.Path
# Property that contains the last password change in long integer format
$pwdLastSet = $account.Properties["pwdlastset"]
# Convert the long integer to normal DateTime format
$lastchange = [datetime]::FromFileTimeUTC($pwdLastSet[0])
# Determine the timespan between the two dates
$datediff = new-TimeSpan $lastchange $(Get-Date)
# Create an output object for table formatting
$obj = new-Object PSObject
# Add member properties with their name and value pair
$obj | Add-Member NoteProperty cn($account.Properties["cn"][0])
$obj | Add-Member NoteProperty distinguishedName($fixme.distinguishedName.Item(0))
$obj | Add-Member NoteProperty operatingSystem($fixme.operatingSystem.Item(0))
# $obj | Add-Member NoteProperty operatingSystemServicePack($fixme.operatingSystemServicePack.Item(0))
$obj | Add-Member NoteProperty userAccountControl($fixme.userAccountControl.Item(0))
$obj | Add-Member NoteProperty pwdLastSet($lastchange)
$obj | Add-Member NoteProperty DaysSinceChange($datediff.Days)
$obj
}
}
}
#foreach($comp in Get-ADComputers) {
# if ($comp.cn -match 'COH') {
# $comp
# }
# }
Get-ADComputers | select $_.distinguishedName
#where ($_.distinguishedName -contains 'CO_Computers')
'script completed'