mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-20 23:00:35 -05:00
Added tool function to split IP and Port from strings
This commit is contained in:
parent
64a5054108
commit
3b9f66d679
1 changed files with 37 additions and 0 deletions
37
lib/core/tools/Get-IPConfigFromString.psm1
Normal file
37
lib/core/tools/Get-IPConfigFromString.psm1
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
function Get-IPConfigFromString()
|
||||
{
|
||||
param(
|
||||
[string]$IPConfig
|
||||
);
|
||||
|
||||
if ($IPConfig.Contains(':') -and ($IPConfig.Contains('[') -eq $FALSE -And $IPConfig.Contains(']') -eq $FALSE)) {
|
||||
throw 'Invalid IP-Address format. For IPv6 and/or port configuration, the syntax must be like [ip]:port';
|
||||
}
|
||||
|
||||
if ($IPConfig.Contains('[') -eq $FALSE) {
|
||||
return @{
|
||||
'address' = $IPConfig;
|
||||
'port' = $null
|
||||
};
|
||||
}
|
||||
|
||||
if ($IPConfig.Contains('[') -eq $FALSE -or $IPConfig.Contains(']') -eq $FALSE) {
|
||||
throw 'Invalid IP-Address format. It must match the following [ip]:port';
|
||||
}
|
||||
|
||||
$StartBracket = $IPConfig.IndexOf('[') + 1;
|
||||
$EndBracket = $IPConfig.IndexOf(']') - 1;
|
||||
$PortDelimeter = $IPConfig.LastIndexOf(':') + 1;
|
||||
|
||||
$Port = '';
|
||||
$IP = $IPConfig.Substring($StartBracket, $EndBracket);
|
||||
|
||||
if ($PortDelimeter -ne 0 -And $PortDelimeter -ge $EndBracket) {
|
||||
$Port = $IPConfig.Substring($PortDelimeter, $IPConfig.Length - $PortDelimeter);
|
||||
}
|
||||
|
||||
return @{
|
||||
'address' = $IP;
|
||||
'port' = $Port
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue