icinga-powershell-framework/lib/core/installer/tools/Write-IcingaManagementConsoleCommand.psm1

64 lines
2.3 KiB
PowerShell
Raw Normal View History

function Write-IcingaManagementConsoleCommand()
{
param (
$Entry = $null,
$Values = @()
);
2021-09-02 07:44:17 -04:00
if ($null -eq $Entry -Or $Entry.ContainsKey('Action') -eq $FALSE) {
return '';
}
2021-09-02 07:44:17 -04:00
[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';
}
}
2021-09-02 07:44:17 -04:00
} elseif ($PrintValue.GetType().Name -eq 'String') {
$PrintValue = (ConvertFrom-IcingaArrayToString -Array $PrintValue -AddQuotes -UseSingleQuotes);
}
if ([string]::IsNullOrEmpty($PrintValue)) {
$PrintArguments += ([string]::Format('{0} ', $cmdArg));
} else {
2021-09-02 07:44:17 -04:00
$PrintArguments += ([string]::Format('{0} {1} ', $cmdArg, $PrintValue));
}
2021-09-02 07:44:17 -04:00
}
2021-09-02 07:44:17 -04:00
$PrintArguments = $PrintArguments.Replace('$DefaultValues$', ((ConvertFrom-IcingaArrayToString -Array $Values -AddQuotes)));
2021-09-02 07:44:17 -04:00
while ($PrintArguments[-1] -eq ' ') {
$PrintArguments = $PrintArguments.SubString(0, $PrintArguments.Length - 1);
}
2021-08-06 12:12:27 -04:00
2021-09-02 07:44:17 -04:00
if ([string]::IsNullOrEmpty($PrintArguments) -eq $FALSE) {
$PrintArguments = [string]::Format(' {0}', $PrintArguments);
}
2021-09-02 07:44:17 -04:00
Write-IcingaConsolePlain ([string]::Format('PS> {0}{1};', $PrintCommand, $PrintArguments)) -ForeColor Magenta;
Write-IcingaConsolePlain '';
}