Merge pull request #356 from Icinga:feature/imc_improvements

Feature: Adds various IMC improvements
This commit is contained in:
Lord Hepipud 2021-09-02 13:44:51 +02:00 committed by GitHub
commit 387d46cc7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 65 deletions

View file

@ -86,8 +86,7 @@ function Show-IcingaForWindowsManagementConsoleManageFramework()
'Disabled' = ($null -eq $IcingaService -Or $ServiceStatus -ne 'Running' -Or (-Not $AdminShell));
'DisabledReason' = 'The service is either not installed, already stopped or you are not inside an administrative shell';
'Action' = @{
'Command' = 'Stop-Service';
'Arguments' = @{ '-Name' = 'icingapowershell'; };
'Command' = 'Stop-IcingaWindowsService';
}
},
@{
@ -97,8 +96,7 @@ function Show-IcingaForWindowsManagementConsoleManageFramework()
'Disabled' = ($null -eq $IcingaService -Or (-Not $AdminShell));
'DisabledReason' = 'The service is either not installed or you are not inside an administrative shell';
'Action' = @{
'Command' = 'Restart-Service';
'Arguments' = @{ '-Name' = 'icingapowershell'; };
'Command' = 'Restart-IcingaWindowsService';
}
}
);

View file

@ -5,17 +5,28 @@ function Write-IcingaManagementConsoleCommand()
$Values = @()
);
if ($null -eq $Entry) {
return;
if ($null -eq $Entry -Or $Entry.ContainsKey('Action') -eq $FALSE) {
return '';
}
if ($Entry.Action -And ($Entry.Action.ContainsKey('Command') -Or ($Entry.Action.ContainsKey('Arguments') -And $Entry.Action.Arguments.ContainsKey('-Command')))) {
$PrintArguments = '';
$PrintCommand = ''
if ($null -ne $Entry.Action.Arguments -And $Entry.Action.Arguments.ContainsKey('-CmdArguments')) {
[string]$PrintArguments = '';
[string]$PrintCommand = '';
[hashtable]$DefinedArgs = @{ };
if ($entry.Action.ContainsKey('Arguments') -And $entry.Action.Arguments.ContainsKey('-Command')) {
$PrintCommand = $Entry.Action.Arguments['-Command'];
foreach ($cmdArg in $Entry.Action.Arguments['-CmdArguments'].Keys) {
$PrintValue = $Entry.Action.Arguments['-CmdArguments'][$cmdArg];
if ($Entry.Action.Arguments.ContainsKey('-CmdArguments')) {
$DefinedArgs = $Entry.Action.Arguments['-CmdArguments'];
}
} elseif ($entry.Action.ContainsKey('Command')) {
$PrintCommand = $entry.Action.Command;
if ($Entry.Action.ContainsKey('Arguments')) {
$DefinedArgs = $Entry.Action.Arguments;
}
}
foreach ($cmdArg in $DefinedArgs.Keys) {
$PrintValue = $DefinedArgs[$cmdArg];
[string]$StringArg = ([string]$cmdArg).Replace('-', '');
if ($PrintValue.GetType().Name -eq 'Boolean') {
if ((Get-Command $PrintCommand).Parameters.$StringArg.ParameterType.Name -eq 'SwitchParameter') {
@ -27,33 +38,14 @@ function Write-IcingaManagementConsoleCommand()
$PrintValue = '$FALSE';
}
}
} elseif ($PrintValue.GetType().Name -eq 'String' -And $PrintValue.Contains(' ')) {
$PrintValue = (ConvertFrom-IcingaArrayToString -Array $PrintValue -AddQuotes);
} elseif ($PrintValue.GetType().Name -eq 'String') {
$PrintValue = (ConvertFrom-IcingaArrayToString -Array $PrintValue -AddQuotes -UseSingleQuotes);
}
if ([string]::IsNullOrEmpty($PrintValue)) {
$PrintArguments += ([string]::Format('{0} ', $cmdArg));
} else {
$PrintArguments += ([string]::Format('{0} {1} ', $cmdArg, $PrintValue));
}
} else {
$PrintCommand = $Entry.Action.Command;
if ($null -ne $Entry.Action.Arguments) {
foreach ($cmdArg in $Entry.Action.Arguments.Keys) {
$PrintValue = $Entry.Action.Arguments[$cmdArg];
[string]$StringArg = ([string]$cmdArg).Replace('-', '');
if ($PrintValue.GetType().Name -eq 'Boolean') {
if ((Get-Command $PrintCommand).Parameters.$StringArg.ParameterType.Name -eq 'SwitchParameter') {
$PrintValue = '';
} else {
if ($PrintValue) {
$PrintValue = '$TRUE';
} else {
$PrintValue = '$FALSE';
}
}
} elseif ($PrintValue.GetType().Name -eq 'String' -And $PrintValue.Contains(' ')) {
$PrintValue = (ConvertFrom-IcingaArrayToString -Array $PrintValue -AddQuotes);
}
$PrintArguments += ([string]::Format('{0} {1} ', $cmdArg, $PrintValue));
}
}
}
$PrintArguments = $PrintArguments.Replace('$DefaultValues$', ((ConvertFrom-IcingaArrayToString -Array $Values -AddQuotes)));
@ -68,5 +60,4 @@ function Write-IcingaManagementConsoleCommand()
Write-IcingaConsolePlain ([string]::Format('PS> {0}{1};', $PrintCommand, $PrintArguments)) -ForeColor Magenta;
Write-IcingaConsolePlain '';
}
}

View file

@ -3,11 +3,16 @@ function ConvertFrom-IcingaArrayToString()
param (
[array]$Array = @(),
[switch]$AddQuotes = $FALSE,
[switch]$UseSingleQuotes = $FALSE,
[switch]$SecureContent = $FALSE
);
if ($null -eq $Array -Or $Array.Count -eq 0) {
if ($AddQuotes) {
if ($UseSingleQuotes) {
return "''";
}
return '""';
}
@ -21,8 +26,12 @@ function ConvertFrom-IcingaArrayToString()
if ($SecureContent) {
$entry = '***';
}
if ($UseSingleQuotes) {
$NewArray += ([string]::Format("'{0}'", $entry));
} else {
$NewArray += ([string]::Format('"{0}"', $entry));
}
}
} else {
if ($SecureContent) {
foreach ($entry in $Array) {