Adds various IMC improvements

This commit is contained in:
Lord Hepipud 2021-09-02 13:44:17 +02:00
parent 0ee4bc52e7
commit c8afe4ed4c
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,17 +5,28 @@ 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')) {
if ($entry.Action.ContainsKey('Arguments') -And $entry.Action.Arguments.ContainsKey('-Command')) {
$PrintCommand = $Entry.Action.Arguments['-Command']; $PrintCommand = $Entry.Action.Arguments['-Command'];
foreach ($cmdArg in $Entry.Action.Arguments['-CmdArguments'].Keys) { if ($Entry.Action.Arguments.ContainsKey('-CmdArguments')) {
$PrintValue = $Entry.Action.Arguments['-CmdArguments'][$cmdArg]; $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('-', ''); [string]$StringArg = ([string]$cmdArg).Replace('-', '');
if ($PrintValue.GetType().Name -eq 'Boolean') { if ($PrintValue.GetType().Name -eq 'Boolean') {
if ((Get-Command $PrintCommand).Parameters.$StringArg.ParameterType.Name -eq 'SwitchParameter') { if ((Get-Command $PrintCommand).Parameters.$StringArg.ParameterType.Name -eq 'SwitchParameter') {
@ -27,33 +38,14 @@ function Write-IcingaManagementConsoleCommand()
$PrintValue = '$FALSE'; $PrintValue = '$FALSE';
} }
} }
} elseif ($PrintValue.GetType().Name -eq 'String' -And $PrintValue.Contains(' ')) { } elseif ($PrintValue.GetType().Name -eq 'String') {
$PrintValue = (ConvertFrom-IcingaArrayToString -Array $PrintValue -AddQuotes); $PrintValue = (ConvertFrom-IcingaArrayToString -Array $PrintValue -AddQuotes -UseSingleQuotes);
} }
if ([string]::IsNullOrEmpty($PrintValue)) {
$PrintArguments += ([string]::Format('{0} ', $cmdArg));
} else {
$PrintArguments += ([string]::Format('{0} {1} ', $cmdArg, $PrintValue)); $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))); $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 ([string]::Format('PS> {0}{1};', $PrintCommand, $PrintArguments)) -ForeColor Magenta;
Write-IcingaConsolePlain ''; Write-IcingaConsolePlain '';
}
} }

View file

@ -3,11 +3,16 @@ function ConvertFrom-IcingaArrayToString()
param ( param (
[array]$Array = @(), [array]$Array = @(),
[switch]$AddQuotes = $FALSE, [switch]$AddQuotes = $FALSE,
[switch]$UseSingleQuotes = $FALSE,
[switch]$SecureContent = $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,8 +26,12 @@ function ConvertFrom-IcingaArrayToString()
if ($SecureContent) { if ($SecureContent) {
$entry = '***'; $entry = '***';
} }
if ($UseSingleQuotes) {
$NewArray += ([string]::Format("'{0}'", $entry));
} else {
$NewArray += ([string]::Format('"{0}"', $entry)); $NewArray += ([string]::Format('"{0}"', $entry));
} }
}
} else { } else {
if ($SecureContent) { if ($SecureContent) {
foreach ($entry in $Array) { foreach ($entry in $Array) {