From 6af16969839c9989b971ccae7bbad53d53c8d466 Mon Sep 17 00:00:00 2001 From: asheroto Date: Tue, 20 Jun 2023 06:46:21 -0500 Subject: [PATCH] Add help, check for updates, version --- UninstallTeams.Tests.ps1 | 44 +++++++++++++++++++++++++++++ UninstallTeams.ps1 | 61 ++++++++++++++++++++++++++++++++++------ 2 files changed, 96 insertions(+), 9 deletions(-) diff --git a/UninstallTeams.Tests.ps1 b/UninstallTeams.Tests.ps1 index 3409352..e9535b2 100644 --- a/UninstallTeams.Tests.ps1 +++ b/UninstallTeams.Tests.ps1 @@ -60,6 +60,40 @@ Describe "Get-ChatWidgetStatus Function" { Remove-ItemProperty -Path "HKCU:\SOFTWARE\Policies\Microsoft\Windows\Windows Chat" -Name "ChatIcon" -Force } } + + Context "When -AllUsers switch is used without -EnableChatWidget, -DisableChatWidget, or -UnsetChatWidget" { + It "Should not throw an error" { + { Get-ChatWidgetStatus -AllUsers } | Should -Not -Throw + } + } + + Context "When -AllUsers switch is used with -EnableChatWidget" { + It "Should set the Chat widget status for all users" { + Set-ChatWidgetStatus -EnableChatWidget -AllUsers + $status = Get-ChatWidgetStatus -AllUsers + $status | Should -Be "Enabled" + # Disable the Chat widget for all users + Set-ChatWidgetStatus -DisableChatWidget -AllUsers + } + } + + Context "When -AllUsers switch is used with -DisableChatWidget" { + It "Should set the Chat widget status for all users" { + Set-ChatWidgetStatus -DisableChatWidget -AllUsers + $status = Get-ChatWidgetStatus -AllUsers + $status | Should -Be "Disabled" + # Enable the Chat widget for all users + Set-ChatWidgetStatus -EnableChatWidget -AllUsers + } + } + + Context "When -AllUsers switch is used with -UnsetChatWidget" { + It "Should set the Chat widget status for all users" { + Set-ChatWidgetStatus -UnsetChatWidget -AllUsers + $status = Get-ChatWidgetStatus -AllUsers + $status | Should -Be "Not Set" + } + } } Describe "Set-ChatWidgetStatus Function" { @@ -90,4 +124,14 @@ Describe "Set-ChatWidgetStatus Function" { $status | Should -Be "Not Set" } } + + Context "When -AllUsers switch is used" { + It "Should set the Chat widget status for all users" { + Set-ChatWidgetStatus -EnableChatWidget -AllUsers + $status = Get-ChatWidgetStatus -AllUsers + $status | Should -Be "Enabled" + # Disable the Chat widget for all users + Set-ChatWidgetStatus -DisableChatWidget -AllUsers + } + } } \ No newline at end of file diff --git a/UninstallTeams.ps1 b/UninstallTeams.ps1 index 0386148..f203add 100644 --- a/UninstallTeams.ps1 +++ b/UninstallTeams.ps1 @@ -78,9 +78,20 @@ param ( [switch]$DisableChatWidget, [switch]$UnsetChatWidget, [switch]$AllUsers, - [switch]$Version + [switch]$Version, + [switch]$Help, + [switch]$CheckForUpdates ) +# Version +$CurrentVersion = '1.0.0' + +# Check if -Version is specified +if ($Version.IsPresent) { + $CurrentVersion + exit 0 +} + function Get-ChatWidgetStatus { param ( [switch]$AllUsers @@ -153,12 +164,45 @@ function Set-ChatWidgetStatus { Write-Output "Chat widget has been $WhatChanged for $AllUsersString." } -# Version -$MyVersion = '1.0.0' +function Check-GitHubRelease { + param ( + [string]$Owner, + [string]$Repo + ) + try { + $url = "https://api.github.com/repos/$Owner/$Repo/releases/latest" + $response = Invoke-RestMethod -Uri $url -ErrorAction Stop -# Check if -Version is specified -if ($Version.IsPresent) { - $MyVersion + $latestVersion = $response.tag_name + $publishedAt = $response.published_at + + [PSCustomObject]@{ + LatestVersion = $latestVersion + PublishedAt = $publishedAt + } + } catch { + Write-Error "Unable to check for updates. Error: $_" + exit 1 + } +} + +# Help +if ($Help) { + Get-Help -Name $MyInvocation.MyCommand.Source -Full + exit 0 +} + +# Check for updates +if ($CheckForUpdates) { + $Data = Check-GitHubRelease + $LatestVersion = $Data.LatestVersion + $PublishedAt = $Data.PublishedAt + + if ($LatestVersion -gt $CurrentVersion) { + Write-Output "A new version of UninstallTeams is available. Current version: $CurrentVersion. Latest version: $LatestVersion. Published at: $PublishedAt." + } else { + Write-Output "UninstallTeams is up to date. Current version: $CurrentVersion. Latest version: $LatestVersion. Published at: $PublishedAt." + } exit 0 } @@ -184,9 +228,8 @@ try { Write-Output "" # Update note - $ScriptPath = $MyInvocation.MyCommand.Path - $ScriptVersion = (Get-Command $ScriptPath).FileVersionInfo.ProductVersion - Write-Output "The current version of this script is $ScriptVersion. To update to the latest version, run 'Update-Script UninstallTeams -Force'." + Write-Output "Uninstall Teams $ScriptVersion" + Write-Output "To check for updates, run UninstallTeams -CheckForUpdates" # Spacer Write-Output ""