-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Get-Status.ps1
221 lines (187 loc) · 7.95 KB
/
Get-Status.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
#requires -version 5.1
#requires -module PSScriptTools
[CmdletBinding()]
Param()
Function Get-Status {
[cmdletbinding(DefaultParameterSetName = 'name')]
[alias("gst")]
Param(
[Parameter(
Position = 0,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
HelpMessage = 'Enter the name of a computer',
ParameterSetName = 'name')
]
[ValidateNotNullOrEmpty()]
[string]$Computername = $env:computername,
[Parameter(ParameterSetName = 'name')]
[pscredential]$Credential,
[Parameter(ParameterSetName = 'Session', ValueFromPipeline)]
[CimSession]$Cimsession,
[switch]$AsString,
[Parameter(HelpMessage="Enable with grapical trace window")]
[switch]$Trace
)
Begin {
Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN ] Starting $($MyInvocation.MyCommand)"
if ($trace) {
Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN ] Using Trace"
$global:TraceEnabled = $True
$traceTitle = "{0} Trace Log" -f $($MyInvocation.MyCommand)
Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN ] $traceTitle"
Trace-Message -Title $traceTitle
Trace-Message "Starting $($MyInvocation.MyCommand)"
}
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Using parameter set $($PSCmdlet.ParameterSetName)"
Trace-Message -message "Using parameter set: $($PSCmdlet.ParameterSetName)"
$sessParams = @{
ErrorAction = 'stop'
computername = $null
}
$cimParams = @{
ErrorAction = 'stop'
classname = $null
}
if ($PSCmdlet.ParameterSetName -eq 'name') {
Trace-Message -message "Create a temporary Cimsession"
$sessParams.Computername = $Computername
if ($Credential) {
$sessParams.Credential = $credential
}
Try {
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Creating temporary cimsession to $computername"
$Cimsession = New-CimSession @sessParams
$tempsession = $True
}
catch {
Write-Error $_
#bail out
return
}
}
if ($Cimsession) {
$hash = [ordered]@{
Computername = $cimsession.computername.ToUpper()
}
Try {
$cimParams.classname = 'Win32_OperatingSystem'
$cimParams.CimSession = $Cimsession
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Querying $($cimparams.classname)"
Trace-Message "Querying $($cimparams.classname)"
$OS = Get-CimInstance @cimParams
$uptime = (Get-Date) - $OS.lastBootUpTime
$hash.Add("Uptime", $uptime)
$pctFreeMem = [math]::Round(($os.FreePhysicalMemory / $os.TotalVisibleMemorySize) * 100, 2)
$hash.Add("PctFreeMem", $pctFreeMem)
$cimParams.classname = 'Win32_Logicaldisk'
$cimParams.filter = "drivetype=3"
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Querying $($cimparams.classname)"
Trace-Message "Querying $($cimparams.classname)"
Get-CimInstance @cimParams | ForEach-Object {
$name = "PctFree{0}" -f $_.deviceid.substring(0, 1)
$pctFree = [math]::Round(($_.FreeSpace / $_.size) * 100, 2)
$hash.add($name, $pctfree)
}
Trace-Message -message "Creating new object"
$hash | Out-String | Trace-Message
$status = New-Object PSObject -Property $hash
if ($AsString) {
Trace-Message "Formatting result as a string"
$upstring = $uptime.ToString().substring(0, $uptime.ToString().LastIndexOf("."))
if (($IsWindows -AND $IsCoreCLR) -OR ($PSEdition -eq 'Desktop')) {
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Formatting for PowerShell 7.x"
Trace-Message -message "Formatting for PowerShell 7.x"
#strip the milliseconds off the uptime
$string = "$([char]0x1b)[38;5;47m{0}$([char]0x1b)[0m Up:{1}" -f $status.computername, $upstring
}
Else {
$string = "{0} Up:{1}" -f $status.computername, $upstring
}
#Get free properties
$free = $status.PSObject.properties | Where-Object Name -match PctFree
foreach ($item in $free) {
$sName = $item.name -replace "Pct", "%"
if (($IsWindows -AND $IsCoreCLR) -OR ($PSEdition -eq 'Desktop')) {
#Colorize values
Trace-Message -message "Colorizing output"
if ([double]$item.value -le 20) {
#red
$value = "$([char]0x1b)[91m$($item.value)$([char]0x1b)[0m"
}
elseif ([double]$item.value -le 50) {
#yellow
$value = "$([char]0x1b)[93m$($item.value)$([char]0x1b)[0m"
}
else {
#green
$value = "$([char]0x1b)[92m$($item.value)$([char]0x1b)[0m"
}
}
else {
$value = $item.Value
}
$string += " {0}:{1}" -f $sname, $value
} #foreach item in free
$string
}
else {
$status
}
}
catch {
Write-Error $_
}
#only remove the cimsession if it was created in this function
if ($tempsession) {
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Removing temporary cimsession"
Trace-Message "Removing temporary cimsession"
Remove-CimSession -CimSession $Cimsession
}
} #if cimsession
} #process
End {
Write-Verbose "[$((Get-Date).TimeOfDay) END ] Ending $($MyInvocation.MyCommand)"
Trace-Message -message "Ending $($MyInvocation.MyCommand)"
#make sure tracing is turned off
$global:TraceEnabled = $False
} #end
} #close function
$data = Get-Status -trace
#define the Escape character
$e = "$([char]0x1b)"
# a helper function to format values with ANSI escape codes
Function ansifmt {
Param([double]$Value)
if ($value -le 20) {
#red
"$([char]0x1b)[91m$($value)$([char]0x1b)[0m"
}
elseif ($value -le 50) {
#yellow
"$([char]0x1b)[93m$($value)$([char]0x1b)[0m"
}
else {
#green
"$([char]0x1b)[92m$($value)$([char]0x1b)[0m"
}
}
#format values
$mem = "{0:00.00}" -f $data.pctFreemem
$disk = "{0:00.00}" -f $data.PctFreeC
#format the values for the graph
$pctMem = $data.PctFreeMem/100
$pctDisk = $data.PctFreeC/100
#add some style to the computername
$comp = "$(New-ANSIBar -Range (235..245) -Gradient)$e[38;5;200m $($data.Computername) $e[0m$(New-ANSIBar -Range (245..235) -Gradient)"
$head = Add-Border -text $comp -Character $psspecialchar.diamond -ANSIBorder "$e[93m" | Out-String
$out = @"
$head
Uptime : $($data.uptime) $e[92m$($psspecialchar.uptriangle)$e[0m
%FreeMemory : $(ansifmt $mem) $(New-RedGreenGradient -percent $pctMem -Character $psspecialchar.lozenge)
%FreeC : $(ansifmt $disk) $(New-RedGreenGradient -percent $pctDisk -Character $psspecialchar.BlackSquare)
Date : $(Get-Date -format u)
"@
$out