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

122 lines
2.7 KiB
PowerShell
Raw Normal View History

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