-
Notifications
You must be signed in to change notification settings - Fork 1
/
LocalAdminAudit.ps1
67 lines (52 loc) · 1.62 KB
/
LocalAdminAudit.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
56
57
58
59
60
61
62
63
64
65
66
67
#REQUIRES -Version 2.0
<#
.SYNOPSIS
Check local admin credentials on a list of servers
.DESCRIPTION
Check local admin credentials on a list of servers defined in a CSV file
The CSV file format is as follows:
Name
MyServer1
MyServer2
The user will be prompted for the local admin username and password
.PARAMETER file
Name of the CSV file specifying the servers (required)
.NOTES
File Name : LocalAdminAudit.ps1
Author : Allan Scullion
Prerequisite : PowerShell V2, ActiveDirectory module
.EXAMPLE
.\LocalAdminAudit.ps1 -file Test.csv -action add
.EXAMPLE
.\LocalAdminAudit.ps1 -file Test.csv -action remove
#>
param (
[parameter(Mandatory=$true)]
[alias("f")]
[string]$file = $(throw "-file is required.")
)
# What username should be tested?
$username = Read-Host "Local Admin Username"
$pass = Read-Host "Password" -AsSecureString
$dataSource = Import-Csv $file
# Find every computer in AD running an operating system with "Server" in its name.
foreach($dataRecord in $dataSource)
{
$server = $dataRecord.Name
# Make sure the server can be contacted
if (Test-Connection $server -Quiet)
{
# Build a local administrator credential
$credential = New-Object System.Management.Automation.PSCredential("$($server)\$username",$pass)
# Try to connect to the computer with the credential
try
{
$null = Get-WmiObject Win32_OperatingSystem -Credential $credential -ComputerName $server
Write-Host "$($server) :: Success" -ForegroundColor Green -BackgroundColor Black
}
catch
{
Write-Host "$($server) :: Fail" -ForegroundColor Red -BackgroundColor Black
}
}
}