Skip to content

Commit 5648792

Browse files
authored
Merge pull request #1000 from FaserF/script-hardening
WAU script hardening
2 parents 46562ac + c6856b7 commit 5648792

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

Sources/Winget-AutoUpdate/WAU-Installer-GUI.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ Add-Type -AssemblyName PresentationFramework
381381
Start-PopUp "Starting..."
382382

383383
#Set config
384-
$null = cmd /c ''
384+
$null = & "$env:WINDIR\System32\cmd.exe" /c ""
385385
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
386386
$Script:ProgressPreference = "SilentlyContinue"
387387
$Script:ErrorActionPreference = "SilentlyContinue"

Sources/Winget-AutoUpdate/Winget-Upgrade.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Get-ChildItem -Path "$($Script:WorkingDir)\functions" -File -Filter "*.ps1" -Dep
99
<# MAIN #>
1010

1111
# Config console output encoding
12-
$null = cmd /c '';
12+
$null = & "$env:WINDIR\System32\cmd.exe" /c ""
1313
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8;
1414
$Script:ProgressPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue;
1515

Sources/Winget-AutoUpdate/functions/Get-WingetOutdatedApps.ps1

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
function Get-WingetOutdatedApps {
44

5-
Param(
6-
[Parameter(Position=0,Mandatory=$True,HelpMessage="You MUST supply value for winget repo, we need it")]
7-
[ValidateNotNullorEmpty()]
8-
[string]$src
9-
)
5+
Param(
6+
[Parameter(Position = 0, Mandatory = $True, HelpMessage = "You MUST supply value for winget repo, we need it")]
7+
[ValidateNotNullorEmpty()]
8+
[string]$src
9+
)
1010
class Software {
1111
[string]$Name
1212
[string]$Id
@@ -15,7 +15,13 @@ Param(
1515
}
1616

1717
#Get list of available upgrades on winget format
18-
$upgradeResult = & $Winget upgrade --source $src | Where-Object { $_ -notlike " *" } | Out-String
18+
try {
19+
$upgradeResult = & $Winget upgrade --source $src | Where-Object { $_ -notlike " *" } | Out-String
20+
}
21+
catch {
22+
Write-ToLog "Error while recieving winget upgrade list: $_" "Red"
23+
$upgradeResult = $null
24+
}
1925

2026
#Start Conversion of winget format to an array. Check if "-----" exists (Winget Error Handling)
2127
if (!($upgradeResult -match "-----")) {

Sources/Winget-AutoUpdate/functions/Test-Network.ps1

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
function Test-Network {
44

5-
#Init
5+
# Init
66
$timeout = 0
77

88
#Test connectivity during 30 min then timeout
99
Write-ToLog "Checking internet connection..." "Yellow"
1010

1111
try {
1212
$NlaRegKey = "HKLM:\SYSTEM\CurrentControlSet\Services\NlaSvc\Parameters\Internet"
13-
$ncsiHost = Get-ItemPropertyValue -Path $NlaRegKey -Name ActiveWebProbeHost
14-
$ncsiPath = Get-ItemPropertyValue -Path $NlaRegKey -Name ActiveWebProbePath
13+
$ncsiHost = Get-ItemPropertyValue -Path $NlaRegKey -Name ActiveWebProbeHost
14+
$ncsiPath = Get-ItemPropertyValue -Path $NlaRegKey -Name ActiveWebProbePath
1515
$ncsiContent = Get-ItemPropertyValue -Path $NlaRegKey -Name ActiveWebProbeContent
1616
}
1717
catch {
@@ -20,7 +20,7 @@ function Test-Network {
2020
$ncsiContent = "Microsoft Connect Test"
2121
}
2222

23-
While ($timeout -lt 1800) {
23+
while ($timeout -lt 1800) {
2424
try {
2525
$ncsiResponse = Invoke-WebRequest -Uri "http://$($ncsiHost)/$($ncsiPath)" -UseBasicParsing -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::Chrome); # DevSkim: ignore DS137138 Insecure URL
2626
}
@@ -31,67 +31,60 @@ function Test-Network {
3131
if (($ncsiResponse) -and ($ncsiResponse.StatusCode -eq 200) -and ($ncsiResponse.content -eq $ncsiContent)) {
3232
Write-ToLog "Connected !" "Green"
3333

34-
#Check for metered connection
35-
[void][Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime]
36-
$cost = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile().GetConnectionCost()
34+
# Check for metered connection
35+
try {
36+
[void][Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime]
37+
$cost = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile().GetConnectionCost()
3738

38-
if ($cost.ApproachingDataLimit -or $cost.OverDataLimit -or $cost.Roaming -or $cost.BackgroundDataUsageRestricted -or ($cost.NetworkCostType -ne "Unrestricted")) {
39+
$networkCostTypeName = [Windows.Networking.Connectivity.NetworkCostType]::GetName(
40+
[Windows.Networking.Connectivity.NetworkCostType],
41+
$cost.NetworkCostType
42+
)
43+
}
44+
catch {
45+
Write-ToLog "Could not evaluate metered connection status - skipping check." "Gray"
46+
return $true
47+
}
3948

49+
if ($cost.ApproachingDataLimit -or $cost.OverDataLimit -or $cost.Roaming -or $cost.BackgroundDataUsageRestricted -or ($networkCostTypeName -ne "Unrestricted")) {
4050
Write-ToLog "Metered connection detected." "Yellow"
4151

4252
if ($WAUConfig.WAU_DoNotRunOnMetered -eq 1) {
43-
4453
Write-ToLog "WAU is configured to bypass update checking on metered connection"
4554
return $false
46-
4755
}
4856
else {
49-
5057
Write-ToLog "WAU is configured to force update checking on metered connection"
5158
return $true
52-
5359
}
54-
5560
}
5661
else {
57-
5862
return $true
59-
6063
}
61-
6264
}
6365
else {
64-
6566
Start-Sleep 10
6667
$timeout += 10
6768

68-
#Send Warning Notif if no connection for 5 min
6969
if ($timeout -eq 300) {
70-
#Log
7170
Write-ToLog "Notify 'No connection' sent." "Yellow"
7271

73-
#Notif
7472
$Title = $NotifLocale.local.outputs.output[0].title
7573
$Message = $NotifLocale.local.outputs.output[0].message
7674
$MessageType = "warning"
7775
$Balise = "Connection"
7876
Start-NotifTask -Title $Title -Message $Message -MessageType $MessageType -Balise $Balise
7977
}
80-
8178
}
82-
8379
}
8480

85-
#Send Timeout Notif if no connection for 30 min
8681
Write-ToLog "Timeout. No internet connection !" "Red"
8782

88-
#Notif
8983
$Title = $NotifLocale.local.outputs.output[1].title
9084
$Message = $NotifLocale.local.outputs.output[1].message
9185
$MessageType = "error"
9286
$Balise = "Connection"
9387
Start-NotifTask -Title $Title -Message $Message -MessageType $MessageType -Balise $Balise
9488

9589
return $false
96-
97-
}
90+
}

0 commit comments

Comments
 (0)