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

84 lines
2.5 KiB
PowerShell
Raw Permalink Normal View History

2023-03-31 15:56:07 +00:00
#Function to check Block/Allow List External Path
2022-09-17 19:22:41 +00:00
function Test-ListPath ($ListPath, $UseWhiteList, $WingetUpdatePath) {
2022-09-23 16:00:46 +00:00
# URL, UNC or Local Path
2022-10-18 13:23:39 +00:00
if ($UseWhiteList) {
$ListType = "included_apps.txt"
2022-09-18 18:25:14 +00:00
}
else {
2022-10-18 13:23:39 +00:00
$ListType = "excluded_apps.txt"
2022-09-18 18:25:14 +00:00
}
2022-09-23 16:00:46 +00:00
# Get local and external list paths
2022-10-18 13:23:39 +00:00
$LocalList = -join ($WingetUpdatePath, "\", $ListType)
$ExternalList = -join ($ListPath, "\", $ListType)
2022-09-23 16:00:46 +00:00
# Check if a list exists
2022-09-19 22:54:23 +00:00
if (Test-Path "$LocalList") {
$dateLocal = (Get-Item "$LocalList").LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
}
2022-09-18 18:40:22 +00:00
2022-09-23 16:00:46 +00:00
# If path is URL
2022-10-18 13:23:39 +00:00
if ($ListPath -like "http*") {
$ExternalList = -join ($ListPath, "/", $ListType)
2022-09-18 18:25:14 +00:00
$wc = New-Object System.Net.WebClient
try {
2022-09-19 21:10:00 +00:00
$wc.OpenRead("$ExternalList").Close() | Out-Null
2022-09-19 21:45:18 +00:00
$dateExternal = ([DateTime]$wc.ResponseHeaders['Last-Modified']).ToString("yyyy-MM-dd HH:mm:ss")
2022-09-19 21:10:00 +00:00
if ($dateExternal -gt $dateLocal) {
2022-09-20 01:08:06 +00:00
try {
$wc.DownloadFile($ExternalList, $LocalList)
}
catch {
2023-01-08 17:57:47 +00:00
$Script:ReachNoPath = $True
2022-09-20 01:08:06 +00:00
return $False
}
2022-09-19 22:54:23 +00:00
return $true
2022-09-19 21:10:00 +00:00
}
}
catch {
try {
$content = $wc.DownloadString("$ExternalList")
2023-03-28 08:22:43 +00:00
if ($null -ne $content -and $content -match "\w\.\w") {
$wc.DownloadFile($ExternalList, $LocalList)
return $true
}
else {
$Script:ReachNoPath = $True
return $False
}
}
catch {
$Script:ReachNoPath = $True
return $False
}
2022-09-18 18:25:14 +00:00
}
}
2022-09-23 16:00:46 +00:00
# If path is UNC or local
2022-09-18 18:25:14 +00:00
else {
2023-01-08 17:57:47 +00:00
if (Test-Path -Path $ExternalList) {
try {
$dateExternal = (Get-Item "$ExternalList").LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
}
catch {
$Script:ReachNoPath = $True
return $False
}
if ($dateExternal -gt $dateLocal) {
2022-09-20 01:08:06 +00:00
try {
Copy-Item $ExternalList -Destination $LocalList -Force
}
catch {
2023-01-08 17:57:47 +00:00
$Script:ReachNoPath = $True
2022-09-20 01:08:06 +00:00
return $False
}
2023-01-08 17:57:47 +00:00
return $True
2022-09-18 18:25:14 +00:00
}
}
2023-01-08 17:57:47 +00:00
else {
$Script:ReachNoPath = $True
}
return $False
2022-09-18 18:25:14 +00:00
}
}