Update Functions Scripts/Functions.ps1

This commit is contained in:
2025-08-05 02:45:48 +00:00
parent 8dc5e6b3a2
commit 758f4bf4eb
+383 -383
View File
@@ -1,384 +1,384 @@
<# <#
Author: Gabe Kerntke Author: Gabe Kerntke
Date: 04/24/25 Date: 04/24/25
.Synopsis .Synopsis
Script to hold all functions to make updating other scripts easier Script to hold all functions to make updating other scripts easier
.Modified .Modified
- GabeK (4/24/25): Script created - GabeK (4/24/25): Script created
.PARAMETER EPCache_Site_Directory .PARAMETER EPCache_Site_Directory
Combines the EPCacheSite with the EPCache_Folder variables Combines the EPCacheSite with the EPCache_Folder variables
#> #>
[CmdletBinding()] [CmdletBinding()]
Param( Param(
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
$PSCPURL = "https://files.gabesville.com/filebrowser/api/raw/Tools%20%26%20Programs/Network/pscp.exe", $PSCPURL = "https://files.gabesville.com/filebrowser/api/raw/Tools%20%26%20Programs/Network/pscp.exe",
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
$PSCPLOC = "$Env:windir\system32\pscp.exe", $PSCPLOC = "$Env:windir\system32\pscp.exe",
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
$EPCache_Site_Directory = $ENV:EPCacheSite + '\' + $EPCache_Folder, $EPCache_Site_Directory = $ENV:EPCacheSite + '\' + $EPCache_Folder,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
$EPCache_File_Location = $ENV:EPCacheSite + '\' + $EPCache_Folder + '\' + $File_Name, $EPCache_File_Location = $ENV:EPCacheSite + '\' + $EPCache_Folder + '\' + $File_Name,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
$EPCacheLocal = "$Env:windir\NCentral\Cache", $EPCacheLocal = "$Env:windir\NCentral\Cache",
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
$Global:EPCacheLocalLocation = $ENV:EPCacheLocal + '\' + $File_Name $Global:EPCacheLocalLocation = $ENV:EPCacheLocal + '\' + $File_Name
) )
# Variables # # Variables #
#things that go in other scripts #things that go in other scripts
#Variables for downloading from MoveIT #Variables for downloading from MoveIT
#$App_Name = "" #$App_Name = ""
#$Install_Parameters = "" #$Install_Parameters = ""
#$File_Hash = "" #$File_Hash = ""
#$File_Name = "CitrixWorkspaceFullInstaller2405.exe" #$File_Name = "CitrixWorkspaceFullInstaller2405.exe"
#$EPCache_Folder = "either software or 3rd party patches" #$EPCache_Folder = "either software or 3rd party patches"
#$MoveIT_File = "" #$MoveIT_File = ""
#Variables for downloading from Online #Variables for downloading from Online
#$App_Name = "" #$App_Name = ""
#$File_Name_Prefix = "" #$File_Name_Prefix = ""
#$File_Name_Extension = "" #$File_Name_Extension = ""
#$Install_Parameters = "" #$Install_Parameters = ""
#$EPCache_Folder = "either software or 3rd party patches" #$EPCache_Folder = "either software or 3rd party patches"
#$Version_URL = "" #$Version_URL = ""
#$Program = (Invoke-WebRequest -UseBasicParsing "$Version_URL").links.href -like "*exe*" #$Program = (Invoke-WebRequest -UseBasicParsing "$Version_URL").links.href -like "*exe*"
#$Online_Version = $Program.split("-") | Select-Object -First 3 | Select-Object -Last 1 #$Online_Version = $Program.split("-") | Select-Object -First 3 | Select-Object -Last 1
#$DownloadURL = "$Version_URL" + "$Program" #$DownloadURL = "$Version_URL" + "$Program"
#$File_Name = "$File_Name_Prefix-$Online_Version.$File_Name_Extension" #$File_Name = "$File_Name_Prefix-$Online_Version.$File_Name_Extension"
#Remove folder variables #Remove folder variables
#$User_Folder = "C:\Users\$($User)\AppData\Local\Mozilla" #$User_Folder = "C:\Users\$($User)\AppData\Local\Mozilla"
#$User_Folder2 = "C:\Users\$($User)\AppData\Roaming\Mozilla" #$User_Folder2 = "C:\Users\$($User)\AppData\Roaming\Mozilla"
#$Program_Folder = "C:\Program Files\Mozilla Firefox" #$Program_Folder = "C:\Program Files\Mozilla Firefox"
#$Program_Folder2 = "C:\Program Files (x86)\Mozilla Firefox" #$Program_Folder2 = "C:\Program Files (x86)\Mozilla Firefox"
#If ((Test-Path "$Env:windir\NCentral\Cache") -eq $False) { New-Item -ItemType directory -Path "$Env:windir\NCentral\Cache" } #If ((Test-Path "$Env:windir\NCentral\Cache") -eq $False) { New-Item -ItemType directory -Path "$Env:windir\NCentral\Cache" }
#Invoke-WebRequest -UseBasicParsing "https://endpoint.ufsdata.com/download/repository/632809041/Functions.ps1" -outfile "$Env:windir\NCentral\Cache\Functions.ps1" #Invoke-WebRequest -UseBasicParsing "https://scripts.gabesville.com/Gabesville/Powershell-Scripts/raw/branch/main/Functions%20Scripts/Functions.ps1" -outfile "$Env:windir\NCentral\Cache\Functions.ps1"
#. "C:\Windows\NCentral\Cache\Functions.ps1" #. "C:\Windows\NCentral\Cache\Functions.ps1"
#Region Functions #Region Functions
#Function checks for 32 or 64 bit processor and then pulls list of applications accordingly #Function checks for 32 or 64 bit processor and then pulls list of applications accordingly
Function Get-Software ($CPU = $ENV:PROCESSOR_ARCHITECTURE) { Function Get-Software ($CPU = $ENV:PROCESSOR_ARCHITECTURE) {
If ($CPU -eq 'AMD64') { If ($CPU -eq 'AMD64') {
$64bit = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* $64bit = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*
$32bit = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* $32bit = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*
$32bit + $64bit $32bit + $64bit
} }
Else { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* } Else { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* }
} }
#Function to test if communication with the probe is possible, and to donwload the installer #Function to test if communication with the probe is possible, and to donwload the installer
Function Test-Probe_MoveIT { Function Test-Probe_MoveIT {
#Download PSCP.exe if it does not exist #Download PSCP.exe if it does not exist
If ((Test-Path $PSCPLOC) -eq $False) { (new-object System.Net.WebClient).DownloadFile($PSCPURL, $PSCPLOC) | Out-Null } If ((Test-Path $PSCPLOC) -eq $False) { (new-object System.Net.WebClient).DownloadFile($PSCPURL, $PSCPLOC) | Out-Null }
If ($null -eq $ENV:EPCacheSite) { If ($null -eq $ENV:EPCacheSite) {
Write-Host "Can't communicate with the $ENV:EPCacheSite." Write-Host "Can't communicate with the $ENV:EPCacheSite."
Get-Download2Local_MoveIT Get-Download2Local_MoveIT
} }
Elseif ($null -ne $ENV:EPCacheSite) { Elseif ($null -ne $ENV:EPCacheSite) {
If (Test-Path $ENV:EPCacheSite) { If (Test-Path $ENV:EPCacheSite) {
If ($null -ne $EPCache_File_Location) { If ($null -ne $EPCache_File_Location) {
If (Test-Path $EPCache_File_Location) { If (Test-Path $EPCache_File_Location) {
Copy-Download_from_Probe Copy-Download_from_Probe
} }
Else { Else {
Get-Download2Probe_MoveIT Get-Download2Probe_MoveIT
} }
} }
} }
Else { Else {
Write-Host "Can't communicate with the $EPCache_File_Location." Write-Host "Can't communicate with the $EPCache_File_Location."
Get-Download2Local_MoveIT Get-Download2Local_MoveIT
} }
} }
} }
#Function to test if communication with the probe is possible, and to donwload the installer #Function to test if communication with the probe is possible, and to donwload the installer
Function Test-Probe_Online { Function Test-Probe_Online {
If ($null -eq $ENV:EPCacheSite) { If ($null -eq $ENV:EPCacheSite) {
Write-Host "Can't communicate with the $ENV:EPCacheSite." Write-Host "Can't communicate with the $ENV:EPCacheSite."
Get-Download2Local_Online Get-Download2Local_Online
} }
Elseif ($null -ne $ENV:EPCacheSite) { Elseif ($null -ne $ENV:EPCacheSite) {
If (Test-Path $ENV:EPCacheSite) { If (Test-Path $ENV:EPCacheSite) {
If ($null -ne $EPCache_File_Location) { If ($null -ne $EPCache_File_Location) {
If (Test-Path $EPCache_File_Location) { If (Test-Path $EPCache_File_Location) {
Copy-Download_from_Probe Copy-Download_from_Probe
} }
Else { Else {
Get-Download2Probe_Online Get-Download2Probe_Online
} }
} }
} }
Else { Else {
Write-Host "Can't communicate with the $EPCache_File_Location." Write-Host "Can't communicate with the $EPCache_File_Location."
Get-Download2Local_Online Get-Download2Local_Online
} }
} }
} }
#Function to download installer locally from MoveIT #Function to download installer locally from MoveIT
Function Get-Download2Local_MoveIT { Function Get-Download2Local_MoveIT {
#Create folder if they are missing #Create folder if they are missing
If ((Test-Path $EPCacheLocal) -eq $False) { New-Item -ItemType directory -Path $EPCacheLocal } If ((Test-Path $EPCacheLocal) -eq $False) { New-Item -ItemType directory -Path $EPCacheLocal }
pscp.exe -hostkey $MoveITkey -pw $MoveITpass $MoveITuser`:$MoveIT_File $EPCacheLocalLocation | Out-Null pscp.exe -hostkey $MoveITkey -pw $MoveITpass $MoveITuser`:$MoveIT_File $EPCacheLocalLocation | Out-Null
Start-Sleep 5 Start-Sleep 5
Confirm-Hash_Local Confirm-Hash_Local
} }
#Function to download installer locally from online #Function to download installer locally from online
Function Get-Download2Local_Online { Function Get-Download2Local_Online {
#Create folder if they are missing #Create folder if they are missing
If ((Test-Path $EPCacheLocal) -eq $False) { New-Item -ItemType directory -Path $EPCacheLocal } If ((Test-Path $EPCacheLocal) -eq $False) { New-Item -ItemType directory -Path $EPCacheLocal }
Invoke-WebRequest -UseBasicParsing $DownloadURL -outfile $EPCacheLocalLocation Invoke-WebRequest -UseBasicParsing $DownloadURL -outfile $EPCacheLocalLocation
} }
#Function to verify the hash of the file locally downloaded from MoveIT #Function to verify the hash of the file locally downloaded from MoveIT
Function Confirm-Hash_Local { Function Confirm-Hash_Local {
If ((Get-FileHash -Path $EPCacheLocalLocation -algorith SHA256 -ErrorAction SilentlyContinue).hash -eq $File_Hash) { If ((Get-FileHash -Path $EPCacheLocalLocation -algorith SHA256 -ErrorAction SilentlyContinue).hash -eq $File_Hash) {
Write-Host "Local hash verified successfully" Write-Host "Local hash verified successfully"
} }
Else { Else {
Write-Host "Local file hash verification failure" Write-Host "Local file hash verification failure"
Remove-Item $EPCacheLocalLocation -Force -ErrorAction SilentlyContinue Remove-Item $EPCacheLocalLocation -Force -ErrorAction SilentlyContinue
Exit Exit
} }
} }
#If file is not locked, copy from probe to local cache #If file is not locked, copy from probe to local cache
Function Copy-Download_from_Probe { Function Copy-Download_from_Probe {
#Create folder if they are missing #Create folder if they are missing
If ((Test-Path $EPCacheLocal) -eq $False) { New-Item -ItemType directory -Path $EPCacheLocal } If ((Test-Path $EPCacheLocal) -eq $False) { New-Item -ItemType directory -Path $EPCacheLocal }
Confirm-File_Lock Confirm-File_Lock
$FileLock = Test-File_Lock $FileLock = Test-File_Lock
If ($FileLock -eq $False) { If ($FileLock -eq $False) {
If ((Test-Path $EPCacheLocalLocation) -eq $false) { If ((Test-Path $EPCacheLocalLocation) -eq $false) {
Copy-Item $EPCache_File_Location -Destination $EPCacheLocalLocation -Force Copy-Item $EPCache_File_Location -Destination $EPCacheLocalLocation -Force
If ($null -ne $File_Hash) { Confirm-Hash_Local } If ($null -ne $File_Hash) { Confirm-Hash_Local }
Limit-Probe Limit-Probe
} }
} }
} }
#Function loops testing if the file is locked #Function loops testing if the file is locked
Function Confirm-File_Lock { Function Confirm-File_Lock {
$FileLock = Test-File_Lock $FileLock = Test-File_Lock
While ($FileLock -eq $True) { While ($FileLock -eq $True) {
Write-Host "File is locked" Write-Host "File is locked"
Start-Sleep (Get-Random -Minimum 15 -Maximum 45) Start-Sleep (Get-Random -Minimum 15 -Maximum 45)
$FileLock = Test-File_Lock $FileLock = Test-File_Lock
} }
} }
#Function tests if the file is in use #Function tests if the file is in use
Function Test-File_Lock { Function Test-File_Lock {
try { try {
$x = [System.IO.File]::Open($EPCache_File_Location, 'Open', 'Read') # Open file $x = [System.IO.File]::Open($EPCache_File_Location, 'Open', 'Read') # Open file
$x.Close() # Close file $x.Close() # Close file
$x.Dispose() # Disposing object $x.Dispose() # Disposing object
return $false # File not in use return $false # File not in use
} }
catch [System.Management.Automation.MethodException] { catch [System.Management.Automation.MethodException] {
return $true # File in use return $true # File in use
} }
} }
#Function to cleanup old downloads on probe #Function to cleanup old downloads on probe
Function Limit-Probe { Function Limit-Probe {
# Set how old the file can be before getting deleted # Set how old the file can be before getting deleted
$limit = (Get-Date).AddDays(-20) $limit = (Get-Date).AddDays(-20)
# Delete files older than the $limit # Delete files older than the $limit
Get-ChildItem -Path $EPCache_Site_Directory -Recurse -Force -ErrorAction SilentlyContinue | Get-ChildItem -Path $EPCache_Site_Directory -Recurse -Force -ErrorAction SilentlyContinue |
Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit } | Remove-Item -Force Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit } | Remove-Item -Force
} }
#Function to donwload installer to probe from MoveIT #Function to donwload installer to probe from MoveIT
Function Get-Download2Probe_MoveIT { Function Get-Download2Probe_MoveIT {
#Creates folder if missing #Creates folder if missing
If ((Test-Path $EPCache_Site_Directory) -eq $False) { New-Item -ItemType directory -Path $EPCache_Site_Directory } If ((Test-Path $EPCache_Site_Directory) -eq $False) { New-Item -ItemType directory -Path $EPCache_Site_Directory }
#Download file to the probe Endpoint Cache site #Download file to the probe Endpoint Cache site
Start-Sleep (Get-Random -Minimum 5 -Maximum 45) Start-Sleep (Get-Random -Minimum 5 -Maximum 45)
If ((Test-Path $EPCache_File_Location -ErrorAction SilentlyContinue) -eq $False) { If ((Test-Path $EPCache_File_Location -ErrorAction SilentlyContinue) -eq $False) {
pscp.exe -hostkey $MoveITkey -pw $MoveITpass $MoveITuser`:$MoveIT_File $EPCache_File_Location | Out-Null pscp.exe -hostkey $MoveITkey -pw $MoveITpass $MoveITuser`:$MoveIT_File $EPCache_File_Location | Out-Null
Confirm-Download $EPCache_File_Location Confirm-Download $EPCache_File_Location
Confirm-Hash_Probe Confirm-Hash_Probe
} }
Elseif ((Test-Path $EPCache_File_Location -ErrorAction SilentlyContinue) -eq $True) { Elseif ((Test-Path $EPCache_File_Location -ErrorAction SilentlyContinue) -eq $True) {
Confirm-File_Lock Confirm-File_Lock
$FileLock = Test-File_Lock $FileLock = Test-File_Lock
If ($FileLock -eq $False) { Confirm-Hash_Probe } If ($FileLock -eq $False) { Confirm-Hash_Probe }
} }
} }
#Function to download installer to probe from online #Function to download installer to probe from online
Function Get-Download2Probe_Online { Function Get-Download2Probe_Online {
#Creates folder if missing #Creates folder if missing
If ((Test-Path $EPCache_Site_Directory) -eq $False) { New-Item -ItemType directory -Path $EPCache_Site_Directory } If ((Test-Path $EPCache_Site_Directory) -eq $False) { New-Item -ItemType directory -Path $EPCache_Site_Directory }
#Download file to the probe Endpoint Cache site #Download file to the probe Endpoint Cache site
Start-Sleep (Get-Random -Minimum 5 -Maximum 45) Start-Sleep (Get-Random -Minimum 5 -Maximum 45)
If ((Test-Path $EPCache_File_Location -ErrorAction SilentlyContinue) -eq $False) { If ((Test-Path $EPCache_File_Location -ErrorAction SilentlyContinue) -eq $False) {
Invoke-WebRequest -UseBasicParsing $DownloadURL -outfile $EPCache_File_Location Invoke-WebRequest -UseBasicParsing $DownloadURL -outfile $EPCache_File_Location
Confirm-Download $EPCache_File_Location Confirm-Download $EPCache_File_Location
Copy-Download_from_Probe Copy-Download_from_Probe
} }
Elseif ((Test-Path $EPCache_File_Location -ErrorAction SilentlyContinue) -eq $True) { Elseif ((Test-Path $EPCache_File_Location -ErrorAction SilentlyContinue) -eq $True) {
Confirm-File_Lock Confirm-File_Lock
$FileLock = Test-File_Lock $FileLock = Test-File_Lock
If ($FileLock -eq $False) { Copy-Download_from_Probe } If ($FileLock -eq $False) { Copy-Download_from_Probe }
} }
} }
#Function checks on the progress of a download and stops when the file no longer grows in size #Function checks on the progress of a download and stops when the file no longer grows in size
Function Confirm-Download { Function Confirm-Download {
$a = $args[0] $a = $args[0]
$b = 1 $b = 1
$c = (Get-Item $a).length $c = (Get-Item $a).length
while ($c -ne $b) { while ($c -ne $b) {
$b = $c $b = $c
Start-Sleep -Seconds 30 Start-Sleep -Seconds 30
$c = (Get-Item $a).length $c = (Get-Item $a).length
} }
} }
#Function to verify the hash of the file downloaded from MoveIT #Function to verify the hash of the file downloaded from MoveIT
Function Confirm-Hash_Probe { Function Confirm-Hash_Probe {
If ((Get-FileHash -Path $EPCache_File_Location -algorith SHA256 -ErrorAction SilentlyContinue).hash -eq $File_Hash) { If ((Get-FileHash -Path $EPCache_File_Location -algorith SHA256 -ErrorAction SilentlyContinue).hash -eq $File_Hash) {
Copy-Download_from_Probe Copy-Download_from_Probe
} }
Else { Else {
Write-Host "Probe file hash verification failure" Write-Host "Probe file hash verification failure"
Remove-Item $EPCache_File_Location -Force -ErrorAction SilentlyContinue Remove-Item $EPCache_File_Location -Force -ErrorAction SilentlyContinue
Exit Exit
} }
} }
#Function to close program's running processes #Function to close program's running processes
Function Close-App { Function Close-App {
#Stops all programs processes #Stops all programs processes
#Get-Process $App_Name -ErrorAction SilentlyContinue | Stop-Process -ErrorAction SilentlyContinue #Get-Process $App_Name -ErrorAction SilentlyContinue | Stop-Process -ErrorAction SilentlyContinue
Get-Process $App_Name -ErrorAction SilentlyContinue | ForEach-Object { Stop-Process $_ -Force -ErrorAction SilentlyContinue } Get-Process $App_Name -ErrorAction SilentlyContinue | ForEach-Object { Stop-Process $_ -Force -ErrorAction SilentlyContinue }
Start-Sleep 5 Start-Sleep 5
} }
#Function to install the downloaded installer #Function to install the downloaded installer
Function Install-App { Function Install-App {
$Install = Start-Process $EPCacheLocalLocation -ArgumentList $Install_Parameters -Wait -PassThru $Install = Start-Process $EPCacheLocalLocation -ArgumentList $Install_Parameters -Wait -PassThru
$ExitCode = ($Install).ExitCode $ExitCode = ($Install).ExitCode
Read-ExitCode Read-ExitCode
} }
#Function checks the exitcode #Function checks the exitcode
Function Read-ExitCode { Function Read-ExitCode {
If ($ExitCode -eq 0) { If ($ExitCode -eq 0) {
Write-Host "Script was Successful" Write-Host "Script was Successful"
Start-Sleep 10 Start-Sleep 10
(Get-Software | Where-Object { $_.DisplayName -like "*$App_Name*" } | Select-Object DisplayName, DisplayVersion | Format-List | Out-string).Trim() (Get-Software | Where-Object { $_.DisplayName -like "*$App_Name*" } | Select-Object DisplayName, DisplayVersion | Format-List | Out-string).Trim()
Remove-Item $EPCacheLocalLocation -Force -ErrorAction SilentlyContinue Remove-Item $EPCacheLocalLocation -Force -ErrorAction SilentlyContinue
} }
ElseIf ($ExitCode -eq 3010) { ElseIf ($ExitCode -eq 3010) {
Write-Host "Script was Successful, but a reboot is required." Write-Host "Script was Successful, but a reboot is required."
Start-Sleep 10 Start-Sleep 10
(Get-Software | Where-Object { $_.DisplayName -like "*$App_Name*" } | Select-Object DisplayName, DisplayVersion | Format-List | Out-string).Trim() (Get-Software | Where-Object { $_.DisplayName -like "*$App_Name*" } | Select-Object DisplayName, DisplayVersion | Format-List | Out-string).Trim()
Remove-Item $EPCacheLocalLocation -Force -ErrorAction SilentlyContinue Remove-Item $EPCacheLocalLocation -Force -ErrorAction SilentlyContinue
} }
ElseIf ($ExitCode -eq 1642) { ElseIf ($ExitCode -eq 1642) {
Write-Host "MUI version not detected, please reinstall Adobe to update." Write-Host "MUI version not detected, please reinstall Adobe to update."
Remove-Item $EPCacheLocalLocation -Force -ErrorAction SilentlyContinue Remove-Item $EPCacheLocalLocation -Force -ErrorAction SilentlyContinue
} }
Else { Else {
Write-Host `n Write-Host `n
Write-Host "Failed... Exit Code:"$ExitCode Write-Host "Failed... Exit Code:"$ExitCode
Remove-Item $EPCacheLocalLocation -Force -ErrorAction SilentlyContinue Remove-Item $EPCacheLocalLocation -Force -ErrorAction SilentlyContinue
} }
} }
#Use TLS 1.2 for communication #Use TLS 1.2 for communication
#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#Function to delete the user appdata folder for reinstall scripts #Function to delete the user appdata folder for reinstall scripts
Function Remove-User_Folder { Function Remove-User_Folder {
$Users = (Get-ChildItem C:\Users).Name $Users = (Get-ChildItem C:\Users).Name
foreach ($User in $Users) { foreach ($User in $Users) {
If (Test-Path $User_Folder) { Remove-Item $User_Folder -Force -Recurse -Verbose } If (Test-Path $User_Folder) { Remove-Item $User_Folder -Force -Recurse -Verbose }
If (Test-Path $User_Folder2) { Remove-Item $User_Folder2 -Force -Recurse -Verbose } If (Test-Path $User_Folder2) { Remove-Item $User_Folder2 -Force -Recurse -Verbose }
} }
} }
#Function to delete the programs folder for reinstall scripts #Function to delete the programs folder for reinstall scripts
Function Remove-Program_Folder { Function Remove-Program_Folder {
If (Test-Path $Program_Folder) { Remove-Item $Program_Folder -Force -Recurse -Verbose } If (Test-Path $Program_Folder) { Remove-Item $Program_Folder -Force -Recurse -Verbose }
If (Test-Path $Program_Folder2) { Remove-Item $Program_Folder2 -Force -Recurse -Verbose } If (Test-Path $Program_Folder2) { Remove-Item $Program_Folder2 -Force -Recurse -Verbose }
} }
#Function to delete the programs folder for reinstall scripts alt method #Function to delete the programs folder for reinstall scripts alt method
Function Remove-Program_Folder-ALT { Function Remove-Program_Folder-ALT {
$Items = Get-ChildItem $Program_Folder -Recurse | Select-Object -ExpandProperty FullName $Items = Get-ChildItem $Program_Folder -Recurse | Select-Object -ExpandProperty FullName
foreach ($Item in $Items) { Remove-Item $Item -Force -Recurse -Confirm:$false -ErrorAction SilentlyContinue } foreach ($Item in $Items) { Remove-Item $Item -Force -Recurse -Confirm:$false -ErrorAction SilentlyContinue }
$Items2 = Get-ChildItem $Program_Folder2 -Recurse | Select-Object -ExpandProperty FullName $Items2 = Get-ChildItem $Program_Folder2 -Recurse | Select-Object -ExpandProperty FullName
foreach ($Item2 in $Items2) { Remove-Item $Item2 -Force -Recurse -Confirm:$false -ErrorAction SilentlyContinue } foreach ($Item2 in $Items2) { Remove-Item $Item2 -Force -Recurse -Confirm:$false -ErrorAction SilentlyContinue }
(Get-Software | Where-Object { $_.DisplayName -like "*$App_Name*" }).PSPath | Remove-Item -Force -Verbose -Recurse -ErrorAction SilentlyContinue (Get-Software | Where-Object { $_.DisplayName -like "*$App_Name*" }).PSPath | Remove-Item -Force -Verbose -Recurse -ErrorAction SilentlyContinue
} }
#Function to uninstall via WMIobject (control panel) #Function to uninstall via WMIobject (control panel)
Function Uninstall-App_WMIobject { Function Uninstall-App_WMIobject {
$Uninstall = Get-WmiObject Win32_Product -filter "name like '$App_Name%'" | ForEach-Object { $_.Uninstall() } $Uninstall = Get-WmiObject Win32_Product -filter "name like '$App_Name%'" | ForEach-Object { $_.Uninstall() }
$Value = $Uninstall | Select-Object -ExpandProperty ReturnValue $Value = $Uninstall | Select-Object -ExpandProperty ReturnValue
If ($Value -eq "0") { Write-Host "$App_Name uninstalled successfully" } If ($Value -eq "0") { Write-Host "$App_Name uninstalled successfully" }
Else { Write-Host "Failed to uninstall $App_Name via WMIobject." } Else { Write-Host "Failed to uninstall $App_Name via WMIobject." }
} }
Function Uninstall-App_UninstallString { Function Uninstall-App_UninstallString {
$UninstallString = $Applist | Where-Object { $_.DisplayName -eq $App_Name } | Select-Object -ExpandProperty QuietUninstallString -ErrorAction SilentlyContinue $UninstallString = $Applist | Where-Object { $_.DisplayName -eq $App_Name } | Select-Object -ExpandProperty QuietUninstallString -ErrorAction SilentlyContinue
If ($null -eq $UninstallString) { If ($null -eq $UninstallString) {
$UninstallString = $Applist | Where-Object { $_.DisplayName -eq $App_Name } | Select-Object -ExpandProperty UninstallString $UninstallString = $Applist | Where-Object { $_.DisplayName -eq $App_Name } | Select-Object -ExpandProperty UninstallString
$UninstallString + $Uninstall_Parameters | cmd $UninstallString + $Uninstall_Parameters | cmd
Start-Sleep 10 Start-Sleep 10
} }
Else { Else {
$UninstallString | cmd $UninstallString | cmd
Start-Sleep 10 Start-Sleep 10
} }
If (Get-Software | Where-Object { $_.DisplayName -eq $App_Name }) { If (Get-Software | Where-Object { $_.DisplayName -eq $App_Name }) {
#Uninstall-App_PSPath #Uninstall-App_PSPath
Write-Host "Still installed" Write-Host "Still installed"
} }
Else { Else {
Write-Host "$App_Name uninstalled successfully" Write-Host "$App_Name uninstalled successfully"
Remove-Program_Folder Remove-Program_Folder
} }
} }
#Working on this function, has issue of not getting the variable and trying to use every single uninstall string from registry... #Working on this function, has issue of not getting the variable and trying to use every single uninstall string from registry...
Function Uninstall-App_PSPath { Function Uninstall-App_PSPath {
$UninstallString = $Applist | Where-Object { $_.DisplayName -eq $App_Name } | Select-Object -ExpandProperty PSPath $UninstallString = $Applist | Where-Object { $_.DisplayName -eq $App_Name } | Select-Object -ExpandProperty PSPath
$B = $UninstallString -replace "Microsoft.PowerShell.Core", "" $B = $UninstallString -replace "Microsoft.PowerShell.Core", ""
$C = $B.Substring(1) $C = $B.Substring(1)
Remove-Item -ErrorAction SilentlyContinue -Path $C -Force -Verbose Remove-Item -ErrorAction SilentlyContinue -Path $C -Force -Verbose
Start-Sleep 10 Start-Sleep 10
If (Get-Software | Where-Object { $_.DisplayName -eq $App_Name }) { If (Get-Software | Where-Object { $_.DisplayName -eq $App_Name }) {
Write-Host "$App_Name uninstall failed, exting script" Write-Host "$App_Name uninstall failed, exting script"
} }
Else { Else {
Write-Host "$App_Name uninstalled successfully" Write-Host "$App_Name uninstalled successfully"
Remove-Program_Folder Remove-Program_Folder
} }
} }
Function Start-Disk_Cleanup { Function Start-Disk_Cleanup {
#Runs Disk Cleanup with all boxes checked and without user input #Runs Disk Cleanup with all boxes checked and without user input
CLEANMGR C:\ /VERYLOWDISK CLEANMGR C:\ /VERYLOWDISK
CLEANMGR C:\ /AUTOCLEAN CLEANMGR C:\ /AUTOCLEAN
} }