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,68 +5,59 @@ 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')) {
$PrintCommand = $Entry.Action.Arguments['-Command'];
foreach ($cmdArg in $Entry.Action.Arguments['-CmdArguments'].Keys) {
$PrintValue = $Entry.Action.Arguments['-CmdArguments'][$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);
[string]$PrintArguments = '';
[string]$PrintCommand = '';
[hashtable]$DefinedArgs = @{ };
if ($entry.Action.ContainsKey('Arguments') -And $entry.Action.Arguments.ContainsKey('-Command')) {
$PrintCommand = $Entry.Action.Arguments['-Command'];
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') {
$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 {
$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 += ([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()
{
param (
[array]$Array = @(),
[switch]$AddQuotes = $FALSE,
[switch]$SecureContent = $FALSE
[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,7 +26,11 @@ function ConvertFrom-IcingaArrayToString()
if ($SecureContent) {
$entry = '***';
}
$NewArray += ([string]::Format('"{0}"', $entry));
if ($UseSingleQuotes) {
$NewArray += ([string]::Format("'{0}'", $entry));
} else {
$NewArray += ([string]::Format('"{0}"', $entry));
}
}
} else {
if ($SecureContent) {