Enhance Download-WinRMConfigScript.ps1 to support $env:ZabbixProxyIPAddress fallback and improve parameter handling
This commit is contained in:
@@ -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,15 +87,13 @@ 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'."
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to download '$Uri' to '$OutFile': $_"
|
||||
throw
|
||||
}
|
||||
try {
|
||||
Invoke-WebRequest -Uri $Uri -OutFile $OutFile -UseBasicParsing
|
||||
Write-Host "Downloaded '$Uri' to '$OutFile'."
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to download '$Uri' to '$OutFile': $_"
|
||||
throw
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,11 +131,9 @@ 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'."
|
||||
}
|
||||
$xml.ConfigWinRM.Options.FWWinRMTrustedHosts = $TrustedHost
|
||||
$xml.Save($Path)
|
||||
Write-Host "FWWinRMTrustedHosts set to '$TrustedHost' in '$Path'."
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to update '$Path': $_"
|
||||
@@ -152,19 +144,27 @@ 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'."
|
||||
}
|
||||
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')) {
|
||||
$ZabbixProxyIPAddress = Read-Host -Prompt 'Enter Zabbix Proxy IP address (press Enter to keep the current value)'
|
||||
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)) {
|
||||
|
||||
Reference in New Issue
Block a user