-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced-functions.ps1
241 lines (195 loc) · 7.81 KB
/
advanced-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
#requires -version 5.1
#requires -module CIMCmdlets
<#
an example of an advanced function based on earlier work
- parameter sets
- pipeline input
- Verbose output
- Comment-based help
- function alias
- Type update
- formatting views
#>
Function Get-Server {
<#
.Synopsis
Get company server information
.Description
This command will get information about company servers using WMI and the CIM
cmdlets. You must have admin rights on the remote server. If you need to use
alternate credentials, create a CIM session and use that with this command.
.Parameter Computername
Enter the name of a remote company computer.
.Parameter CimSession
Specify an existing CIMSession. You will need this if you need to use
alternate credentials.
.Parameter ProcessCount
Specify the total number of top processes by working set size. You can enter
a value between 1 and 50.
.Example
PS C:\> Get-Server dom1
Computername: DOM1
OperatingSystem TopProcesses FreeDiskGB FreeMemGB
--------------- ------------ ---------- ---------
Microsoft Windows Server WmiPrvSE.exe,MsMpEng.exe,svchost.exe,dns.exe,lsass. 115.9884 1
2019 Standard exe
.Example
PS C:\> Get-CimSession | Get-Server | Select Computername,Free*
Computername FreeDiskGB FreeMemoryGB
------------ ---------- ------------
DOM2 117.592987060547 0.710628509521484
SRV2 118.196933746338 0.531368255615234
SRV1 111.824684143066 0.406295776367188
Pipe existing CIMSessions to Get-Server and select specific properties.
.Example
PS C:\> gs dom1,dom2,srv1,srv2 -ProcessCount 1 | ft -view process
Computername: DOM1
ProcessID Name HandleCount WorkingSetSize CreationDate
--------- ---- ----------- -------------- ------------
4108 WmiPrvSE.exe 822 383057920 8/4/2021 4:19:47 AM
Computername: DOM2
ProcessID Name HandleCount WorkingSetSize CreationDate
--------- ---- ----------- -------------- ------------
1184 svchost.exe 3264 265658368 7/6/2021 3:44:46 PM
Computername: SRV1
ProcessID Name HandleCount WorkingSetSize CreationDate
--------- ---- ----------- -------------- ------------
1420 MsMpEng.exe 435 123600896 7/6/2021 3:42:04 PM
Computername: SRV2
ProcessID Name HandleCount WorkingSetSize CreationDate
--------- ---- ----------- -------------- ------------
1588 MsMpEng.exe 434 153194496 7/6/2021 3:42:03 PM
Use the custom table view to expand processes. This example uses the command
alias for Get-Server.
.Example
PS C:\> $c | Get-Server| sort os | Format-Table -view os
OperatingSystem: Microsoft Windows 10 Enterprise
Computername FreeMemGB ReportDate
------------ --------- ----------
WIN10 3.0774 8/10/2021
OperatingSystem: Microsoft Windows 10 Pro
Computername FreeMemGB ReportDate
------------ --------- ----------
PROSPERO 34.9595 8/10/2021
OperatingSystem: [38;5;86mMicrosoft Windows 11 Pro[0m
Computername FreeMemGB ReportDate
------------ --------- ----------
THINKP1 18.7802 8/10/2021
OperatingSystem: [38;5;47mMicrosoft Windows Server 2016 Standard[0m
Computername FreeMemGB ReportDate
------------ --------- ----------
SRV2 0.5325 8/10/2021
SRV1 0.2766 8/10/2021
OperatingSystem: [38;5;200mMicrosoft Windows Server 2019 Standard[0m
Computername FreeMemGB ReportDate
------------ --------- ----------
DOM1 0.501 8/10/2021
DOM2 0.7004 8/10/2021
Process a list of computer names and display the result using the custom table
view for operating system. Operating system information will be displayed with
ANSI formatting.
.Inputs
System.String
Microsoft.Management.Infrastructure.CimSession
.Link
Get-CimInstance
.Notes
Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/
#>
[cmdletbinding(DefaultParameterSetName = "computer")]
[alias("gs")]
[OutputType("companyServerInfo")]
Param(
[Parameter(
Position = 0,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
HelpMessage = "Enter the name of a server to query",
ParameterSetName = "computer"
)]
[Alias("cn")]
[ValidateNotNullOrEmpty()]
[string[]]$Computername = $env:COMPUTERNAME,
[Parameter(ParameterSetName = "session", ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[Microsoft.Management.Infrastructure.CimSession[]]$CimSession,
[ValidateRange(1, 50)]
[int]$ProcessCount = 5
)
Begin {
Write-Verbose "Starting $($MyInvocation.MyCommand)"
#define a hashtable of parameter values to splat for Get-CimInstance
$splat = @{
ErrorAction = "Stop"
Classname = $null
CimSession = $null
}
#initialize an array to hold cimsessions
$sessions = @()
Write-Verbose "Retrieving $ProcessCount top processes"
} #begin
Process {
Write-Verbose "Using parameter set $($pscmdlet.ParameterSetName)"
if ($pscmdlet.ParameterSetName -eq 'computer') {
#create a temporary cimsession
foreach ($computer in $computername) {
Try {
Write-Verbose "Creating a temporary CIMSession to $($computer.toUpper())"
$sessions += New-CimSession -ComputerName $computer -ErrorAction stop
$tempCS = $True
}
Catch {
Write-Warning "Failed to connect to $($computer.toupper()). $($_.Exception.message)"
}
} #foreach computer
}
Else {
$sessions += $CimSession
}
} #process
End {
foreach ($session in $sessions) {
#update the hashtable
$splat.Cimsession = $session
$splat.Classname = "win32_operatingsystem"
Write-Verbose "Getting server info for $($session.computername.toUpper())"
#get the data
$os = Get-CimInstance @splat
Write-Verbose "Getting process information"
$splat.classname = "win32_Process"
$processes = Get-CimInstance @splat
Write-Verbose "Found $($processes.count) total processes"
$cdrive = Get-Volume -DriveLetter C -CimSession $session
#Yes, I could have built this around a PowerShell class
[pscustomobject]@{
PSTypename = "companyServerInfo"
Computername = $os.csname
OperatingSystem = $os.caption
TopProcesses = $processes | Sort-Object WorkingsetSize -Descending | Select-Object -First $ProcessCount
FreeDiskGB = $cdrive.SizeRemaining / 1GB
FreeMemoryGB = $os.FreePhysicalMemory / 1mb
ReportDate = Get-Date
}
} #foreach session
#clear temporary sessions
if ($tempCS) {
Write-Verbose "Removing temporary CIMSessions"
$sessions | Remove-CimSession
}
Write-Verbose "Ending $($MyInvocation.MyCommand)"
} #end
} #close Get-Server
#region extensions
#add a type extension
#create an alias property
$td = @{
TypeName = "companyServerInfo"
MemberType = "AliasProperty"
MemberName = "os"
Value = "OperatingSystem"
force = $True
}
Update-TypeData @td
#load the custom format file
Update-FormatData $PSScriptRoot\companyserverinfo.format.ps1xml
#endregion