diff --git a/doc/31-Changelog.md b/doc/31-Changelog.md index b607a63..4257de1 100644 --- a/doc/31-Changelog.md +++ b/doc/31-Changelog.md @@ -13,6 +13,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic ### Bugfixes +* [#059](https://github.com/Icinga/icinga-powershell-framework/issues/059), [#060](https://github.com/Icinga/icinga-powershell-framework/pull/060) Fixes interface handling for multiple interfaces and returns only the main interface by fallback to routing table and adds support for Windows 2008 R2 * [#127](https://github.com/Icinga/icinga-powershell-framework/issues/127) Fixes wrong error message on failed MSSQL connection due to database not reachable by using `-IntegratedSecurity` * [#128](https://github.com/Icinga/icinga-powershell-framework/issues/128) Fixes unhandled output from loading `System.Reflection.Assembly` which can cause weird side effects for plugin outputs * [#130](https://github.com/Icinga/icinga-powershell-framework/issues/130) Fix crash while running services as background task to collect metrics over time by missing Performance Counter cache initialisation diff --git a/lib/core/tools/Get-IcingaNetworkInterface.psm1 b/lib/core/tools/Get-IcingaNetworkInterface.psm1 index e06a918..bf5c8e2 100644 --- a/lib/core/tools/Get-IcingaNetworkInterface.psm1 +++ b/lib/core/tools/Get-IcingaNetworkInterface.psm1 @@ -36,6 +36,13 @@ function Get-IcingaNetworkInterface() return $null; } + # Ensure that we can still process on older Windows system where + # Get-NetRoute ist not available + if ((Test-IcingaFunction 'Get-NetRoute') -eq $FALSE) { + Write-IcingaConsoleWarning 'Your Windows system does not support "Get-NetRoute". A fallback solution is used to fetch the IP of the first Network Interface routing through 0.0.0.0' + return (Get-IcingaNetworkRoute).Interface; + } + try { [array]$IP = ([System.Net.Dns]::GetHostAddresses($IP)).IPAddressToString; } catch { @@ -150,14 +157,21 @@ function Get-IcingaNetworkInterface() } } - $InternalCount = 0; - $UseInterface = ''; + $InternalCount = 0; + [array]$UseInterface = @(); foreach ($interface in $ExternalInterfaces.Keys) { $currentCount = $ExternalInterfaces[$interface].count; if ($currentCount -gt $InternalCount) { $InternalCount = $currentCount; - $UseInterface = $interface; + $UseInterface += $interface; } } - return $UseInterface; + + # In case we found multiple interfaces, fallback to our + # 'route print' function and return this interface instead + if ($UseInterface.Count -ne 1) { + return (Get-IcingaNetworkRoute).Interface; + } + + return $UseInterface[0]; } diff --git a/lib/core/tools/Get-IcingaNetworkRoute.psm1 b/lib/core/tools/Get-IcingaNetworkRoute.psm1 new file mode 100644 index 0000000..f1af9e6 --- /dev/null +++ b/lib/core/tools/Get-IcingaNetworkRoute.psm1 @@ -0,0 +1,37 @@ +<# +.SYNOPSIS + Fetch the used interface for our Windows System +.DESCRIPTION + Newer Windows systems provide a Cmdlet 'Get-NetRoute' for fetching the + network route configurations. Older systems however do not provide this + and to ensure some sort of backwards compatibility, we will have a look + on our route configuration and return the first valid interface found +.FUNCTIONALITY + This Cmdlet will return first valid IP for our interface +.EXAMPLE + PS>Get-IcingaNetworkRoute +.OUTPUTS + System.Array +.LINK + https://github.com/Icinga/icinga-powershell-framework +.NOTES +#> + +function Get-IcingaNetworkRoute() +{ + $RouteConfig = (&route print | Where-Object { + $_.TrimStart() -Like "0.0.0.0*"; + }).Split() | Where-Object { + return $_; + }; + + $Interface = @{ + 'Destination' = $RouteConfig[0]; + 'Netmask' = $RouteConfig[1]; + 'Gateway' = $RouteConfig[2]; + 'Interface' = $RouteConfig[3]; + 'Metric' = $RouteConfig[4]; + } + + return $Interface; +}