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

112 lines
4.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"
#Get File Names Locally
$InternalModsNames = Get-ChildItem -Path $LocalMods -Name -Recurse -Include *.ps1
2022-11-02 18:38:31 +00:00
# If path is URL
2022-11-03 23:09:53 +00:00
if ($ExternalMods -like "http*") {
2022-11-02 18:38:31 +00:00
$wc = New-Object System.Net.WebClient
2022-11-03 23:09:53 +00:00
# enable TLS 1.2 and TLS 1.1 protocols
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Tls11
#Get Index of $ExternalMods (or index page with href listings of all the Mods)
2022-11-04 21:22:11 +00:00
try {
$WebResponse = Invoke-WebRequest -Uri $ExternalMods
}
catch {
return $False
}
2022-11-04 21:40:03 +00:00
# Get the list of links, skip the first one ("../") if listing is allowed
$ModLinks = $WebResponse.Links | Select-Object -ExpandProperty href -Skip 1
2022-11-03 23:09:53 +00:00
#Delete Local Mods that doesn't exist Externally
2022-11-04 21:40:03 +00:00
foreach ($Mod in $InternalModsNames) {
try {
If ($Mod -notin $ModLinks) {
Remove-Item $LocalMods\$Mod -Force | Out-Null
2022-11-03 23:09:53 +00:00
}
}
2022-11-04 21:40:03 +00:00
catch {
#Do nothing
}
2022-11-03 23:09:53 +00:00
}
2022-11-04 21:22:11 +00:00
2022-11-03 23:09:53 +00:00
#Loop through all links
$WebResponse.Links | Select-Object -ExpandProperty href -Skip 1 | ForEach-Object {
#Check for .ps1 in listing/HREF:s in an index page pointing to .ps1
if ($_ -like "*.ps1") {
2022-11-02 18:38:31 +00:00
try {
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('/'), $_
Invoke-WebRequest -Uri "$Mod" -OutFile $SaveMod
$ModsUpdated++
}
catch {
return $False
}
}
2022-11-02 18:38:31 +00:00
}
catch {
return $False
}
}
}
2022-11-03 23:09:53 +00:00
return $ModsUpdated
2022-11-02 18:38:31 +00:00
}
# If path is UNC or local
else {
2022-11-02 23:19:59 +00:00
if (Test-Path -Path $ExternalMods"\*.ps1") {
2022-11-03 00:19:04 +00:00
#Get File Names Externally
$ExternalModsNames = Get-ChildItem -Path $ExternalMods -Name -Recurse -Include *.ps1
2022-11-02 23:19:59 +00:00
#Delete Local Mods that doesn't exist Externally
foreach ($Mod in $InternalModsNames){
2022-11-02 18:38:31 +00:00
try {
2022-11-02 23:19:59 +00:00
If($Mod -notin $ExternalModsNames ){
Remove-Item $LocalMods\$Mod -Force | Out-Null
}
2022-11-02 18:38:31 +00:00
}
catch {
2022-11-02 23:19:59 +00:00
#Do nothing
2022-11-02 18:38:31 +00:00
}
2022-11-02 23:19:59 +00:00
}
try {
foreach ($Mod in $ExternalModsNames){
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) {
try {
Copy-Item $ExternalMods\$Mod -Destination $LocalMods\$Mod -Force
2022-11-03 00:19:04 +00:00
$ModsUpdated++
2022-11-02 23:19:59 +00:00
}
catch {
return $False
}
}
}
2022-11-03 00:19:04 +00:00
2022-11-02 23:19:59 +00:00
}
catch {
return $False
2022-11-02 18:38:31 +00:00
}
2022-11-03 00:19:04 +00:00
return $ModsUpdated
2022-11-02 18:38:31 +00:00
}
}
2022-11-02 23:19:59 +00:00
return $False
2022-11-02 18:38:31 +00:00
}