Improved code styling

This commit is contained in:
Christian Stein 2019-12-12 14:19:37 +01:00
parent 9adfe087d8
commit ce9257a30d
4 changed files with 27 additions and 15 deletions

View file

@ -24,11 +24,15 @@ function ConvertTo-IcingaIPv4BinaryString()
param( param(
[string]$IP [string]$IP
); );
$IP = $IP -split '\.' | ForEach-Object { $IP = $IP -split '\.' | ForEach-Object {
[System.Convert]::ToString($_,2).PadLeft(8,'0'); [System.Convert]::ToString($_, 2).PadLeft(8, '0');
} }
$IP = $IP -join ''; $IP = $IP -join '';
$IP = $IP -replace '\s',''; $IP = $IP -replace '\s','';
return @{'value' = $IP; 'name' = 'IPv4'} return @{
'value' = $IP;
'name' = 'IPv4'
}
} }

View file

@ -24,7 +24,7 @@ function ConvertTo-IcingaIPv6BinaryString()
param( param(
[string]$IP [string]$IP
); );
[string]$IP = Expand-IcingaIPv6String $IP; [string]$IP = Expand-IcingaIPv6String $IP;
[array]$IPArr = $IP.Split(':'); [array]$IPArr = $IP.Split(':');
$IPArr = $IPArr.ToCharArray(); $IPArr = $IPArr.ToCharArray();
@ -34,5 +34,8 @@ function ConvertTo-IcingaIPv6BinaryString()
$IP = $IP -join ''; $IP = $IP -join '';
$IP = $IP -replace '\s',''; $IP = $IP -replace '\s','';
return @{'value' = $IP; 'name' = 'IPv6'} return @{
'value' = $IP;
'name' = 'IPv6'
}
} }

View file

@ -28,14 +28,14 @@ function Expand-IcingaIPv6String()
[String]$IP [String]$IP
); );
$Counter = 0 $Counter = 0;
$RelV = -1 $RelV = -1;
for($Index = 0; $Index -lt $IP.Length; $Index++) { for ($Index = 0; $Index -lt $IP.Length; $Index++) {
if ($IP[$Index] -eq ':') { if ($IP[$Index] -eq ':') {
$Counter++ $Counter++;
if (($Index - 1) -ge 0 -and $IP[$Index - 1] -eq ':'){ 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) { 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 = @(); $Result = @();
@ -59,7 +59,7 @@ function Expand-IcingaIPv6String()
[System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.CultureInfo]::InvariantCulture,
[Ref]$Value [Ref]$Value
) | Out-Null; ) | Out-Null;
$Result += ('{0:X4}' -f $Value) $Result += ('{0:X4}' -f $Value);
} }
$Result = $Result -join ':'; $Result = $Result -join ':';

View file

@ -31,6 +31,11 @@ function Get-IcingaNetworkInterface()
[string]$IP [string]$IP
); );
if ([string]::IsNullOrEmpty($IP)) {
Write-Host 'Please specify a valid IP-Address or FQDN';
return $null;
}
try { try {
$IP = ([System.Net.Dns]::GetHostAddresses($IP)).IPAddressToString; $IP = ([System.Net.Dns]::GetHostAddresses($IP)).IPAddressToString;
} catch { } catch {