functions/Start-PSClock.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
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
Function Start-PSClock {
    [cmdletbinding()]
    [alias("psclock")]
    [OutputType("None", "PSClock")]
    Param(
        [Parameter(Position = 0, HelpMessage = "Specify a .NET format string value like F, or G.", ValueFromPipelineByPropertyName)]
        [alias("format")]
        [ValidateNotNullOrEmpty()]
        [string]$DateFormat = "F",

        [Parameter(ValueFromPipelineByPropertyName)]
        [ValidateScript({ $_ -gt 8 })]
        [alias("size")]
        [int]$FontSize = 18,

        [Parameter(HelpMessage = "Specify a font style.", ValueFromPipelineByPropertyName)]
        [ValidateSet("Normal", "Italic", "Oblique")]
        [alias("style")]
        [string]$FontStyle = "Normal",

        [Parameter(HelpMessage = "Specify a font weight.", ValueFromPipelineByPropertyName)]
        [ValidateSet("Normal", "Bold", "Light")]
        [alias("weight")]
        [string]$FontWeight = "Normal",

        [Parameter(HelpMessage = "Specify a font color like Green or an HTML code like '#FF1257EA'", ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [string]$Color = "White",

        [Parameter(HelpMessage = "Specify a font family.", ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [alias("family")]
        [string]$FontFamily = "Segoi UI",

        [Parameter(HelpMessage = "Do you want the clock to always be on top?", ValueFromPipelineByPropertyName)]
        [switch]$OnTop,

        [Parameter(HelpMessage = "Specify the clock position as an array of left and top values.", ValueFromPipelineByPropertyName)]
        [ValidateCount(2, 2)]
        [Int32[]]$Position,

        [Parameter(HelpMessage = "Force a new PSClock, ignoring any previously saved settings. The saved settings file will remain.")]
        [switch]$Force,

        [switch]$Passthru
    )

    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
    } #begin
    Process {
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Validating"
        if ($IsLinux -OR $isMacOS) {
            Write-Warning "This command requires a Windows platform."
            return
        }

        if ($PSClockSettings.Running) {
            Write-Warning "You already have a clock running. You can only have one clock running at a time."
            Return
        }

        if (Test-Path $env:temp\psclock-flag.txt) {
            $msg = @"
 
A running clock has been detected from another PowerShell session:
 
$(Get-Content $env:temp\psclock-flag.txt)
 
If this is incorrect, delete $env:temp\psclock-flag.txt and try again.
 
"@

            Write-Warning $msg
            $r = Read-Host "Do you want to remove the flag file? Y/N"
            if ($r -eq 'Y') {
                Remove-Item $env:temp\psclock-flag.txt
            }
            else {
                #bail out
                Return
            }
        }

        #verify the datetime format
        Try {
            [void](Get-Date -Format $DateFormat -ErrorAction Stop)
        }
        Catch {
            Write-Warning "The DateFormat value $DateFormat is not a valid format string. Try something like F,G, or U which are case-sensitive."
            Return
        }

        #Test if there is a saved settings file and no other parameters have been called
        # $SavePath is a module-scoped variable set in the psm1 file
        # $SavePath = Join-Path -Path $home -ChildPath PSClockSettings.xml
        if ((Test-Path $SavePath)-AND (-not $Force)) {
            Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Using saved settings"
            $import = Import-Clixml -Path $SavePath
            foreach ($prop in $import.psobject.properties) {
                Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Using imported value for $($prop.name)"
                Set-Variable -name $prop.name -value $prop.Value
            }
        }

        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Building a synchronized hashtable"
        $global:PSClockSettings = [hashtable]::Synchronized(@{
                DateFormat       = $DateFormat
                FontSize         = $FontSize
                FontStyle        = $FontStyle
                FontWeight       = $FontWeight
                Color            = $Color
                FontFamily       = $FontFamily
                OnTop            = $OnTop
                StartingPosition = $Position
                CurrentPosition  = $Null
            })
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] $($global:PSClockSettings | Out-String)"
        #Run the clock in a runspace
        $rs = [RunspaceFactory]::CreateRunspace()
        $rs.ApartmentState = "STA"
        $rs.ThreadOptions = "ReuseThread"
        $rs.Open()

        $global:PSClockSettings.add("Runspace", $rs)

        $rs.SessionStateProxy.SetVariable("PSClockSettings", $global:PSClockSettings)

        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Defining the runspace command"
        $psCmd = [PowerShell]::Create().AddScript({

                Add-Type -AssemblyName PresentationFramework -ErrorAction Stop
                Add-Type -AssemblyName PresentationCore -ErrorAction Stop
                Add-Type -AssemblyName WindowsBase -ErrorAction Stop

                # a private function to stop the clock and clean up
                Function _QuitClock {
                    $timer.stop()
                    $timer.isenabled = $False
                    $form.close()
                    $PSClockSettings.Running = $False

                    #define a thread job to clean up the runspace
                    $cmd = {
                        Param([int]$ID)
                        $r = Get-Runspace -Id $id
                        $r.close()
                        $r.dispose()
                    }
                    Start-ThreadJob -ScriptBlock $cmd -ArgumentList $PSClockSettings.runspace.id

                    #delete the flag file
                    if (Test-Path $env:temp\psclock-flag.txt) {
                        Remove-Item $env:temp\psclock-flag.txt
                    }
                }

                $form = New-Object System.Windows.Window

                <#
                some of the form settings are irrelevant because it is transparent
                but leaving them in the event I need to turn off transparency
                to debug or troubleshoot
                #>


                $form.Title = "PSClock"
                $form.Height = 200
                $form.Width = 400
                $form.SizeToContent = "WidthAndHeight"
                $form.AllowsTransparency = $True
                $form.Topmost = $PSClockSettings.Ontop

                $form.Background = "Transparent"
                $form.borderthickness = "1,1,1,1"
                $form.VerticalAlignment = "top"

                if ($PSClockSettings.StartingPosition) {
                    $form.left = $PSClockSettings.StartingPosition[0]
                    $form.top = $PSClockSettings.StartingPosition[1]
                }
                else {
                    $form.WindowStartupLocation = "CenterScreen"
                }
                $form.WindowStyle = "None"
                $form.ShowInTaskbar = $False

                #define events
                #call the private function to stop the clock and clean up
                $form.Add_MouseRightButtonUp({ _QuitClock })

                $form.Add_MouseLeftButtonDown({ $form.DragMove() })

                #press + to increase the size and - to decrease
                #the clock needs to refresh to see the result
                $form.Add_KeyDown({
                    switch ($_.key) {
                        { 'Add', 'OemPlus' -contains $_ } {
                            If ( $PSClockSettings.fontSize -ge 8) {
                                $PSClockSettings.fontSize++
                                $form.UpdateLayout()
                            }
                        }
                        { 'Subtract', 'OemMinus' -contains $_ } {
                            If ($PSClockSettings.FontSize -ge 8) {
                                $PSClockSettings.FontSize--
                                $form.UpdateLayout()
                            }
                        }
                    }
                })

                    #fail safe to remove flag file
                    $form.Add_Unloaded({
                        if (Test-Path $env:temp\psclock-flag.txt) {
                            Remove-Item $env:temp\psclock-flag.txt
                        }
                    })

                $stack = New-Object System.Windows.Controls.StackPanel

                $label = New-Object System.Windows.Controls.label
                $label.Content = Get-Date -Format $PSClockSettings.DateFormat

                $label.HorizontalContentAlignment = "Center"
                $label.Foreground = $PSClockSettings.Color
                $label.FontStyle = $PSClockSettings.FontStyle
                $label.FontWeight = $PSClockSettings.FontWeight
                $label.FontSize = $PSClockSettings.FontSize
                $label.FontFamily = $PSClockSettings.FontFamily

                $label.VerticalAlignment = "Top"

                $stack.AddChild($label)
                $form.AddChild($stack)

                $timer = New-Object System.Windows.Threading.DispatcherTimer
                $timer.Interval = [TimeSpan]"0:0:1.00"
                $timer.Add_Tick({
                        if ($PSClockSettings.Running) {
                            $label.Foreground = $PSClockSettings.Color
                            $label.FontStyle = $PSClockSettings.FontStyle
                            $label.FontWeight = $PSClockSettings.FontWeight
                            $label.FontSize = $PSClockSettings.FontSize
                            $label.FontFamily = $PSClockSettings.FontFamily
                            $label.Content = Get-Date -Format $PSClockSettings.DateFormat

                            $form.TopMost = $PSClockSettings.OnTop
                            $form.UpdateLayout()

                            #$PSClockSettings.Window = $Form
                            $PSClockSettings.CurrentPosition = $form.left,$form.top
                        }
                        else {
                            _QuitClock
                        }
                    })
                $timer.Start()

                $PSClockSettings.Running = $True
                $PSClockSettings.Started = Get-Date

                #Show the clock form
                [void]$form.ShowDialog()
            })

        $pscmd.runspace = $rs
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Launching the runspace"
        [void]$pscmd.BeginInvoke()

        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Creating the flag file $env:temp\psclock-flag.txt"
        "[{0}] PSClock started by {1} under PowerShell process id $pid" -f (Get-Date), $env:USERNAME |
        Out-File -filepath $env:temp\psclock-flag.txt

        if ($Passthru) {
            Start-Sleep -Seconds 1
            Get-PSClock
        }
    } #process
    End {
        Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
    } #end

} #close function