-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADQuery.ps1
55 lines (48 loc) · 1.89 KB
/
ADQuery.ps1
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
# This Script is designed to help with administration of user accounts that contain DNS names of computers that are disabled or have been removed from Active Directory
# 1. Prompts for username
# 2. The "LogonWorkstations" property is queried in AD and is stored as an array
# 3. Each machine in the new array is then queried against the "Enabled" property
# 4. A CSV file is created with the machine name and with it's associated "Enabled" value
#This script automatically returns a $false value for "InAD" if the machine isn't found at all using Try/Catch
function Get-LogonWorkstations {
[cmdletbinding()]
param(
[parameter(Mandatory)]
[string]$UserName
)
try {
$Account = Get-AdUser -Identity $Username -Properties LogonWorkstations | Select-Object -ExpandProperty Logonworkstations
Write-Verbose -Message ('Located user {0}' -f $UserName)
}
catch {
Throw ('Unable to locate user {0}' -f $UserName)
}
$output = $Account -split ',' | ForEach-Object {
$computer = $_
try {
$adcomp = Get-ADComputer -Identity $_ -ErrorAction Stop | Select-Object -ExpandProperty Enabled
if($adcomp -eq $true) {
[PSCustomObject]@{
MachineName = $computer
InAD = $true
}
Write-Verbose ('{0} is enabled' -f $computer)
}
else {
[PSCustomObject]@{
MachineName = $computer
InAD = $false
}
Write-Verbose ('{0} is disabled' -f $computer)
}
}
catch {
[PSCustomObject]@{
MachineName = $computer
InAD = $false
}
Write-Verbose ('Unable to locate machine {0}' -f $computer)
}
}
$output | export-csv -path C:\Powershell\data.csv
}