-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-Service Accounts from a Specific domains.ps1
More file actions
50 lines (42 loc) · 2.18 KB
/
Get-Service Accounts from a Specific domains.ps1
File metadata and controls
50 lines (42 loc) · 2.18 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
# Load GUI assembly
Add-Type -AssemblyName Microsoft.VisualBasic
# 1. Prompt for Domain
$TargetDomain = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the domain to search", "Service Account Search", "MTVN.ad.viacom.com")
if ([string]::IsNullOrWhiteSpace($TargetDomain)) { exit }
Write-Host "Searching ${TargetDomain} for accounts containing 'Service Account' or 'SVC'..." -ForegroundColor Cyan
try {
# 2. LDAP Filter logic:
# EA6 contains "Service Account" OR EmployeeID contains "SVC" OR EmployeeNumber contains "SVC"
$LdapFilter = "(|(extensionAttribute6=*Service Account*)(employeeID=*SVC*)(employeeNumber=*SVC*))"
# 3. Fetch users with the required attributes
$Users = Get-ADUser -LDAPFilter $LdapFilter -Server $TargetDomain -Properties `
extensionAttribute6, employeeID, employeeNumber, Title, Department, Description, whenCreated, Enabled
if ($null -eq $Users) {
Write-Warning "No accounts found matching these 'Contains' criteria in ${TargetDomain}."
} else {
# 4. Process the data
$Results = $Users | Select-Object `
Name,
SamAccountName,
Enabled,
@{Name="EA6_Value"; Expression={$_.extensionAttribute6}},
@{Name="EmployeeID"; Expression={$_.employeeID}},
@{Name="EmployeeNumber"; Expression={$_.employeeNumber}},
Title,
Department,
Description,
@{Name="CreationDate"; Expression={$_.whenCreated}},
DistinguishedName
# 5. Show results
Write-Host "Success: Found $($Users.Count) accounts." -ForegroundColor Green
$Results | Out-GridView -Title "Service Account Search Results"
# 6. Export to Desktop
$Path = "$env:USERPROFILE\Desktop\Service_Account_Search_$(Get-Date -Format 'yyyyMMdd').csv"
$Results | Export-Csv -Path $Path -NoTypeInformation -Encoding UTF8
Write-Host "Report exported to Desktop: $Path" -ForegroundColor Yellow
}
}
catch {
# FIXED: Using ${} to prevent Drive Provider error with the colon
Write-Error "An error occurred connecting to ${TargetDomain}: $($_.Exception.Message)"
}