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

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