From c8afe4ed4c3d041a3726f636804b1a384968e8a4 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Thu, 2 Sep 2021 13:44:17 +0200 Subject: [PATCH] Adds various IMC improvements --- .../manage/framework/ManageFramework.psm1 | 6 +- .../Write-IcingaManagementConsoleCommand.psm1 | 105 ++++++++---------- .../ConvertFrom-IcingaArrayToString.psm1 | 17 ++- 3 files changed, 63 insertions(+), 65 deletions(-) diff --git a/lib/core/installer/menu/manage/framework/ManageFramework.psm1 b/lib/core/installer/menu/manage/framework/ManageFramework.psm1 index 0b1d258..19e26ef 100644 --- a/lib/core/installer/menu/manage/framework/ManageFramework.psm1 +++ b/lib/core/installer/menu/manage/framework/ManageFramework.psm1 @@ -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'; } } ); diff --git a/lib/core/installer/tools/Write-IcingaManagementConsoleCommand.psm1 b/lib/core/installer/tools/Write-IcingaManagementConsoleCommand.psm1 index ba4f121..82e9dff 100644 --- a/lib/core/installer/tools/Write-IcingaManagementConsoleCommand.psm1 +++ b/lib/core/installer/tools/Write-IcingaManagementConsoleCommand.psm1 @@ -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 ''; } diff --git a/lib/core/tools/ConvertFrom-IcingaArrayToString.psm1 b/lib/core/tools/ConvertFrom-IcingaArrayToString.psm1 index c8b610e..56195dd 100644 --- a/lib/core/tools/ConvertFrom-IcingaArrayToString.psm1 +++ b/lib/core/tools/ConvertFrom-IcingaArrayToString.psm1 @@ -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) {