Skip to content

Instantly share code, notes, and snippets.

@techdecline
Created June 15, 2021 07:21
Show Gist options
  • Select an option

  • Save techdecline/9c48a9fd2ddc32fdd047618a3154921d to your computer and use it in GitHub Desktop.

Select an option

Save techdecline/9c48a9fd2ddc32fdd047618a3154921d to your computer and use it in GitHub Desktop.
Update Adobereader Using Proactive Remediation
[cmdletbinding()]
param (
[Parameter(Mandatory=$false)]
[String]$PackageName = "adobereader"
)
function Test-Choco
{
[CmdletBinding()]
param (
)
Process
{
if (-not (Test-Path "$env:ALLUSERSPROFILE\Chocolatey\bin\choco.exe")) {
return
}
else {
Write-Verbose "The Chocolatey Executable variable was found."
return $true
}
}
}
function Get-ChocoInstalledApps {
$chocoExe = (Get-Command "choco.exe" -ErrorAction Stop | Where-Object {$_.CommandType -eq "Application"}).Source
$cmd = $chocoExe + " list --localonly"
$rtnArr = Invoke-Expression $cmd | Select-Object -SkipLast 1 | Select-Object @{name='PackageName';expression={($_ -split " ")[0]}},
@{name='PackageVersion';expression={($_ -split " ")[1]}}
return $rtnArr
}
function Get-ChocoOutdatedApps {
$chocoExe = (Get-Command "choco.exe" -ErrorAction Stop | Where-Object {$_.CommandType -eq "Application"}).Source
$cmd = $chocoExe + " outdated"
$rtnArr = Invoke-Expression $cmd | Where-Object {$_ -match "^(\w|\d).*\|.*\|.*\|.*$"} | Select-Object -Property @{name='PackageName';expression={($_ -split "\|")[0]}},
@{name='InstallVersion';expression={($_ -split "\|")[1]}},
@{name='LatestVersion';expression={($_ -split "\|")[2]}}
return $rtnArr
}
## Preflight
if (-not (Test-Choco)) {
Write-Warning "Chocolatey is not installed"
return 1
}
## Query installed Packages
$installedPackageArr = Get-ChocoInstalledApps
$outdatedPackageArr = Get-ChocoOutdatedApps
Write-Verbose "Package Name is: $PackageName"
if ($installedPackageArr.PackageName -notcontains $PackageName) {
Write-Output "The selected package is not installed"
Exit 0
}
elseif ($outdatedPackageArr.PackageName -notcontains $PackageName) {
Write-Output "The selected package is already updated"
Exit 0
}
else {
Write-Output "The selected package needs to be updated"
Exit 1
}
[cmdletbinding(DefaultParameterSetName="ByPackageName",SupportsShouldProcess=$true)]
param (
[Parameter(Mandatory=$false,ParameterSetName="ByPackageName")]
[String]$PackageName = "adobereader",
[Parameter(Mandatory,ParameterSetName="ByGlobalList")]
[Switch]$UpdateAll,
[Parameter(Mandatory = $false)]
[String]$LogoImageUri = "https://windeployprodst.blob.core.windows.net/public/areader.png"
)
function Test-Choco
{
[CmdletBinding()]
param (
)
Process
{
if (-not (Test-Path "$env:ALLUSERSPROFILE\Chocolatey\bin\choco.exe")) {
return
}
else {
Write-Verbose "The Chocolatey Executable variable was found."
return $true
}
}
}
function Show-ToastNotification() {
$Load = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
$Load = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]
# Load the notification into the required format
$ToastXML = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
$ToastXML.LoadXml($Toast.OuterXml)
# Display the toast notification
try {
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($App).Show($ToastXml)
}
catch {
Write-Output -Message 'Something went wrong when displaying the toast notification' -Level Warn
Write-Output -Message 'Make sure the script is running as the logged on user' -Level Warn
}
}
function Get-ChocoInstalledApps {
$chocoExe = (Get-Command "choco.exe" -ErrorAction Stop | Where-Object {$_.CommandType -eq "Application"}).Source
$cmd = $chocoExe + " list --localonly"
$rtnArr = Invoke-Expression $cmd | Select-Object -SkipLast 1 | Select-Object @{name='PackageName';expression={($_ -split " ")[0]}},
@{name='PackageVersion';expression={($_ -split " ")[1]}}
return $rtnArr
}
function Get-ChocoOutdatedApps {
$chocoExe = (Get-Command "choco.exe" -ErrorAction Stop | Where-Object {$_.CommandType -eq "Application"}).Source
$cmd = $chocoExe + " outdated"
$rtnArr = Invoke-Expression $cmd | Where-Object {$_ -match "^(\w|\d).*\|.*\|.*\|.*$"} | Select-Object -Property @{name='PackageName';expression={($_ -split "\|")[0]}},
@{name='InstallVersion';expression={($_ -split "\|")[1]}},
@{name='LatestVersion';expression={($_ -split "\|")[2]}}
return $rtnArr
}
function Update-ChocoPackage {
param (
[Parameter(Mandatory)]
[string]$PackageName
)
$chocoExe = (Get-Command "choco.exe" -ErrorAction Stop | Where-Object {$_.CommandType -eq "Application"}).Source
$cmd = $chocoExe + " upgrade $PackageName -y"
$null = Invoke-Expression $cmd
return $?
}
# Start Script Execution
## Preflight
if (-not (Test-Choco)) {
Write-Warning "Chocolatey is not installed"
return 1
}
## Setting image variables
$LogoImage = "$env:TEMP\ToastLogoImage.png"
## Fetching images from uri
Invoke-WebRequest -Uri $LogoImageUri -OutFile $LogoImage
#Defining the Toast notification settings
#ToastNotification Settings
$Scenario = 'short'
# Load Toast Notification text
$AttributionText = "Fritz Kola"
$HeaderText = "Application Update: $PackageName"
$TitleText = "Application Update $PackageName"
$BodyText1 = "Your application may close unexpectedly"
$BodyText2 = "Please save your work and close it manually"
# Check for required entries in registry for when using Powershell as application for the toast
# Register the AppID in the registry for use with the Action Center, if required
$RegPath = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings'
$App = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
# Creating registry entries if they don't exists
if (-NOT(Test-Path -Path "$RegPath\$App")) {
New-Item -Path "$RegPath\$App" -Force
New-ItemProperty -Path "$RegPath\$App" -Name 'ShowInActionCenter' -Value 1 -PropertyType 'DWORD'
}
# Make sure the app used with the action center is enabled
if ((Get-ItemProperty -Path "$RegPath\$App" -Name 'ShowInActionCenter' -ErrorAction SilentlyContinue).ShowInActionCenter -ne '1') {
New-ItemProperty -Path "$RegPath\$App" -Name 'ShowInActionCenter' -Value 1 -PropertyType 'DWORD' -Force
}
# Formatting the toast notification XML
[xml]$Toast = @"
<toast scenario="$Scenario">
<visual>
<binding template="ToastGeneric">
<image placement="hero" src="$HeroImage"/>
<image id="1" placement="appLogoOverride" hint-crop="circle" src="$LogoImage"/>
<text placement="attribution">$AttributionText</text>
<text>$HeaderText</text>
<group>
<subgroup>
<text hint-style="title" hint-wrap="true" >$TitleText</text>
</subgroup>
</group>
<group>
<subgroup>
<text hint-style="body" hint-wrap="true" >$BodyText1</text>
</subgroup>
</group>
<group>
<subgroup>
<text hint-style="body" hint-wrap="true" >$BodyText2</text>
</subgroup>
</group>
</binding>
</visual>
<actions>
<action activationType="system" arguments="dismiss" content="$DismissButtonContent"/>
</actions>
</toast>
"@
#Send the notification
Show-ToastNotification
## Query installed Packages
$installedPackageArr = Get-ChocoInstalledApps
$outdatedPackageArr = Get-ChocoOutdatedApps
## Determine Execution Mode
Write-Verbose "Parameter Set Name is: $($PSCmdlet.ParameterSetName)"
switch ($PSCmdlet.ParameterSetName) {
"ByPackageName" {
Write-Verbose "Package Name is: $PackageName"
if ($installedPackageArr.PackageName -notcontains $PackageName) {
Write-Warning "The selected package is not installed"
return 0
}
elseif ($outdatedPackageArr.PackageName -notcontains $PackageName) {
Write-Verbose "The selected package is already updated"
return 0
}
else {
Write-Verbose "The selected package will be updated"
if ($PSCmdlet.ShouldProcess($PackageName)) {
$updateRtn = Update-ChocoPackage -PackageName $PackageName
return $updateRtn
}
}
}
"ByGlobalList" {
Write-Warning "This has not been implemented yet"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment