functions/Watch-DbaUpdate.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
Function Watch-DbaUpdate
{
<#
.SYNOPSIS
Just for fun - checks the PowerShell Gallery ever few hours for updates to dbatools - notifies max every 6 hours.
 
.DESCRIPTION
Only supports Windows 10. Not sure how to make the notification last longer (like Slack does).
  
Anyone know how to make it clickable so that it opens an URL?
 
.NOTES
Tags: JustForFun
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
 
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
.LINK
https://dbatools.io/Watch-DbaUpdate
 
.EXAMPLE
Watch-DbaUpdate
 
Watches the gallery for updates to dbatools.
#>
    

    PROCESS
    {
        if (([Environment]::OSVersion).Version.Major -lt 10)
        {
            Write-Warning "This command only supports Windows 10 and above"
            return
        }
        
        if ($null -eq (Get-ScheduledTask -TaskName "dbatools version check" -ErrorAction SilentlyContinue))
        {
            Install-DbaWatchUpdate
        }
        
        # leave this in for the scheduled task
        $module = Get-Module -Name dbatools
        
        if (!$module)
        {
            Import-Module dbatools
            $module = Get-Module -Name dbatools
        }
        
        $galleryversion = (Find-Module -Name dbatools -Repository PSGallery).Version
        $localversion = $module.Version
        
        if ($galleryversion -le $localversion) { return }
        
        $file = "$env:LOCALAPPDATA\dbatools\watchupdate.xml"
        
        $new = [pscustomobject]@{
            NotifyVersion = $galleryversion
        }
        
        # now that notifications stay until they are checked, we just have to keep
        # track of the last version we notified about
        
        if (Test-Path $file)
        {
            $old = Import-Clixml -Path $file -ErrorAction SilentlyContinue
            
            if ($galleryversion -gt $old.NotifyVersion)
            {
                Export-Clixml -InputObject $new -Path $file
                Show-Notification
            }
        }
        else
        {
            $directory = Split-Path $file
            
            if (!(Test-Path $directory))
            {
                $null = New-Item -ItemType Directory -Path $directory
            }
            
            Export-Clixml -InputObject $new -Path $file
            Show-Notification
        }
    }
}