From 9f85f4795c398285d9cbcfc4695851bce268c8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0t=C4=9Bp=C3=A1n?= Date: Tue, 21 Jul 2026 18:38:30 +0200 Subject: [PATCH] Fix handling of missing FWWinRMTrustedHosts element and update version to 1.2.2 --- .../Download-WinRMConfigScript.ps1 | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/ConfigWinRMForZabbix/Download-WinRMConfigScript.ps1 b/ConfigWinRMForZabbix/Download-WinRMConfigScript.ps1 index 0f5febd..f621f91 100644 --- a/ConfigWinRMForZabbix/Download-WinRMConfigScript.ps1 +++ b/ConfigWinRMForZabbix/Download-WinRMConfigScript.ps1 @@ -1,4 +1,4 @@ -<# +<# .SYNOPSIS Downloads ConfigWinRM.ps1 and ConfigWinRM.xml to C:\WinRM and optionally sets the Zabbix Proxy IP address as the WinRM firewall trusted host. @@ -34,15 +34,24 @@ first; prompts interactively for the Zabbix Proxy IP address. .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 Created: 2026-07-21 - Version: 1.2.0 + Version: 1.2.2 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 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 + 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 + element is detected cleanly instead of throwing + a raw StrictMode error #> [CmdletBinding()] @@ -128,12 +137,15 @@ function Update-WinRMTrustedHostConfig { $xml.PreserveWhitespace = $true $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) { throw "Element was not found in '$Path'." } - $xml.ConfigWinRM.Options.FWWinRMTrustedHosts = $TrustedHost + $node.InnerText = $TrustedHost $xml.Save($Path) Write-Host "FWWinRMTrustedHosts set to '$TrustedHost' in '$Path'." } @@ -183,16 +195,16 @@ try { } catch { Write-Error "Script failed: $_" - # $MyInvocation.MyCommand.Path is only set when this runs as an actual .ps1 file. - # When run via "irm | iex" there is no backing file - the code executes in - # the caller's own session, so calling exit here would close their whole - # PowerShell window instead of just ending this script. - if ($MyInvocation.MyCommand.Path) { + # $PSCommandPath is only set when this runs as an actual .ps1 file. When run via + # "irm | iex" there is no backing file - the code executes in the caller's + # own session, so calling exit here would close their whole PowerShell window + # instead of just ending this script. + if ($PSCommandPath) { exit 1 } return } -if ($MyInvocation.MyCommand.Path) { +if ($PSCommandPath) { exit 0 }