PSCommander.Extensions.psm1

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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
function New-CommanderSimplePerformanceInfo
{
    <#
    .SYNOPSIS
    Creates a simple performance info bar.
    
    .DESCRIPTION
    Creates a simple performance info bar.
    
    .PARAMETER Height
    Height
    
    .PARAMETER Width
    Width
    
    .PARAMETER Top
    Top position.
    
    .PARAMETER Left
    Left position
    #>

    param(
        [Parameter()]
        [int]$Height = 200,
        [Parameter()]
        [int]$Width = 1400,
        [Parameter()]
        [int]$Top = 20,
        [Parameter()]
        [int]$Left = 100
    )

    Register-CommanderDataSource -Name 'ComputerInfo' -LoadData {
        $Stats = Get-NetAdapterStatistics
        $NetworkDown = 0
        $Stats.ReceivedBytes | Foreach-Object { $NetworkDown += $_ } 
        
        $NetworkUp = 0
        $Stats.SentBytes | Foreach-Object { $NetworkUp += $_ } 
            
        @{
            CPU = Get-CimInstance Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select-Object -Expand Average
            Memory = (Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue
            NetworkUp = $NetworkUp / 1KB
            NetworkDown = $NetworkDown / 1KB
            Space = (Get-PSDrive C).Free / 1GB
        }
    } -RefreshInterval 5
     
    New-CommanderDesktopWidget -LoadWidget {
        [xml]$Form = Get-Content ("$PSScriptRoot\XAML\SimplePerformanceInfo.xaml") -Raw
            $XMLReader = (New-Object System.Xml.XmlNodeReader $Form)
            [Windows.Markup.XamlReader]::Load($XMLReader)
    } -Height $Height -Width $Width -Top $Top -Left $Left -DataSource 'ComputerInfo'
}

function New-CommanderDriveSpaceGauge
{
    <#
    .SYNOPSIS
    Creates a simple gauge to display drive space.
    
    .DESCRIPTION
    Creates a simple gauge to display drive space.
    
    .PARAMETER DriveLetter
    The drive letter to display the space for.
    
    .PARAMETER Height
    Widget height
    
    .PARAMETER Width
    Widget width
    
    .PARAMETER Top
    Widget top offset
    
    .PARAMETER Left
    Widget left offset.
    
    .EXAMPLE
    New-CommanderDriveSpaceGauage -DriveLetter C
    #>

    param(
        [Parameter(Mandatory)]
        [char]$DriveLetter,
        [Parameter()]
        [int]$Height = 200,
        [Parameter()]
        [int]$Width = 200,
        [Parameter()]
        [int]$Top = 50,
        [Parameter()]
        [int]$Left = 50
    )

    Register-CommanderDataSource -Name "DriveSpace_$DriveLetter" -LoadData {
        $Drive = Get-PSDrive $args[0]
        @{
            Used = [Math]::Round($Drive.Used / 1GB)
            Total = [Math]::Round(($Drive.Free + $Drive.Used) / 1GB)
            Drive = $args[0]
        }
    } -RefreshInterval 60 -ArgumentList @($DriveLetter)

    New-CommanderDesktopWidget -LoadWidget {
        [xml]$Form = Get-Content ("$PSScriptRoot\XAML\DriveSpace.xaml") -Raw
            $XMLReader = (New-Object System.Xml.XmlNodeReader $Form)
            [Windows.Markup.XamlReader]::Load($XMLReader)
    } -Height $Height -Width $Width -Top $Top -Left $Left -DataSource "DriveSpace_$DriveLetter"
}

function New-CommanderTimeAndDate 
{
    <#
    .SYNOPSIS
    Displays the date and time
    
    .DESCRIPTION
    Displays the date and time
    
    .PARAMETER Height
    Widget height
    
    .PARAMETER Width
    Widget width
    
    .PARAMETER Top
    Widget top offset
    
    .PARAMETER Left
    Widget left offset.
    
    .EXAMPLE
    New-CommanderTimeAndDate
    #>

    param(
        [Parameter()]
        [int]$Height = 200,
        [Parameter()]
        [int]$Width = 300,
        [Parameter()]
        [int]$Top = 50,
        [Parameter()]
        [int]$Left = 50,
        [Parameter()]
        [System.TimeZoneInfo]$TimeZone
    )

    Register-CommanderDataSource -Name "Time" -LoadData {
        if ($args[0] -ne $null)
        {
            $Date = [TimeZoneInfo]::ConvertTime((Get-Date), $args[0])
            @{
                TimeZone = $args[0].Id
                Date = $Date
            }
        }
        else 
        {
            @{
                Date = Get-Date 
            }
        }
    } -RefreshInterval 60 -ArgumentList @($TimeZone)

    New-CommanderDesktopWidget -LoadWidget {
        [xml]$Form = Get-Content ("$PSScriptRoot\XAML\Clock.xaml") -Raw
            $XMLReader = (New-Object System.Xml.XmlNodeReader $Form)
            [Windows.Markup.XamlReader]::Load($XMLReader)
    } -Height $Height -Width $Width -Top $Top -Left $Left -DataSource "Time"
}

function New-CommanderComputerInfo 
{
    <#
    .SYNOPSIS
    Displays computer information.
    
    .DESCRIPTION
    Displays computer information.
    
    .PARAMETER Height
    Widget height
    
    .PARAMETER Width
    Widget width
    
    .PARAMETER Top
    Widget top offset
    
    .PARAMETER Left
    Widget left offset.
    
    .EXAMPLE
    New-CommanderComputerInfo

    #>

    param(
        [Parameter()]
        [int]$Height = 600,
        [Parameter()]
        [int]$Width = 400,
        [Parameter()]
        [int]$Top = 50,
        [Parameter()]
        [int]$Left = 50
    )

    Register-CommanderDataSource -Name "ComputerInfo" -LoadData {
        Get-ComputerInfo
    } -RefreshInterval 60

    New-CommanderDesktopWidget -LoadWidget {
        [xml]$Form = Get-Content ("$PSScriptRoot\XAML\ComputerInfo.xaml") -Raw
            $XMLReader = (New-Object System.Xml.XmlNodeReader $Form)
            [Windows.Markup.XamlReader]::Load($XMLReader)
    } -Height $Height -Width $Width -Top $Top -Left $Left -DataSource "ComputerInfo"
}


function New-CommanderTicker 
{
    <#
    .SYNOPSIS
    Displays a stock or crypto ticker
    
    .DESCRIPTION
    Displays a stock or crypto ticker
    
    .PARAMETER Height
    Widget height
    
    .PARAMETER Width
    Widget width
    
    .PARAMETER Top
    Widget top offset
    
    .PARAMETER Left
    Widget left offset.

    .PARAMETER StockSymbol
    Stock symbol

    .PARAMETER CryptoSymbol
    Crypto symbol
    
    .EXAMPLE
    New-CommanderTicker -StockSymbol MSFT -ApiKey ''

    #>

    param(
        [Parameter()]
        [int]$Height = 600,
        [Parameter()]
        [int]$Width = 400,
        [Parameter()]
        [int]$Top = 50,
        [Parameter()]
        [int]$Left = 50,
        [Parameter(Mandatory, ParameterSetName = "Stock")]
        [string]$StockSymbol,
        [Parameter(Mandatory, ParameterSetName = "Crypto")]
        [string]$CryptoSymbol,
        [Parameter(ParameterSetName = "Crypto")]
        [string]$CryptoTarget = 'USD',
        [Parameter(Mandatory)]
        [string]$ApiKey
    )

    $Symbol = $CryptoSymbol 
    if ($StockSymbol)
    {
        $Symbol = $StockSymbol
    }

    Register-CommanderDataSource -Name "Stock_$Symbol" -LoadData {

        $Symbol = $Args[0]
        $ApiKey = $Args[1]
        $Type = $Args[2]
        $CryptoTarget = $Args[3]

        $Date = Get-Date
        if ($Date.DayOfWeek -eq [DayOfWeek]::Monday)
        {
            $Date = $Date.AddDays(-2)
        }

        if ($Date.DayOfWeek -eq [DayOfWeek]::Saturday)
        {
            $Date = $Date.AddDays(-1)
        }

        $StrDate = $Date.ToString("yyyy-MM-dd")

        if ($Type -eq 'Crypto')
        {
            $Uri = "v1/open-close/crypto/$Symbol/USD/$($StrDate)?apiKey=$ApiKey"
        }
        else 
        {
            $Uri = "v1/open-close/$Symbol/$($StrDate)?unadjusted=true&apiKey=$ApiKey"
        }
    
        $Result = Invoke-RestMethod "https://api.polygon.io/$Uri" -ErrorAction SilentlyContinue

        if ($Result.Close -lt $Result.Open)
        {
            $ForegroundColor = "#c63629"
        }
        else 
        {
            $ForegroundColor = "#28bf37"
        }

        if ($Type -eq 'Crypto')
        {
            $Symbol = "$Symbol-$CryptoTarget"
        }

        @{
            Symbol = $Symbol
            Close = $Result.Close
            Open = $Result.Open
            ForegroundColor = $ForegroundColor
        }
    } -RefreshInterval 86400 -ArgumentList @($Symbol, $ApiKey, $PSCmdlet.ParameterSetName, $CryptoTarget)

    New-CommanderDesktopWidget -LoadWidget {
        [xml]$Form = Get-Content ("$PSScriptRoot\XAML\Ticker.xaml") -Raw
        $XMLReader = (New-Object System.Xml.XmlNodeReader $Form)
        [Windows.Markup.XamlReader]::Load($XMLReader)
    } -Height $Height -Width $Width -Top $Top -Left $Left -DataSource "Stock_$Symbol"
}