-
Notifications
You must be signed in to change notification settings - Fork 61
/
Report-Functions.ps1
294 lines (258 loc) · 9.37 KB
/
Report-Functions.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
$style = @"
<style>
body {
background-color: #FFFFFF;
font-family: sans-serif;
}
h1 {
color: #1E87F0;
}
h2 {
color: #1E87F0;
}
table {
background-color: #1E87F0;
}
td {
background-color: #FFFFFF;
color: #666666;
padding: 3px;
}
th {
background-color: #1E87F0;
color: #FFFFFF;
text-align: left;
padding: 3px;
}
</style>
"@
function Generate-ADUserPasswordReport() {
[cmdletBinding()]
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[System.String]$FileName,
[parameter(Mandatory=$false)]
[Management.Automation.PSCredential] $Credential
)
process {
if ($Credential -ne $null) {
$users = Get-ADUser -Filter * -Credential $Credential -Properties "UserPrincipalName","SamAccountName","DisplayName","LastLogonDate","PasswordLastSet","LastBadPasswordAttempt","PasswordNeverExpires","LockedOut","PasswordExpired","PasswordNotRequired","BadLogonCount","badPwdCount","CannotChangePassword","logonCount" | Select-Object * -ExcludeProperty DistinguishedName,GivenName,ModifiedProperties,PropertyCount,RemovedProperties,AddedProperties,PropertyNames,Surname,SID,ObjectGUID,ObjectClass
} else {
$users = Get-ADUser -Filter * -Properties "UserPrincipalName","SamAccountName","DisplayName","LastLogonDate","PasswordLastSet","LastBadPasswordAttempt","PasswordNeverExpires","LockedOut","PasswordExpired","PasswordNotRequired","BadLogonCount","badPwdCount","CannotChangePassword","logonCount" | Select-Object * -ExcludeProperty DistinguishedName,GivenName,ModifiedProperties,PropertyCount,RemovedProperties,AddedProperties,PropertyNames,Surname,SID,ObjectGUID,ObjectClass
}
$usersHtml = $users | Select-Object * | ConvertTo-Html -Fragment -PreContent "<h2>Active Directory Users - Password Information</h2>"
$title = "<title>AD Users - Password Information</title>"
ConvertTo-Html -Head "$title$style" -Body "$searchbar$usersHtml" | Out-File $FileName
}
}
function Generate-ADUserReport() {
[cmdletBinding()]
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[System.String]$FileName,
[parameter(Mandatory=$false)]
[Management.Automation.PSCredential] $Credential
)
process {
if ($Credential -ne $null) {
$users = Get-ADUser -Filter * -Credential $Credential -Properties "UserPrincipalName","SamAccountName","DisplayName","EmailAddress","Title","BadLogonCount","badPwdCount","CannotChangePassword","CanonicalName","Company","Country","Created","Department","Description","Enabled","GivenName","HomeDirectory","HomePhone","LastBadPasswordAttempt","LastLogonDate","LockedOut","logonCount","mail","Manager","Modified","Name","Office","OfficePhone","PasswordExpired","PasswordNeverExpires","PasswordNotRequired","primaryGroupID","ProtectedFromAccidentalDeletion","PasswordLastSet" | Select-Object * -ExcludeProperty ModifiedProperties,PropertyCount,RemovedProperties,AddedProperties,PropertyNames,ObjectClass
} else {
$users = Get-ADUser -Filter * -Properties "UserPrincipalName","SamAccountName","DisplayName","EmailAddress","Title","BadLogonCount","badPwdCount","CannotChangePassword","CanonicalName","Company","Country","Created","Department","Description","Enabled","GivenName","HomeDirectory","HomePhone","LastBadPasswordAttempt","LastLogonDate","LockedOut","logonCount","mail","Manager","Modified","Name","Office","OfficePhone","PasswordExpired","PasswordNeverExpires","PasswordNotRequired","primaryGroupID","ProtectedFromAccidentalDeletion","PasswordLastSet" | Select-Object * -ExcludeProperty ModifiedProperties,PropertyCount,RemovedProperties,AddedProperties,PropertyNames,ObjectClass
}
$usersHtml = $users | Select-Object * | ConvertTo-Html -Fragment -PreContent "<h2>Active Directory Users</h2>"
$title = "<title>AD Users</title>"
ConvertTo-Html -Head "$title$style" -Body "$searchbar$usersHtml" | Out-File $FileName
}
}
function Generate-ADGroupReport() {
[cmdletBinding()]
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[System.String]$FileName,
[parameter(Mandatory=$false)]
[Management.Automation.PSCredential] $Credential
)
process {
if ($Credential -ne $null) {
$groups = Get-ADGroup -Filter * -Credential $Credential
} else {
$groups = Get-ADGroup -Filter *
}
$groupsHtml = $groups | Select-Object Name,GroupCategory,GroupScope | ConvertTo-Html -Fragment -PreContent "<h2>Active Directory Groups</h2>"
$title = "<title>AD Groups</title>"
ConvertTo-Html -Head "$title$style" -Body "$searchbar$groupsHtml" | Out-File $FileName
}
}
### Generate AD Computer Report ###
function Generate-ADComputerReport() {
[cmdletBinding()]
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[System.String]$FileName,
[parameter(Mandatory=$false)]
[Management.Automation.PSCredential] $Credential
)
process {
# Grab list of computers in Active Directory
if ($Credential -ne $null) {
$servers = Get-ADComputer -Filter * -Credential $Credential
} else {
$servers = Get-ADComputer -Filter *
}
# Convert list to HTML
$serversHtml = $servers | Select-Object DNSHostName,Enabled,Name,ObjectClass,ObjectGUID,SamAccountName | ConvertTo-Html -Fragment -PreContent "<h2>Active Directory Computers</h2>"
# Create HTML file
$title = "<title>AD Computer List</title>"
# Convert everything to HTML and output to file
ConvertTo-Html -Head "$title$style" -Body "$searchbar$serversHtml" | Out-File $FileName
}
}
### Generate AD Computer Network Report ###
function Generate-ADComputerNetworkReport() {
[cmdletBinding()]
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[System.String]$FileName,
[parameter(Mandatory=$false)]
[Management.Automation.PSCredential] $Credential
)
process {
# Grab list of computers in Active Directory
if ($Credential -ne $null) {
$servers = Get-ADComputer -Filter * -Credential $Credential
} else {
$servers = Get-ADComputer -Filter *
}
$serversNet = @()
foreach ($server in $servers) {
Write-Host "Testing "$server.DNSHostName
$results = Test-Server -ComputerName $server.DNSHostName
$serversNet += $results
}
$serversHtml = $serversNet | ConvertTo-Html -Fragment -PreContent "<h2>AD Computers - Network Status</h2>"
# Create HTML file
$title = "<title>AD Computer List - Network</title>"
# Convert everything to HTML and output to file
ConvertTo-Html -Head "$title$style" -Body "$searchbar$serversHtml" | Out-File $FileName
}
}
# This is not my original work, I downloaded it to use with GetFailed.ps1
Function Test-Server{
[cmdletBinding()]
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string[]]$ComputerName,
[parameter(Mandatory=$false)]
[switch]$CredSSP,
[Management.Automation.PSCredential] $Credential)
begin{
$total = Get-Date
$results = @()
if($credssp){if(!($credential)){Write-Host "must supply Credentials with CredSSP test";break}}
}
process{
foreach($name in $computername)
{
$dt = $cdt= Get-Date
Write-verbose "Testing: $Name"
$failed = 0
try{
$DNSEntity = [Net.Dns]::GetHostEntry($name)
$domain = ($DNSEntity.hostname).replace("$name.","")
$ips = $DNSEntity.AddressList | %{$_.IPAddressToString}
}
catch
{
$rst = "" | select Name,IP,Domain,Ping,WSMAN,CredSSP,RemoteReg,RPC,RDP
$rst.name = $name
$results += $rst
$failed = 1
}
Write-verbose "DNS: $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
if($failed -eq 0){
foreach($ip in $ips)
{
$rst = "" | select Name,IP,Domain,Ping,WSMAN,CredSSP,RemoteReg,RPC,RDP
$rst.name = $name
$rst.ip = $ip
$rst.domain = $domain
####RDP Check (firewall may block rest so do before ping
try{
$socket = New-Object Net.Sockets.TcpClient($name, 3389)
if($socket -eq $null)
{
$rst.RDP = $false
}
else
{
$rst.RDP = $true
$socket.close()
}
}
catch
{
$rst.RDP = $false
}
Write-verbose "RDP: $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
#########ping
if(test-connection $ip -count 1 -Quiet)
{
Write-verbose "PING: $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
$rst.ping = $true
try{############wsman
Test-WSMan $ip | Out-Null
$rst.WSMAN = $true
}
catch
{$rst.WSMAN = $false}
Write-verbose "WSMAN: $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
if($rst.WSMAN -and $credssp) ########### credssp
{
try{
Test-WSMan $ip -Authentication Credssp -Credential $cred
$rst.CredSSP = $true
}
catch
{$rst.CredSSP = $false}
Write-verbose "CredSSP: $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
}
try ########remote reg
{
[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $ip) | Out-Null
$rst.remotereg = $true
}
catch
{$rst.remotereg = $false}
Write-verbose "remote reg: $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
try ######### wmi
{
$w = [wmi] ''
$w.psbase.options.timeout = 15000000
$w.path = "\\$Name\root\cimv2:Win32_ComputerSystem.Name='$Name'"
$w | select none | Out-Null
$rst.RPC = $true
}
catch
{$rst.rpc = $false}
Write-verbose "WMI: $((New-TimeSpan $dt ($dt = get-date)).totalseconds)"
}
else
{
$rst.ping = $false
$rst.wsman = $false
$rst.credssp = $false
$rst.remotereg = $false
$rst.rpc = $false
}
$results += $rst
}}
Write-Verbose "Time for $($Name): $((New-TimeSpan $cdt ($dt)).totalseconds)"
Write-Verbose "----------------------------"
}
}
end{
Write-Verbose "Time for all: $((New-TimeSpan $total ($dt)).totalseconds)"
Write-Verbose "----------------------------"
return $results
}
}