Files
Powershell-Scripts/Utility Scripts/Windows Upgrade Prep.ps1
T

135 lines
4.9 KiB
PowerShell

<#
Author: Gabe Kerntke
Company: UFS LLC
Date: 02-13-2025
.Synopsis
.Modified
2025-02-13 (GabeK) - Original script created
2025-06-24 (GabeK) - Script updated to fix bugs and use functions script
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
$App_Name = "WindowsPCHealthCheckSetup",
[Parameter(Mandatory = $false)]
$File_Name_Extension = "msi",
[Parameter(Mandatory = $false)]
$Install_Parameters = "/qn",
[Parameter(Mandatory = $false)]
$EPCache_Folder = "3rd Party Patches\$App_Name",
[Parameter(Mandatory = $false)]
$DownloadURL = "https://aka.ms/GetPCHealthCheckApp",
[Parameter(Mandatory = $false)]
$File_Name = "$App_Name.$File_Name_Extension"
)
#Region Functions
Function Start-DiskCleanup {
$Size = Get-PSDrive C | Select-Object -ExpandProperty Free
Function Remove-Windows_Updates_Cache {
#Stops services related to Windows Updates
Stop-Service wuauserv
Stop-Service BITS
Start-Sleep 30
#Cleans out the Windows Updates patch cache folder
Get-ChildItem "C:\Windows\SoftwareDistribution\Download\" | Remove-Item -Force -Recurse
Start-Sleep 30
#Starts the services
Start-Service wuauserv
Start-Service BITS
Start-Sleep 15
}
#Check free space after running script
Function Convert-Size {
switch ($Size) {
{ $_ -gt 1TB } { ($Size / 1TB).ToString("n2") + " TB"; break }
{ $_ -gt 1GB } { ($Size / 1GB).ToString("n2") + " GB"; break }
{ $_ -gt 1MB } { ($Size / 1MB).ToString("n2") + " MB"; break }
{ $_ -gt 1KB } { ($Size / 1KB).ToString("n2") + " KB"; break }
default { "$Size B" }
}
}
# Script Start
If ((Test-Path "C:\Temp\Script Cache") -eq $False) { New-Item -ItemType directory -Path "C:\Temp\Script Cache" }
Invoke-WebRequest -UseBasicParsing "https://scripts.gabesville.com/Gabesville/Powershell-Scripts/raw/branch/main/Functions%20Scripts/Functions.ps1" -outfile "C:\Temp\Script Cache\Functions.ps1"
. "C:\Temp\Script Cache\Functions.ps1"
#Calculate starting free space
[INT64]$StartFreeSpace = (Get-WmiObject -Class Win32_logicaldisk | Where-Object { $_.DeviceID -eq 'C:' }).Freespace
#Check to see if program is installed
$Applist = Get-Software | Sort-Object -Descending
If ($Applist.DisplayName -like "*$App_Name*") {
Remove-Probe_Cache
}
Remove-Agent_Cache
Start-Disk_Cleanup
Remove-Windows_Updates_Cache
#Calculate space freed up and report out
[INT64]$EndFreeSpace = (Get-WmiObject -Class Win32_logicaldisk | Where { $_.DeviceID -eq 'C:' }).Freespace
$SpaceFreed = "{0:N2}" -f (($EndFreeSpace - $StartFreeSpace) / 1GB)
Write-Output "Space freed up: $($SpaceFreed)GB"
$FreeAfter = Convert-Size
Write-Host "$FreeAfter space free on C drive"
}
Function Start-PCHealthCheck {
Function Set-RegKeys {
New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS
Start-Sleep 5
$GetPaths = Get-ChildItem "HKU:\" -ErrorAction SilentlyContinue | Select-Object -Skip 1 -ExpandProperty Name
$RegPaths = $GetPaths.replace('HKEY_USERS\', '')
$Location = foreach ($Path in $RegPaths) {
Get-ChildItem "HKU:\$Path\Software\Microsoft" -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.Name -like "*PCHC*" } | Select-Object -ExpandProperty PSPath
Get-ChildItem "HKU:\$Path\Software\Microsoft" -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.Name -like "*PCHealthCheck*" } | Select-Object -ExpandProperty PSPath
}
New-ItemProperty -Path $Location.substring(26) -Name "UpgradeEligibility" -Value 1 -Type DWORD -Force -Verbose
}
# Start Script #
#Use TLS 1.2 for communication
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
If ((Test-Path "C:\Temp\Script Cache") -eq $False) { New-Item -ItemType directory -Path "C:\Temp\Script Cache" }
Invoke-WebRequest -UseBasicParsing "https://scripts.gabesville.com/Gabesville/Powershell-Scripts/raw/branch/main/Functions%20Scripts/Functions.ps1" -outfile "C:\Temp\Script Cache\Functions.ps1"
. "C:\Temp\Script Cache\Functions.ps1"
#Check if App is installed
$Applist = Get-Software | Sort-Object -Descending
Start-Sleep 5
If ($Applist.DisplayName -notlike "*$App_Name*") {
Test-Probe_Online
Install-App
Set-RegKeys
}
Else {
Write-Host "$Name is installed, exiting script."
Set-RegKeys
}
}
#Region Script Start
Start-PCHealthCheck
Start-DiskCleanup
Start-Sleep 900
$BLinfo = Get-Bitlockervolume
If ($blinfo.ProtectionStatus -eq 'On' -and $blinfo.EncryptionPercentage -eq '100') {
Manage-bde -Protectors -Disable C: -RebootCount 5
}
Restart-Computer -Force
#Region Script End