Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
Refactor to remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
StrangeRanger committed Mar 28, 2024
1 parent 0651e0a commit 16b82df
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions ActiveDirectoryQuerier/ActiveDirectory/ADCommandParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -53,28 +52,26 @@ private async Task LoadAvailableParametersCore(Command? psCommand, bool isAsync)

if (_availableParameters.Count == 0)
{
ICollection<PSObject> 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<PSObject> result = await powerShell.InvokeAsync();
foreach (PSObject adCommandParameter in result)
{
_availableParameters.Add($"-{adCommandParameter}");
}
result = await powerShell.InvokeAsync();
}
else
{
Collection<PSObject> result = powerShell.Invoke();
foreach (PSObject adCommandParameter in result)
{
_availableParameters.Add($"-{adCommandParameter}");
}
result = powerShell.Invoke();
}

foreach (PSObject adCommandParameter in result)
{
_availableParameters.Add($"-{adCommandParameter}");
}
}
}
Expand Down

0 comments on commit 16b82df

Please sign in to comment.