-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server-GetComputerList.ps1
160 lines (136 loc) · 7.81 KB
/
Server-GetComputerList.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
function Server-GetComputerList {
param (
[string]$ExportExcel,
[switch]$GetHelp
)
$dcpath = "OU=PC,DC=EXAMPLE,DC=COM,DC=SG"; #Default path for AD workstations
if ($GetHelp) { #Shows helpmenu
$ScriptName = $MyInvocation.MyCommand.Name
Write-Host;
Write-Host "Returns a list of all workstations in domain";
Write-Host;
Write-Host "Usage: $ScriptName [-ExportExcel <filepath>]";
Write-Host;
Write-Host 'Options:';
Write-Host ' -ExportExcel <filepath> Specify a filepath ending in .xlsx to export list, e.g. ".\Downloads\list.xlsx\" '
return;
}
if ($ExportExcel) {
if (!($ExportExcel -Like "*.xlsx")) {
throw "Filepath must end in '.xlsx'";
}
[ref]$ExportPath = [ref]$ExportExcel;
}
if ($ExportPath) {
if ($ExportPath.Value -like "\*") {
$ExportPath.Value = $ExportPath.Value.Substring(1);
}
if ($ExportPath.Value -like ".\*") {
$ExportPath.Value = (pwd).Path + $ExportPath.Value.Substring(1);
}
elseif (!($ExportPath.Value -like "[a-z]:\*")) {
$ExportPath.Value = (pwd).Path + "\" + $ExportPath.Value;
}
$ExportPath.Value -match "(.*\\).*\.[a-z]{4}$" | Out-Null
$TestPath = $Matches[1];
if (!(Test-Path $TestPath)) {
throw "Invalid path $($ExportPath.Value)";
}
}
#Retrieve all computers found in AD
$com = Get-ADComputer -Server $ADServer `
-Properties Name, CanonicalName, ManagedBy, Modified, IPv4Address, `
OperatingSystem, OperatingSystemServicePack `
-Filter {Enabled -eq 'True'} `
-SearchBase $dcpath | `
Select @{name='Computer Name';expression={$_.Name}}, `
@{name='Location';expression={$_.CanonicalName}}, `
@{name='Last User';expression={$_.ManagedBy}}, `
@{name='Last Used On';expression={$_.Modified}}, `
@{name='IP Address';expression={$_.IPv4Address}}, `
@{name='Operating System';expression={$_.OperatingSystem + ' ' + $_.OperatingSystemServicePack}};
foreach ($c in $com) { #Format Computer Location and Last User parameters
$c.Location = $c.Location -replace '([.]*)/[0-9a-zA-Z\.\- ]*$', $1;
if ($c.'Last User') {
$c.'Last User' = (Get-ADUser -Server $ADServer -Identity $c.'Last User' -Properties CanonicalName).CanonicalName;
}
}
if ($ExportExcel) { #Export list to excel
$com | Export-Excel $ExportExcel -WorkSheetname 'Computer List';
}
else { #Show list on console
$com | Format-Table @{name='Computer Name';expression={$_.'Computer Name'}}, `
@{name='Location';expression={$_.Location}}, `
@{name='Last User';expression={$_.'Last User'}}, `
@{name='Last Used On';expression={$_.'Last Used On'}}, `
@{name='IP Address';expression={$_.'IP Address'}}, `
@{name='Operating System';expression={$_.'Operating System'}};
}
}
# SIG # Begin signature block
# MIIMIQYJKoZIhvcNAQcCoIIMEjCCDA4CAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU6+C61T9lUcCuAXKuo0Tn+LfT
# 0l6gggoMMIIE7DCCBFWgAwIBAgIKE7/jkQAAAAAADjANBgkqhkiG9w0BAQUFADBS
# MRIwEAYKCZImiZPyLGQBGRYCU0cxEzARBgoJkiaJk/IsZAEZFgNDT00xEzARBgoJ
# kiaJk/IsZAEZFgNNSVQ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