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

View file

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

View file

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