wingetautoupdate/Winget-AutoUpdate/functions/Test-ModsPath.ps1

193 lines
8.2 KiB
PowerShell
Raw Normal View History

2022-11-02 18:38:31 +00:00
#Function to check Mods External Path
function Test-ModsPath ($ModsPath, $WingetUpdatePath) {
# URL, UNC or Local Path
# Get local and external Mods paths
$LocalMods = -join ($WingetUpdatePath, "\", "mods")
2022-11-02 23:19:59 +00:00
$ExternalMods = "$ModsPath"
2022-11-02 23:19:59 +00:00
#Get File Names Locally
2022-12-08 16:53:37 +00:00
$InternalModsNames = Get-ChildItem -Path $LocalMods -Name -Recurse -Include *.ps1, *.txt
$InternalBinsNames = Get-ChildItem -Path $LocalMods"\bins" -Name -Recurse -Include *.exe
2022-11-02 18:38:31 +00:00
# If path is URL
2022-11-03 23:09:53 +00:00
if ($ExternalMods -like "http*") {
# enable TLS 1.2 and TLS 1.1 protocols
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Tls11
2022-11-06 05:37:34 +00:00
#Get Index of $ExternalMods (or index page with href listing of all the Mods)
2022-11-04 21:22:11 +00:00
try {
2022-11-14 23:04:54 +00:00
$WebResponse = Invoke-WebRequest -Uri $ExternalMods -UseBasicParsing
2022-11-04 21:22:11 +00:00
}
catch {
2023-01-08 17:57:47 +00:00
$Script:ReachNoPath = $True
2022-11-04 21:22:11 +00:00
return $False
}
#Check for bins, download if newer. Delete if not external
$ExternalBins = "$ModsPath/bins"
if ($WebResponse -match "bins/") {
$BinResponse = Invoke-WebRequest -Uri $ExternalBins -UseBasicParsing
# Collect the external list of href links
$BinLinks = $BinResponse.Links | Select-Object -ExpandProperty HREF
#If there's a directory path in the HREF:s, delete it (IIS)
$CleanBinLinks = $BinLinks -replace "/.*/",""
#Modify strings to HREF:s
$index = 0
foreach ($Bin in $CleanBinLinks) {
if ($Bin) {
$CleanBinLinks[$index] = '<a href="' + $Bin + '"> ' + $Bin + '</a>'
}
$index++
}
#Delete Local Bins that don't exist Externally
$index = 0
$CleanLinks = $BinLinks -replace "/.*/",""
foreach ($Bin in $InternalBinsNames) {
If ($CleanLinks -notcontains "$Bin") {
Remove-Item $LocalMods\bins\$Bin -Force -ErrorAction SilentlyContinue | Out-Null
}
$index++
}
$CleanBinLinks = $BinLinks -replace "/.*/",""
$Bin = ""
#Loop through all links
$wc = New-Object System.Net.WebClient
$CleanBinLinks | ForEach-Object {
#Check for .exe in listing/HREF:s in an index page pointing to .exe
if ($_ -like "*.exe") {
$dateExternalBin = ""
$dateLocalBin =""
$wc.OpenRead("$ExternalBins/$_").Close() | Out-Null
$dateExternalBin = ([DateTime]$wc.ResponseHeaders['Last-Modified']).ToString("yyyy-MM-dd HH:mm:ss")
if (Test-Path -Path $LocalMods"\bins\"$_) {
$dateLocalBin = (Get-Item "$LocalMods\bins\$_").LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
}
if ($dateExternalBin -gt $dateLocalBin) {
$SaveBin = Join-Path -Path "$LocalMods\bins" -ChildPath $_
Invoke-WebRequest -Uri "$ExternalBins/$_" -OutFile $SaveBin.Replace("%20"," ") -UseBasicParsing
}
}
}
}
2022-11-05 19:52:51 +00:00
# Collect the external list of href links
$ModLinks = $WebResponse.Links | Select-Object -ExpandProperty HREF
#If there's a directory path in the HREF:s, delete it (IIS)
$CleanLinks = $ModLinks -replace "/.*/",""
#Modify strings to HREF:s
$index = 0
foreach ($Mod in $CleanLinks) {
if ($Mod) {
$CleanLinks[$index] = '<a href="' + $Mod + '"> ' + $Mod + '</a>'
}
$index++
}
2022-11-06 05:37:34 +00:00
#Delete Local Mods that don't exist Externally
$DeletedMods = 0
$index = 0
$CleanLinks = $ModLinks -replace "/.*/",""
2022-11-04 21:40:03 +00:00
foreach ($Mod in $InternalModsNames) {
If ($CleanLinks -notcontains "$Mod") {
2022-11-05 11:52:58 +00:00
Remove-Item $LocalMods\$Mod -Force -ErrorAction SilentlyContinue | Out-Null
$DeletedMods++
2022-11-04 21:40:03 +00:00
}
$index++
2022-11-03 23:09:53 +00:00
}
$CleanLinks = $ModLinks -replace "/.*/",""
2022-11-03 23:09:53 +00:00
#Loop through all links
$wc = New-Object System.Net.WebClient
$CleanLinks | ForEach-Object {
2022-12-08 16:53:37 +00:00
#Check for .ps1/.txt in listing/HREF:s in an index page pointing to .ps1/.txt
if (($_ -like "*.ps1") -or ($_ -like "*.txt")) {
2022-11-02 18:38:31 +00:00
try {
2022-11-08 00:09:52 +00:00
$dateExternalMod = ""
$dateLocalMod =""
2022-11-03 23:09:53 +00:00
$wc.OpenRead("$ExternalMods/$_").Close() | Out-Null
$dateExternalMod = ([DateTime]$wc.ResponseHeaders['Last-Modified']).ToString("yyyy-MM-dd HH:mm:ss")
if (Test-Path -Path $LocalMods"\"$_) {
$dateLocalMod = (Get-Item "$LocalMods\$_").LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
}
if ($dateExternalMod -gt $dateLocalMod) {
try {
$SaveMod = Join-Path -Path "$LocalMods\" -ChildPath $_
$Mod = '{0}/{1}' -f $ModsPath.TrimEnd('/'), $_
2022-11-14 23:04:54 +00:00
Invoke-WebRequest -Uri "$Mod" -OutFile $SaveMod -UseBasicParsing
2022-11-03 23:09:53 +00:00
$ModsUpdated++
}
catch {
2023-01-08 17:57:47 +00:00
$Script:ReachNoPath = $True
2022-11-03 23:09:53 +00:00
}
}
2022-11-02 18:38:31 +00:00
}
catch {
2023-01-08 17:57:47 +00:00
$Script:ReachNoPath = $True
2022-11-02 18:38:31 +00:00
}
}
}
2022-11-05 00:12:13 +00:00
return $ModsUpdated, $DeletedMods
2022-11-02 18:38:31 +00:00
}
2023-01-05 02:31:45 +00:00
# If path is UNC or local
else {
$ExternalBins = "$ModsPath\bins"
if (Test-Path -Path $ExternalBins"\*.exe") {
$ExternalBinsNames = Get-ChildItem -Path $ExternalBins -Name -Recurse -Include *.exe
#Delete Local Bins that don't exist Externally
foreach ($Bin in $InternalBinsNames){
If ($Bin -notin $ExternalBinsNames ){
Remove-Item $LocalMods\bins\$Bin -Force -ErrorAction SilentlyContinue | Out-Null
}
}
#Copy newer external bins
foreach ($Bin in $ExternalBinsNames){
$dateExternalBin = ""
$dateLocalBin =""
if (Test-Path -Path $LocalMods"\bins\"$Bin) {
$dateLocalBin = (Get-Item "$LocalMods\bins\$Bin").LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
}
$dateExternalBin = (Get-Item "$ExternalBins\$Bin").LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
if ($dateExternalBin -gt $dateLocalBin) {
Copy-Item $ExternalBins\$Bin -Destination $LocalMods\bins\$Bin -Force -ErrorAction SilentlyContinue | Out-Null
}
}
}
2023-01-05 02:31:45 +00:00
if ((Test-Path -Path $ExternalMods"\*.ps1") -or (Test-Path -Path $ExternalMods"\*.txt")) {
#Get File Names Externally
$ExternalModsNames = Get-ChildItem -Path $ExternalMods -Name -Recurse -Include *.ps1, *.txt
2023-01-08 17:57:47 +00:00
2023-01-05 02:31:45 +00:00
#Delete Local Mods that don't exist Externally
2023-01-08 17:57:47 +00:00
$DeletedMods = 0
2023-01-05 02:31:45 +00:00
foreach ($Mod in $InternalModsNames){
2023-01-08 17:57:47 +00:00
If ($Mod -notin $ExternalModsNames ){
2023-01-05 02:31:45 +00:00
Remove-Item $LocalMods\$Mod -Force -ErrorAction SilentlyContinue | Out-Null
$DeletedMods++
}
}
2023-01-08 17:57:47 +00:00
#Copy newer external mods
foreach ($Mod in $ExternalModsNames){
$dateExternalMod = ""
$dateLocalMod =""
if (Test-Path -Path $LocalMods"\"$Mod) {
$dateLocalMod = (Get-Item "$LocalMods\$Mod").LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
}
$dateExternalMod = (Get-Item "$ExternalMods\$Mod").LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
if ($dateExternalMod -gt $dateLocalMod) {
Copy-Item $ExternalMods\$Mod -Destination $LocalMods\$Mod -Force -ErrorAction SilentlyContinue | Out-Null
$ModsUpdated++
2023-01-05 02:31:45 +00:00
}
}
}
2023-01-08 17:57:47 +00:00
else {
$Script:ReachNoPath = $True
}
2023-01-05 02:31:45 +00:00
return $ModsUpdated, $DeletedMods
}
2022-11-02 18:38:31 +00:00
}