From 16b82df3371047f716634447125d2c44ca86b8d0 Mon Sep 17 00:00:00 2001 From: Hunter T Date: Thu, 28 Mar 2024 15:58:19 -0700 Subject: [PATCH] Refactor to remove redundant code --- .../ActiveDirectory/ADCommandParameters.cs | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/ActiveDirectoryQuerier/ActiveDirectory/ADCommandParameters.cs b/ActiveDirectoryQuerier/ActiveDirectory/ADCommandParameters.cs index 8c6b035..fc77fc4 100644 --- a/ActiveDirectoryQuerier/ActiveDirectory/ADCommandParameters.cs +++ b/ActiveDirectoryQuerier/ActiveDirectory/ADCommandParameters.cs @@ -40,10 +40,9 @@ public void LoadAvailableParameters(Command? psCommand) private async Task LoadAvailableParametersCore(Command? psCommand, bool isAsync) { - // psCommand can be null if the user attempts to select an ActiveDirectory command that doesn't exist. - // More specifically, if the entered command doesn't exist in the ADCommands property defined in - // MainWindowViewModel.cs, psCommand will be null, causing an exception to be thrown, crashing the - // program. + // psCommand may be null if the user attempts to select an Active Directory command that doesn't exist. More + // specifically, if the entered command doesn't exist in the ADCommands property defined in + // MainWindowViewModel.cs, psCommand will be null, causing an exception to be thrown, crashing the program. if (psCommand is null) { Trace.WriteLine("Error: command is null"); @@ -53,28 +52,26 @@ private async Task LoadAvailableParametersCore(Command? psCommand, bool isAsync) if (_availableParameters.Count == 0) { + ICollection result; using var powerShell = System.Management.Automation.PowerShell.Create(); - string commandString = - $"Get-Command {psCommand.CommandText} | Select -ExpandProperty Parameters | ForEach-Object {{ $_.Keys }}"; + var commandString = $"Get-Command {psCommand.CommandText} | Select -ExpandProperty Parameters | " + + $"ForEach-Object {{ $_.Keys }}"; powerShell.Commands.Clear(); powerShell.AddScript(commandString); if (isAsync) { - PSDataCollection result = await powerShell.InvokeAsync(); - foreach (PSObject adCommandParameter in result) - { - _availableParameters.Add($"-{adCommandParameter}"); - } + result = await powerShell.InvokeAsync(); } else { - Collection result = powerShell.Invoke(); - foreach (PSObject adCommandParameter in result) - { - _availableParameters.Add($"-{adCommandParameter}"); - } + result = powerShell.Invoke(); + } + + foreach (PSObject adCommandParameter in result) + { + _availableParameters.Add($"-{adCommandParameter}"); } } }