-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ADComputer-Accounts.ps1
165 lines (136 loc) · 5.93 KB
/
Get-ADComputer-Accounts.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
<#
Author: Stan Crider
Date: 8May2020
Crap: Gets a list of all computer objects in an AD environment
### Must have ImportExcel module installed!!!
### https://github.com/dfinke/ImportExcel
#>
## Prepare script
# Configure variables
$Date = Get-Date -Format yyyyMMMdd
$Workbook = "C:\Temp\Computer Report $Date.xlsx"
$Searchbase = "DC=Acme,DC=com"
# Configure arrays
$ComputerArray = @()
$OperatingSystemArray = @()
## Functions
# Convert number of object items into Excel column headers
Function Get-ColumnName ([int]$ColumnCount){
<#
.SYNOPSIS
Converts integer into Excel column headers
.DESCRIPTION
Takes a provided number of columns in a table and converts the number into Excel header format
Input: 27 - Output: AA
Input: 2 - Ouput: B
.EXAMPLE
Get-ColumnName 27
.INPUTS
Integer
.OUTPUTS
String
.NOTES
Author: Stan Crider and Dennis Magee
#>
If(($ColumnCount -le 702) -and ($ColumnCount -ge 1)){
$ColumnCount = [Math]::Floor($ColumnCount)
$CharStart = 64
$FirstCharacter = $null
# Convert number into double letter column name (AA-ZZ)
If($ColumnCount -gt 26){
$FirstNumber = [Math]::Floor(($ColumnCount)/26)
$SecondNumber = ($ColumnCount) % 26
# Reset increment for base-26
If($SecondNumber -eq 0){
$FirstNumber--
$SecondNumber = 26
}
# Left-side column letter (first character from left to right)
$FirstLetter = [int]($FirstNumber + $CharStart)
$FirstCharacter = [char]$FirstLetter
# Right-side column letter (second character from left to right)
$SecondLetter = $SecondNumber + $CharStart
$SecondCharacter = [char]$SecondLetter
# Combine both letters into column name
$CharacterOutput = $FirstCharacter + $SecondCharacter
}
# Convert number into single letter column name (A-Z)
Else{
$CharacterOutput = [char]($ColumnCount + $CharStart)
}
}
Else{
$CharacterOutput = "ZZ"
}
# Output column name
$CharacterOutput
}
## Script: gather data from AD and add to array
If(Test-Path $Workbook){
Write-Warning "The file $Workbook already exists. Script terminated."
}
Else{
$Computers = Get-ADComputer -Filter * -SearchBase $Searchbase -Properties * | Sort-Object CanonicalName
ForEach($Computer in $Computers){
# Set computer account password age in days
$PwdAge = $null
If($null -ne $Computer.PasswordLastSet){
$PwdAge = ((Get-Date)-$Computer.PasswordLastSet).Days
}
# Ping computer if account enabled to check if online
$Pingable = "Not Tested"
<#If($Computer.Enabled -eq $true){
$Pingable = Test-Connection $Computer.Name -Count 2 -Quiet
}#>
# Add properties to array
$ComputerArray += [PSCustomObject]@{
"Name" = $Computer.Name
"IP Address" = $Computer.IPv4Address
"Enabled" = $Computer.Enabled
"Operating System" = $Computer.OperatingSystem
"Created" = $Computer.Created
"Last Logon" = $Computer.LastLogonDate
"Last Password" = $Computer.PasswordLastSet
"Password Age" = $PwdAge
"Online" = $Pingable
"Description" = $Computer.Description
"Location" = $Computer.Location
"Last Logon By" = $Computer.employeeNumber
"Domain Name" = $Computer.CanonicalName
"Distinguished Name" = $Computer.DistinguishedName
}
}
# Count operating systems and amount of each OS
$OperatingSystems = $ComputerArray | Select-Object "Operating System" | Sort-Object "Operating System" | Get-Unique -AsString
ForEach($OS in $OperatingSystems){
$OperatingSystemArray += [PSCustomObject]@{
"OS" = $OS.'Operating System'
"Count" = ($ComputerArray | Where-Object{$_.'Operating System' -eq $OS.'Operating System'} | Measure-Object).Count
}
}
## Output
# Create Excel standard configuration properties
$ExcelProps = @{
Autosize = $true;
FreezeTopRow = $true;
BoldTopRow = $true;
}
$ExcelProps.Path = $Workbook
# Computer worksheet
$ComputerArrayColumnCount = Get-ColumnName ($ComputerArray | Get-Member | Where-Object{$_.MemberType -match "NoteProperty"} | Measure-Object).Count
$ComputerArrayHeaderRow = "`$A`$1:`$$ComputerArrayColumnCount`$1"
$ComputerArrayLastRow = ($ComputerArray | Measure-Object).Count + 1
$EnabledColumn = "'Computers'!`$C`$2:`$C`$$ComputerArrayLastRow"
$PasswordAgeColumn = "'Computers'!`$H`$2:`$H`$$ComputerArrayLastRow"
$ComputerArrayStyle = @()
$ComputerArrayStyle += New-ExcelStyle -Range "'Computers'!$ComputerArrayHeaderRow" -HorizontalAlignment Center
$ComputerArrayConditionalFormatting = @()
$ComputerArrayConditionalFormatting += New-ConditionalText -Range $EnabledColumn -ConditionalType ContainsText "FALSE" -ConditionalTextColor Brown -BackgroundColor Yellow
$ComputerArrayConditionalFormatting += New-ConditionalText -Range $PasswordAgeColumn -ConditionalType GreaterThan 180 -ConditionalTextColor Maroon -BackgroundColor Pink
$ComputerArray | Export-Excel @ExcelProps -WorkSheetname "Computers" -ConditionalText $ComputerArrayConditionalFormatting -Style $ComputerArrayStyle
# OS Count worksheet
$OSArrayColumnCount = Get-ColumnName ($OperatingSystemArray | Get-Member | Where-Object{$_.MemberType -match "NoteProperty"} | Measure-Object).Count
$OSArrayHeaderRow = "`$A`$1:`$$OSArrayColumnCount`$1"
$OSArrayStyle = New-ExcelStyle -Range "'Computers'!$OSArrayHeaderRow" -HorizontalAlignment Center
$OperatingSystemArray | Export-Excel @ExcelProps -WorkSheetname "OS Count" -Style $OSArrayStyle
}