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
Zabbix Proxy IP address to allow through the WinRM firewall rule
(FWWinRMTrustedHosts in ConfigWinRM.xml). Omit this parameter to be prompted
interactively; pass an empty string to skip the prompt and leave the existing
value in ConfigWinRM.xml unchanged.
interactively (or to fall back to $env:ZabbixProxyIPAddress if that is set); pass
an empty string to skip the prompt and leave the existing value in ConfigWinRM.xml
unchanged.
.EXAMPLE
PS> .\Download-WinRMConfigScript.ps1
@@ -27,27 +28,22 @@
PS> .\Download-WinRMConfigScript.ps1 -ZabbixProxyIPAddress '192.168.10.50'
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
PS> irm https://s.tslab.cz/ZBXWinRM | iex
Runs the script directly from the repo without saving it locally first, prompting
interactively for the Zabbix Proxy IP address. To pass -ZabbixProxyIPAddress in the
same one-liner, wrap it as a script block instead:
PS> iex "& { $(irm https://s.tslab.cz/ZBXWinRM) } -ZabbixProxyIPAddress '192.168.10.50'"
Shortest way to run the script directly from the repo without saving it locally
first; prompts interactively for the Zabbix Proxy IP address.
.NOTES
Author: Petr Štěpán
Created: 2026-07-21
Version: 1.0.0
Version: 1.1.0
Changelog:
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(
[Parameter(Mandatory = $false, HelpMessage = 'Zabbix Proxy IP address to allow through the WinRM firewall rule. Leave empty to keep the existing value.')]
[string]
@@ -78,7 +74,7 @@ function Invoke-RemoteFileDownload {
.PARAMETER OutFile
Local destination file path.
#>
[CmdletBinding(SupportsShouldProcess = $true)]
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
@@ -91,7 +87,6 @@ function Invoke-RemoteFileDownload {
$OutFile
)
if ($PSCmdlet.ShouldProcess($OutFile, "Download from $Uri")) {
try {
Invoke-WebRequest -Uri $Uri -OutFile $OutFile -UseBasicParsing
Write-Host "Downloaded '$Uri' to '$OutFile'."
@@ -100,7 +95,6 @@ function Invoke-RemoteFileDownload {
Write-Error "Failed to download '$Uri' to '$OutFile': $_"
throw
}
}
}
function Update-WinRMTrustedHostConfig {
@@ -112,7 +106,7 @@ function Update-WinRMTrustedHostConfig {
.PARAMETER TrustedHost
Value to write into <FWWinRMTrustedHosts>.
#>
[CmdletBinding(SupportsShouldProcess = $true)]
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
@@ -137,12 +131,10 @@ function Update-WinRMTrustedHostConfig {
throw "Element <FWWinRMTrustedHosts> was not found in '$Path'."
}
if ($PSCmdlet.ShouldProcess($Path, "Set FWWinRMTrustedHosts to '$TrustedHost'")) {
$xml.ConfigWinRM.Options.FWWinRMTrustedHosts = $TrustedHost
$xml.Save($Path)
Write-Host "FWWinRMTrustedHosts set to '$TrustedHost' in '$Path'."
}
}
catch {
Write-Error "Failed to update '$Path': $_"
throw
@@ -152,20 +144,28 @@ function Update-WinRMTrustedHostConfig {
# --- Main --------------------------------------------------------------
try {
if (-not (Test-Path -Path $DestinationPath)) {
if ($PSCmdlet.ShouldProcess($DestinationPath, 'Create directory')) {
New-Item -Path $DestinationPath -ItemType Directory -Force | Out-Null
Write-Host "Created directory '$DestinationPath'."
}
}
Invoke-RemoteFileDownload -Uri "$SourceBaseUri/ConfigWinRM.ps1" -OutFile $ScriptFile
Invoke-RemoteFileDownload -Uri "$SourceBaseUri/ConfigWinRM.xml" -OutFile $ConfigFile
# Only prompt when the caller did not pass the parameter at all - an explicit empty
# string ("") means "skip the prompt, leave ConfigWinRM.xml unchanged".
# Only fall back to the environment variable / prompt when the caller did not pass
# 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 ($env:ZabbixProxyIPAddress) {
$ZabbixProxyIPAddress = $env:ZabbixProxyIPAddress
}
else {
$ZabbixProxyIPAddress = Read-Host -Prompt 'Enter Zabbix Proxy IP address (press Enter to keep the current value)'
}
}
if ([string]::IsNullOrWhiteSpace($ZabbixProxyIPAddress)) {
Write-Host 'No Zabbix Proxy IP address provided - ConfigWinRM.xml left unchanged.'