# installChocoApps.ps1
[string[]]$computers=@(
'SERVER01',
'SERVER02',
'SERVER03'
)
[string]$chocoAppName='pgadmin4'
[version]$minVersion='6.0'
function installChocoApp{
param(
[string[]]$computers=$env:computername,
[string]$chocoAppName='Firefox',
[version]$minVersion='7.0'
)
$results=[hashtable]@{}
foreach ($computer in $computers){
$session=new-pssession $computer
if($session.State -eq 'Opened'){
$result=invoke-command -computer $computer -scriptblock{
param($chocoAppName,$minVersion)
# Install Chocolatey
if (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Set-ExecutionPolicy Bypass -Scope Process -Force;
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))}
$chocoList=choco list -l
$installedApps=$chocoList|%{
$appLine=([regex]'^([\w-_]+)\s([\d\.]+)$').matches($_).Value;
if($appLine){$appLine}
}
$appsList=.{
$apps=[hashtable]@{}
foreach($x in $installedApps){
if($null -ne $x){
$y=$x.Split(' ')
$apps+=@{$y[0]=$y[1]}
}
}
return $apps
}
$installedVersion=$appsList[$chocoAppName]
[bool]$toUpgrade=[version]$minVersion -ge [version]$installedVersion
#[version]$installedVersion=if($alreadyInstalled){$appsList.$chocoAppName}else{$null}
if(!$toUpgrade){
write-host "$chocoAppName $installedVersion has already been installed on $env:computername" -ForegroundColor Green
return $true
}elseif($toUpgrade){
write-host "Upgrading $chocoAppName $installedVersion to $minVersion or higher..."
$null=choco upgrade $chocoAppName -y
$chocoList=choco list -l
$installedApps=$chocoList|%{
$appLine=([regex]'^([\w-_]+)\s([\d\.]+)$').matches($_).Value;
if($appLine){$appLine}
}
$appsList=.{
$apps=[hashtable]@{}
foreach($x in $installedApps){
if($null -ne $x){
$y=$x.Split(' ')
$apps+=@{$y[0]=$y[1]}
}
}
return $apps
}
[bool]$installedSuccess=$chocoAppName -in $appsList.GetEnumerator().Name
if($installedSuccess){
write-host "$chocoAppName has been upgraded on $env:computername successfully" -ForegroundColor Green
return $true
}else{
write-host "$chocoAppName has NOT been upgraded on $env:computername successfully" -ForegroundColor Yellow
return $false
}
}else{
write-host "$chocoAppName is being installed on $env:computername..."
$null=choco install $chocoAppName -y
$chocoList=choco list -l
$installedApps=$chocoList|%{
$appLine=([regex]'^([\w-_]+)\s([\d\.]+)$').matches($_).Value;
if($appLine){$appLine}
}
$appsList=.{
$apps=[hashtable]@{}
foreach($x in $installedApps){
if($null -ne $x){
$y=$x.Split(' ')
$apps+=@{$y[0]=$y[1]}
}
}
return $apps
}
[bool]$installedSuccess=$chocoAppName -in $appsList.GetEnumerator().Name
if($installedSuccess){
write-host "$chocoAppName has been installed on $env:computername successfully" -ForegroundColor Green
return $true
}else{
write-host "$chocoAppName has NOT been installed on $env:computername successfully" -ForegroundColor Yellow
return $false
}
}
} -Args $chocoAppName,$minVersion
$results+=@{$computer=$result}
remove-pssession $session
}else{
write-warning "unable to connect to $computer"
}
}
foreach ($item in $results.GetEnumerator()){
write-output "$($item.Name): $($item.value)"
}
return $results
}
installChocoApp $computers $chocoAppName $minVersion
# removeChocoApps.ps1
[string[]]$computers=@(
'SERVER01',
'SERVER02',
'SERVER03'
)
[string]$chocoAppName='pgadmin3'
function removeChocoApp{
param(
[string[]]$computers=$env:computername,
[string]$chocoAppName='Firefox'
)
$results=[hashtable]@{}
foreach ($computer in $computers){
$session=new-pssession $computer
if($session.State -eq 'Opened'){
$result=invoke-command -computer $computer -scriptblock{
param($chocoAppName)
# Install Chocolatey
if (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Set-ExecutionPolicy Bypass -Scope Process -Force;
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))}
$chocoList=choco list -l
$installedApps=$chocoList|%{
$appLine=([regex]'^([\w-_]+)\s([\d\.]+)$').matches($_).Value;
if($appLine){$appLine}
}
$appsList=.{
$apps=[hashtable]@{}
foreach($x in $installedApps){
if($null -ne $x){
$y=$x.Split(' ')
$apps+=@{$y[0]=$y[1]}
}
}
return $apps
}
[bool]$alreadyInstalled=$chocoAppName -in $appsList.GetEnumerator().Name
#[version]$installedVersion=if($alreadyInstalled){$appsList.$chocoAppName}else{$null}
if(!$alreadyInstalled){
write-host "$appName $installedVersion has already been uninstalled on $env:computername" -ForegroundColor Green
return $true
}else{
write-host "Installing $chocoAppName on $env:computername..."
$null=choco uninstall $chocoAppName -y;
$chocoList=choco list -l
$installedApps=$chocoList|%{
$appLine=([regex]'^([\w-_]+)\s([\d\.]+)$').matches($_).Value;
if($appLine){$appLine}
}
$appsList=.{
$apps=[hashtable]@{}
foreach($x in $installedApps){
if($null -ne $x){
$y=$x.Split(' ')
$apps+=@{$y[0]=$y[1]}
}
}
return $apps
}
[bool]$uninstallSuccess=$chocoAppName -notin $appsList.GetEnumerator().Name
if($uninstallSuccess){
write-host "$chocoAppName has been uninstalled on $env:computername successfully" -ForegroundColor Green
return $true
}else{
write-host "$chocoAppName has NOT been uninstalled on $env:computername successfully" -ForegroundColor Yellow
return $false
}
}
} -Args $chocoAppName
$results+=@{$computer=$result}
remove-pssession $session
}else{
write-warning "unable to connect to $computer"
}
}
foreach ($item in $results.GetEnumerator()){
write-output "$($item.Name): $($item.value)"
}
return $results
}
removeChocoApp $computers $chocoAppName
Simple snippet:
# Uninstall Choco Apps $chocoAppname='forticlient' $computernames=@( 'test-windows1', 'test-windows2' ) $computernames | %{ write-host "Uninstalling $chocoAppname on $_" invoke-command -computername $_ -scriptblock{param($appName) choco uninstall $appName -y} -Args $chocoAppname }
# removeAppwizProgram.ps1
[string[]]$computers=@(
'SERVER01',
'SERVER02',
'SERVER03'
)
$appName='pgAdmin 4'
$versionEqualOrLower='5.0'
function removeAppwizProgram{
param(
$computers=$env:computername,
$appName='Firefox',
$version='7.0'
)
$results=[hashtable]@{}
foreach($computer in $computers){
$session=new-pssession $computer
if($session.State -eq 'Opened'){
$result=invoke-command -session $session -scriptblock{
param($appName,$version)
$ErrorActionPreference='stop'
try{
$uninstallStringRegPaths='HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstallStrings=Get-ChildItem -Path $uninstallStringRegPaths
#$uninstallStrings|Get-ItemProperty|Where-Object {$_.DisplayName -match $appName -and [version]$version -ge [version]$_.DisplayVersion}
$uninstallString=($uninstallStrings|Get-ItemProperty|Where-Object {$_.DisplayName -match $appName -and [version]$version -ge [version]$_.DisplayVersion}).UninstallString
if ($uninstallString.count -eq 1){
$null=& cmd /c $uninstallString /verysilent /norestart
write-host "$appName version $version has been uninstalled successfully"
}elseif($uninstallString.count -eq 0){
write-host "$appName version $version has no matches on $env:computername" -ForegroundColor Green
}else{
write-warning "There are $($uninstallString.count) matched items. Please perform uninstallation manually."
return $false
}
return $true
}catch{
return $false
}
} -Args $appName,$version
$results+=@{$computer=$result}
remove-pssession $session
}else{
write-warning "unable to connect to $computer"
}
}
foreach ($item in $results.GetEnumerator()){
write-output "$($item.Name): $($item.value)"
}
return $results
}
removeAppwizProgram $computers $appName $versionEqualOrLower
Categories: