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

94 lines
2.9 KiB
PowerShell
Raw Normal View History

#Function to check Block/Allow List External Path
2022-09-17 19:22:41 +00:00
function Test-ListPath ($ListPath, $UseWhiteList, $WingetUpdatePath) {
# URL, UNC or Local Path
if ($UseWhiteList) {
$ListType = "included_apps.txt"
}
else {
$ListType = "excluded_apps.txt"
}
# Get local and external list paths
$LocalList = -join ($WingetUpdatePath, "\", $ListType)
$ExternalList = -join ($ListPath, "\", $ListType)
# Check if a list exists
if (Test-Path "$LocalList") {
$dateLocal = (Get-Item "$LocalList").LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
}
# If path is URL
if ($ListPath -like "http*") {
$ExternalList = -join ($ListPath, "/", $ListType)
# Test if $ListPath contains the character "?" (testing for SAS token)
if ($Listpath -match "\?") {
# Split the URL into two strings at the "?" substring
$splitPath = $ListPath.Split("`?")
# Assign the first string (up to "?") to the variable $resourceURI
$resourceURI = $splitPath[0]
# Assign the second string (after "?" to the end) to the variable $sasToken
$sasToken = $splitPath[1]
# Join the parts and add "/$ListType?" in between the parts
$ExternalList = -join ($resourceURI, "/$ListType`?", $sasToken)
}
$wc = New-Object System.Net.WebClient
try {
$wc.OpenRead("$ExternalList").Close() | Out-Null
$dateExternal = ([DateTime]$wc.ResponseHeaders['Last-Modified']).ToString("yyyy-MM-dd HH:mm:ss")
if ($dateExternal -gt $dateLocal) {
try {
$wc.DownloadFile($ExternalList, $LocalList)
}
catch {
$Script:ReachNoPath = $True
return $False
}
return $true
2022-09-19 21:10:00 +00:00
}
}
catch {
try {
$wc.DownloadFile($ExternalList, $LocalList)
$Script:AlwaysDownloaded = $True
return $true
}
catch {
$Script:ReachNoPath = $True
return $False
}
}
}
# If path is UNC or local
else {
if (Test-Path -Path $ExternalList) {
try {
$dateExternal = (Get-Item "$ExternalList").LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
2023-01-08 17:57:47 +00:00
}
catch {
$Script:ReachNoPath = $True
return $False
2023-01-08 17:57:47 +00:00
}
if ($dateExternal -gt $dateLocal) {
try {
Copy-Item $ExternalList -Destination $LocalList -Force
}
catch {
$Script:ReachNoPath = $True
return $False
}
return $True
2022-09-18 18:25:14 +00:00
}
}
else {
$Script:ReachNoPath = $True
return $False
}
}
2022-09-18 18:25:14 +00:00
}