Created
May 3, 2016 18:38
-
-
Save Graham-Beer/dc7f47a4dcc03435a6bb942198f2ba1d to your computer and use it in GitHub Desktop.
App-V PowerShell removal script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Remove Visio 2010 AppV Package | |
.DESCRIPTION | |
Controller script to invoke 'Remove-Package' cmdlet | |
.EXAMPLE | |
. .\$PSScriptRoot\Remove-AppvPackage.ps1 | |
.LINK | |
Links to further documentation | |
.NOTES | |
Version : 1.0 | |
Date Created : 28/04/2016 | |
Created by : Graham Beer | |
#> | |
#Dot source the script | |
. $PSScriptRoot\Remove-AppvPackage.ps1 | |
#splat the arguements to pass to 'Remove-AppvPackage' cmdlet | |
$RemoveVisio = @{ | |
AppVPackage = 'Microsoft_Visio_Standard_2010SP1' | |
Shortcut = 'Microsoft Visio 2010.lnk' | |
FlagFileName = 'Visio_2010_Removal' # .flg extension created by 'Remove-AppvPackage.ps1' script. | |
} | |
#Run the code to remove Visio 2010 | |
Remove-AppvPackage @RemoveVisio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Remove Appv from device | |
.DESCRIPTION | |
This script has been created for the removal of AppV applications published to a user | |
.EXAMPLE | |
remove-Appv -AppVPackage 'SomeApp' -shortcut "file.lnk" -FlagFileName Marker | |
remove-Appv -AppVPAckage 'Microsoft_Visio_Standard_2010SP1' -Shortcut "Microsoft Visio 2010.lnk" | |
.INPUTS | |
Add APPV package and shortcut file to remove | |
.OUTPUTS | |
If the application removes successfully it will create a flag file in c:\windows\temp | |
.NOTES | |
General notes | |
.COMPONENT | |
SCCM / AppV | |
.Details | |
Created by G Beer | |
27/04/2016 | |
#> | |
Function Remove-AppvPackage { | |
[cmdletbinding()] | |
param ( | |
[Parameter(Mandatory=$true)] | |
[ValidateScript({ test-connection $ENV:COMPUTERNAME -quiet })] | |
[string]$AppVPackage, | |
[Parameter()] | |
[string]$Shortcut, | |
[Parameter()] | |
[string]$FlagFileName | |
) | |
Begin | |
{ | |
#create log file | |
try { | |
$Path = 'C:\Windows\Temp' | |
$FileName = (Get-Date).tostring("ddMMyyyy-hhmmss") | |
$LogFile = "AppV_Removal_" + $FileName + ".log" | |
New-Item -itemType File -Path C:\Windows\Temp -Name $logfile | out-null | |
} Catch [exception] { | |
Write-Warning $_ | |
} | |
#Set log file header | |
Write-Output "[ Title ] Removal of $AppVPackage`n" | Add-Content -Path $($path + "\" + $LogFile) | |
Write-Output "[ DateTime ] $(get-date)`n" | Add-Content -Path $($path + "\" + $LogFile) | |
}#End Begin block | |
process | |
{ | |
#Get application | |
$AppvPKG = Get-AppvClientPackage -all | ? { $_.name -eq $AppVPackage } | |
if ($AppvPKG -eq $null) { | |
Write-Output "[ Error ] App-V Package Not Found`n" | Add-Content -Path $($path + "\" + $LogFile) ; break | |
} else { | |
Write-Output "[ Success ] App-V Package $AppVPackage Found`n" | Add-Content -Path $($path + "\" + $LogFile) | |
} | |
#Add error control | |
try { | |
$AppvPKG | Publish-AppvClientPackage -Global -ErrorAction Stop | Out-Null #re-publish the application globally | |
Write-Output "[ Success ] Republished $AppVPackage to global as to overwrite previous installation settings`n" | Add-Content -Path $($path + "\" + $LogFile) | |
} catch [exception] { | |
Write-Output "[ Error ] $_" | Add-Content -Path $($path + "\" + $LogFile) ; break | |
} | |
#Get application | |
$GlobalAppvPKG = Get-AppvClientPackage -all | ? { $_.name -eq $AppVPackage } | |
try { | |
$GlobalAppvPKG | Unpublish-AppvClientPackage -Global -ErrorAction Stop | Out-Null | |
Write-Output "[ Success ] globally UnPublished $AppVPackage package in readiness for removal`n" | Add-Content -Path $($path + "\" + $LogFile) | |
$GlobalAppvPKG | Remove-AppvClientPackage -ErrorAction Stop | Out-Null | |
Write-Output "[ Success ] Removed all traces of package $AppVPackage`n" | Add-Content -Path $($path + "\" + $LogFile) | |
} catch [exception] { | |
Write-Output "[ Error ] $_" | Add-Content -Path $($path + "\" + $LogFile) ; break | |
} | |
#remove shortcut link from machine | |
#Excluding C:\Users\Public as it contains a different directory structure and UnPublish cmdlet removes if in Public directory | |
$users = ((Get-ChildItem C:\Users).Name).GetEnumerator() | ? { $_ -ne "Public" } | |
#Run check against all users | |
$exist = foreach ($user in $users) | |
{ | |
Get-ChildItem "C:\Users\$user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" -Filter $shortcut -Recurse | |
} | |
if ( ($exist | Measure-Object).Count -eq 0 ) { | |
Write-Output "[ Information ] Shortcut does not exist" | Add-Content -Path $($path + "\" + $LogFile) | |
} else { | |
try { | |
$ErrorActionPreference = "stop" | |
$exist | foreach { $_.delete() ; Write-Output "[ Information ] Shortcut remove from $_ `n" | Add-Content -Path $($path + "\" + $LogFile) } | |
} catch [exception] { | |
Write-Output "[ Error ] $_" | Add-Content -Path $($path + "\" + $LogFile) ; break | |
} | |
} | |
}#End of Process block | |
End | |
{ | |
#Post removal check | |
$Check = Get-AppvClientPackage -all | ? { $_.name -eq $AppvPKG } | |
if ([boolean]$Check) { | |
Write-Output "[ Error ] Failed to remove $AppVPackage" | Add-Content -Path $($path + "\" + $LogFile) ; break | |
} else { | |
New-Item -Path $Path -Name $($FlagFileName + ".flg") -ItemType File -Force | Out-Null | |
Write-Output "[ Completed ] Successfully removed $AppVPackage" | Add-Content -Path $($path + "\" + $LogFile) | |
} | |
}#End of 'End' block | |
}#End of function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment