Fix handling of missing FWWinRMTrustedHosts element and update version to 1.2.2

This commit is contained in:
Petr Štěpán
2026-07-21 18:38:30 +02:00
parent 54bcd25553
commit 9f85f4795c
@@ -1,4 +1,4 @@
<# <#
.SYNOPSIS .SYNOPSIS
Downloads ConfigWinRM.ps1 and ConfigWinRM.xml to C:\WinRM and optionally sets the Downloads ConfigWinRM.ps1 and ConfigWinRM.xml to C:\WinRM and optionally sets the
Zabbix Proxy IP address as the WinRM firewall trusted host. Zabbix Proxy IP address as the WinRM firewall trusted host.
@@ -34,15 +34,24 @@
first; prompts interactively for the Zabbix Proxy IP address. first; prompts interactively for the Zabbix Proxy IP address.
.NOTES .NOTES
This file is saved as UTF-8 with BOM because the author name below contains
non-ASCII characters (diacritics) - keep the BOM if this file is re-saved.
Author: Petr Štěpán Author: Petr Štěpán
Created: 2026-07-21 Created: 2026-07-21
Version: 1.2.0 Version: 1.2.2
Changelog: Changelog:
1.0.0 - Initial version 1.0.0 - Initial version
1.1.0 - Added $env:ZabbixProxyIPAddress fallback so a plain "irm | iex" 1.1.0 - Added $env:ZabbixProxyIPAddress fallback so a plain "irm | iex"
one-liner can supply the IP address without "& { ... } -Param" wrapping one-liner can supply the IP address without "& { ... } -Param" wrapping
1.2.0 - Only call exit when running as a real .ps1 file, not when run via 1.2.0 - Only call exit when running as a real .ps1 file, not when run via
"irm | iex" - exit would otherwise close the caller's PowerShell window "irm | iex" - exit would otherwise close the caller's PowerShell window
1.2.1 - Fixed "'Path' cannot be found" under Set-StrictMode when run via
iex - use $PSCommandPath instead of $MyInvocation.MyCommand.Path
1.2.2 - Fixed the same class of error in Update-WinRMTrustedHostConfig: use
SelectSingleNode instead of dot-property access so a missing
<FWWinRMTrustedHosts> element is detected cleanly instead of throwing
a raw StrictMode error
#> #>
[CmdletBinding()] [CmdletBinding()]
@@ -128,12 +137,15 @@ function Update-WinRMTrustedHostConfig {
$xml.PreserveWhitespace = $true $xml.PreserveWhitespace = $true
$xml.Load($Path) $xml.Load($Path)
$node = $xml.ConfigWinRM.Options.FWWinRMTrustedHosts # SelectSingleNode (XPath), not dot-property access - a missing element would
# throw "property cannot be found" under Set-StrictMode -Version Latest instead
# of returning $null, which would skip the not-found check below entirely.
$node = $xml.SelectSingleNode('/ConfigWinRM/Options/FWWinRMTrustedHosts')
if ($null -eq $node) { if ($null -eq $node) {
throw "Element <FWWinRMTrustedHosts> was not found in '$Path'." throw "Element <FWWinRMTrustedHosts> was not found in '$Path'."
} }
$xml.ConfigWinRM.Options.FWWinRMTrustedHosts = $TrustedHost $node.InnerText = $TrustedHost
$xml.Save($Path) $xml.Save($Path)
Write-Host "FWWinRMTrustedHosts set to '$TrustedHost' in '$Path'." Write-Host "FWWinRMTrustedHosts set to '$TrustedHost' in '$Path'."
} }
@@ -183,16 +195,16 @@ try {
} }
catch { catch {
Write-Error "Script failed: $_" Write-Error "Script failed: $_"
# $MyInvocation.MyCommand.Path is only set when this runs as an actual .ps1 file. # $PSCommandPath is only set when this runs as an actual .ps1 file. When run via
# When run via "irm <url> | iex" there is no backing file - the code executes in # "irm <url> | iex" there is no backing file - the code executes in the caller's
# the caller's own session, so calling exit here would close their whole # own session, so calling exit here would close their whole PowerShell window
# PowerShell window instead of just ending this script. # instead of just ending this script.
if ($MyInvocation.MyCommand.Path) { if ($PSCommandPath) {
exit 1 exit 1
} }
return return
} }
if ($MyInvocation.MyCommand.Path) { if ($PSCommandPath) {
exit 0 exit 0
} }