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