Enhance Download-WinRMConfigScript.ps1 to support $env:ZabbixProxyIPAddress fallback and improve parameter handling

This commit is contained in:
Petr Štěpán
2026-07-21 18:25:46 +02:00
parent f5cbad6162
commit fcf3bb39fc
@@ -15,8 +15,9 @@
.PARAMETER ZabbixProxyIPAddress .PARAMETER ZabbixProxyIPAddress
Zabbix Proxy IP address to allow through the WinRM firewall rule Zabbix Proxy IP address to allow through the WinRM firewall rule
(FWWinRMTrustedHosts in ConfigWinRM.xml). Omit this parameter to be prompted (FWWinRMTrustedHosts in ConfigWinRM.xml). Omit this parameter to be prompted
interactively; pass an empty string to skip the prompt and leave the existing interactively (or to fall back to $env:ZabbixProxyIPAddress if that is set); pass
value in ConfigWinRM.xml unchanged. an empty string to skip the prompt and leave the existing value in ConfigWinRM.xml
unchanged.
.EXAMPLE .EXAMPLE
PS> .\Download-WinRMConfigScript.ps1 PS> .\Download-WinRMConfigScript.ps1
@@ -27,27 +28,22 @@
PS> .\Download-WinRMConfigScript.ps1 -ZabbixProxyIPAddress '192.168.10.50' PS> .\Download-WinRMConfigScript.ps1 -ZabbixProxyIPAddress '192.168.10.50'
Downloads both files and sets FWWinRMTrustedHosts to 192.168.10.50 without prompting. Downloads both files and sets FWWinRMTrustedHosts to 192.168.10.50 without prompting.
.EXAMPLE
PS> .\Download-WinRMConfigScript.ps1 -ZabbixProxyIPAddress '192.168.10.50' -WhatIf
Shows what would be downloaded/changed without actually downloading or modifying
anything.
.EXAMPLE .EXAMPLE
PS> irm https://s.tslab.cz/ZBXWinRM | iex PS> irm https://s.tslab.cz/ZBXWinRM | iex
Runs the script directly from the repo without saving it locally first, prompting Shortest way to run the script directly from the repo without saving it locally
interactively for the Zabbix Proxy IP address. To pass -ZabbixProxyIPAddress in the first; prompts interactively for the Zabbix Proxy IP address.
same one-liner, wrap it as a script block instead:
PS> iex "& { $(irm https://s.tslab.cz/ZBXWinRM) } -ZabbixProxyIPAddress '192.168.10.50'"
.NOTES .NOTES
Author: Petr Štěpán Author: Petr Štěpán
Created: 2026-07-21 Created: 2026-07-21
Version: 1.0.0 Version: 1.1.0
Changelog: Changelog:
1.0.0 - Initial version 1.0.0 - Initial version
1.1.0 - Added $env:ZabbixProxyIPAddress fallback so a plain "irm | iex"
one-liner can supply the IP address without "& { ... } -Param" wrapping
#> #>
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $false, HelpMessage = 'Zabbix Proxy IP address to allow through the WinRM firewall rule. Leave empty to keep the existing value.')] [Parameter(Mandatory = $false, HelpMessage = 'Zabbix Proxy IP address to allow through the WinRM firewall rule. Leave empty to keep the existing value.')]
[string] [string]
@@ -78,7 +74,7 @@ function Invoke-RemoteFileDownload {
.PARAMETER OutFile .PARAMETER OutFile
Local destination file path. Local destination file path.
#> #>
[CmdletBinding(SupportsShouldProcess = $true)] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
@@ -91,7 +87,6 @@ function Invoke-RemoteFileDownload {
$OutFile $OutFile
) )
if ($PSCmdlet.ShouldProcess($OutFile, "Download from $Uri")) {
try { try {
Invoke-WebRequest -Uri $Uri -OutFile $OutFile -UseBasicParsing Invoke-WebRequest -Uri $Uri -OutFile $OutFile -UseBasicParsing
Write-Host "Downloaded '$Uri' to '$OutFile'." Write-Host "Downloaded '$Uri' to '$OutFile'."
@@ -101,7 +96,6 @@ function Invoke-RemoteFileDownload {
throw throw
} }
} }
}
function Update-WinRMTrustedHostConfig { function Update-WinRMTrustedHostConfig {
<# <#
@@ -112,7 +106,7 @@ function Update-WinRMTrustedHostConfig {
.PARAMETER TrustedHost .PARAMETER TrustedHost
Value to write into <FWWinRMTrustedHosts>. Value to write into <FWWinRMTrustedHosts>.
#> #>
[CmdletBinding(SupportsShouldProcess = $true)] [CmdletBinding()]
param( param(
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })] [ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
@@ -137,12 +131,10 @@ function Update-WinRMTrustedHostConfig {
throw "Element <FWWinRMTrustedHosts> was not found in '$Path'." throw "Element <FWWinRMTrustedHosts> was not found in '$Path'."
} }
if ($PSCmdlet.ShouldProcess($Path, "Set FWWinRMTrustedHosts to '$TrustedHost'")) {
$xml.ConfigWinRM.Options.FWWinRMTrustedHosts = $TrustedHost $xml.ConfigWinRM.Options.FWWinRMTrustedHosts = $TrustedHost
$xml.Save($Path) $xml.Save($Path)
Write-Host "FWWinRMTrustedHosts set to '$TrustedHost' in '$Path'." Write-Host "FWWinRMTrustedHosts set to '$TrustedHost' in '$Path'."
} }
}
catch { catch {
Write-Error "Failed to update '$Path': $_" Write-Error "Failed to update '$Path': $_"
throw throw
@@ -152,20 +144,28 @@ function Update-WinRMTrustedHostConfig {
# --- Main -------------------------------------------------------------- # --- Main --------------------------------------------------------------
try { try {
if (-not (Test-Path -Path $DestinationPath)) { if (-not (Test-Path -Path $DestinationPath)) {
if ($PSCmdlet.ShouldProcess($DestinationPath, 'Create directory')) {
New-Item -Path $DestinationPath -ItemType Directory -Force | Out-Null New-Item -Path $DestinationPath -ItemType Directory -Force | Out-Null
Write-Host "Created directory '$DestinationPath'." Write-Host "Created directory '$DestinationPath'."
} }
}
Invoke-RemoteFileDownload -Uri "$SourceBaseUri/ConfigWinRM.ps1" -OutFile $ScriptFile Invoke-RemoteFileDownload -Uri "$SourceBaseUri/ConfigWinRM.ps1" -OutFile $ScriptFile
Invoke-RemoteFileDownload -Uri "$SourceBaseUri/ConfigWinRM.xml" -OutFile $ConfigFile Invoke-RemoteFileDownload -Uri "$SourceBaseUri/ConfigWinRM.xml" -OutFile $ConfigFile
# Only prompt when the caller did not pass the parameter at all - an explicit empty # Only fall back to the environment variable / prompt when the caller did not pass
# string ("") means "skip the prompt, leave ConfigWinRM.xml unchanged". # the parameter at all - an explicit empty string ("") means "skip the prompt,
# leave ConfigWinRM.xml unchanged". $env:ZabbixProxyIPAddress lets a plain
# "irm <url> | iex" one-liner supply a value without needing "& { ... } -Param" -
# piping into iex runs the downloaded param() block as a plain statement with no
# bound arguments, so -ZabbixProxyIPAddress itself can only be set that way when
# the script is invoked as a real command (locally or via "& { ... }").
if (-not $PSBoundParameters.ContainsKey('ZabbixProxyIPAddress')) { if (-not $PSBoundParameters.ContainsKey('ZabbixProxyIPAddress')) {
if ($env:ZabbixProxyIPAddress) {
$ZabbixProxyIPAddress = $env:ZabbixProxyIPAddress
}
else {
$ZabbixProxyIPAddress = Read-Host -Prompt 'Enter Zabbix Proxy IP address (press Enter to keep the current value)' $ZabbixProxyIPAddress = Read-Host -Prompt 'Enter Zabbix Proxy IP address (press Enter to keep the current value)'
} }
}
if ([string]::IsNullOrWhiteSpace($ZabbixProxyIPAddress)) { if ([string]::IsNullOrWhiteSpace($ZabbixProxyIPAddress)) {
Write-Host 'No Zabbix Proxy IP address provided - ConfigWinRM.xml left unchanged.' Write-Host 'No Zabbix Proxy IP address provided - ConfigWinRM.xml left unchanged.'