From ce9257a30d22f7de9c83ddbf7b98365efd445eb0 Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Thu, 12 Dec 2019 14:19:37 +0100 Subject: [PATCH] Improved code styling --- .../tools/ConvertTo-IcingaIPv4BinaryString.psm1 | 10 +++++++--- .../tools/ConvertTo-IcingaIPv6BinaryString.psm1 | 9 ++++++--- lib/core/tools/Expand-IcingaIPv6String.psm1 | 16 ++++++++-------- lib/core/tools/Get-IcingaNetworkInterface.psm1 | 7 ++++++- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/core/tools/ConvertTo-IcingaIPv4BinaryString.psm1 b/lib/core/tools/ConvertTo-IcingaIPv4BinaryString.psm1 index c4a8adb..a6c9133 100644 --- a/lib/core/tools/ConvertTo-IcingaIPv4BinaryString.psm1 +++ b/lib/core/tools/ConvertTo-IcingaIPv4BinaryString.psm1 @@ -24,11 +24,15 @@ function ConvertTo-IcingaIPv4BinaryString() param( [string]$IP ); + $IP = $IP -split '\.' | ForEach-Object { - [System.Convert]::ToString($_,2).PadLeft(8,'0'); + [System.Convert]::ToString($_, 2).PadLeft(8, '0'); } $IP = $IP -join ''; $IP = $IP -replace '\s',''; - return @{'value' = $IP; 'name' = 'IPv4'} -} \ No newline at end of file + return @{ + 'value' = $IP; + 'name' = 'IPv4' + } +} diff --git a/lib/core/tools/ConvertTo-IcingaIPv6BinaryString.psm1 b/lib/core/tools/ConvertTo-IcingaIPv6BinaryString.psm1 index 5fa202b..87d3124 100644 --- a/lib/core/tools/ConvertTo-IcingaIPv6BinaryString.psm1 +++ b/lib/core/tools/ConvertTo-IcingaIPv6BinaryString.psm1 @@ -24,7 +24,7 @@ function ConvertTo-IcingaIPv6BinaryString() param( [string]$IP ); - [string]$IP = Expand-IcingaIPv6String $IP; + [string]$IP = Expand-IcingaIPv6String $IP; [array]$IPArr = $IP.Split(':'); $IPArr = $IPArr.ToCharArray(); @@ -34,5 +34,8 @@ function ConvertTo-IcingaIPv6BinaryString() $IP = $IP -join ''; $IP = $IP -replace '\s',''; - return @{'value' = $IP; 'name' = 'IPv6'} -} \ No newline at end of file + return @{ + 'value' = $IP; + 'name' = 'IPv6' + } +} diff --git a/lib/core/tools/Expand-IcingaIPv6String.psm1 b/lib/core/tools/Expand-IcingaIPv6String.psm1 index f8b495f..8637b89 100644 --- a/lib/core/tools/Expand-IcingaIPv6String.psm1 +++ b/lib/core/tools/Expand-IcingaIPv6String.psm1 @@ -28,14 +28,14 @@ function Expand-IcingaIPv6String() [String]$IP ); - $Counter = 0 - $RelV = -1 + $Counter = 0; + $RelV = -1; - for($Index = 0; $Index -lt $IP.Length; $Index++) { + for ($Index = 0; $Index -lt $IP.Length; $Index++) { if ($IP[$Index] -eq ':') { - $Counter++ + $Counter++; if (($Index - 1) -ge 0 -and $IP[$Index - 1] -eq ':'){ - $RelV = $Index + $RelV = $Index; } } } @@ -46,7 +46,7 @@ function Expand-IcingaIPv6String() } if ($Counter -lt 7) { - $IP = $IP.Substring(0, $RelV) + (':'*(7 - $Counter)) + $IP.Substring($RelV) + $IP = $IP.Substring(0, $RelV) + (':'*(7 - $Counter)) + $IP.Substring($RelV); } $Result = @(); @@ -59,9 +59,9 @@ function Expand-IcingaIPv6String() [System.Globalization.CultureInfo]::InvariantCulture, [Ref]$Value ) | Out-Null; - $Result += ('{0:X4}' -f $Value) + $Result += ('{0:X4}' -f $Value); } $Result = $Result -join ':'; return $Result; -} \ No newline at end of file +} diff --git a/lib/core/tools/Get-IcingaNetworkInterface.psm1 b/lib/core/tools/Get-IcingaNetworkInterface.psm1 index 39e40d9..f8f514e 100644 --- a/lib/core/tools/Get-IcingaNetworkInterface.psm1 +++ b/lib/core/tools/Get-IcingaNetworkInterface.psm1 @@ -31,6 +31,11 @@ function Get-IcingaNetworkInterface() [string]$IP ); + if ([string]::IsNullOrEmpty($IP)) { + Write-Host 'Please specify a valid IP-Address or FQDN'; + return $null; + } + try { $IP = ([System.Net.Dns]::GetHostAddresses($IP)).IPAddressToString; } catch { @@ -116,4 +121,4 @@ function Get-IcingaNetworkInterface() } } return ((Get-NetIPAddress -InterfaceIndex (Get-NetRoute | Where-Object -Property DestinationPrefix -like '0.0.0.0/0')[0].IfIndex -AddressFamily $IPBinStringMaster.name).IPAddress).split('%')[0]; -} \ No newline at end of file +}