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

75 lines
2.5 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
if ($ModsPath -like "http*") {
$ExternalMods = "$ModsPath/"
$wc = New-Object System.Net.WebClient
try {
$wc.OpenRead("$ExternalMods").Close() | Out-Null
$dateExternal = ([DateTime]$wc.ResponseHeaders['Last-Modified']).ToString("yyyy-MM-dd HH:mm:ss")
if ($dateExternal -gt $dateLocal) {
try {
$wc.DownloadFile($ExternalMods, $LocalMods)
}
catch {
return $False
}
return $true
}
}
catch {
return $False
}
}
# 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
}