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

Commit

Permalink
Browse files Browse the repository at this point in the history
…l-Tool into dev
  • Loading branch information
J0seph0 committed Mar 9, 2024
2 parents 1574101 + 3bf4759 commit 099fea5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 46 deletions.
9 changes: 5 additions & 4 deletions ActiveDirectoryQuerier/PowerShell/ActiveDirectoryCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ public static async Task<ObservableCollection<Command>> GetActiveDirectoryComman
ReturnValues commandListTemp = await powerShellExecutor.ExecuteAsync(command);

// NOTE: This is more of an internal error...
// TODO: Provide a more detailed error message??? Maybe log the error to a file???
// TODO: Provide a more detailed error message, possibly to the user and to a log file.
if (commandListTemp.HadErrors)
{
MessageBox.Show(string.Join(" ", commandListTemp.StdErr),
"Warning",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
throw new InvalidPowerShellStateException(
"An error occurred while retrieving the ActiveDirectory commands.");
$"An error occurred while retrieving the ActiveDirectory commands: " +
$"{string.Join(" ", commandListTemp.StdErr)}");
}

foreach (var cmd in commandListTemp.StdOut)
foreach (string cmd in commandListTemp.StdOut)
{
commandList.Add(new Command(cmd));
}
Expand Down
65 changes: 23 additions & 42 deletions ActiveDirectoryQuerier/PowerShell/CommandParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace ActiveDirectoryQuerier.PowerShell;

/// <summary>
/// Manages and provides the parameters available for a given PowerShell command.
/// TODO: Figure out what to do with the Async and non-Async methods! Too much duplicate code..
/// </summary>
public class CommandParameters : ICommandParameters
{
Expand All @@ -16,10 +15,10 @@ public class CommandParameters : ICommandParameters
/// <summary>
/// Gets the collection of possible parameters for a command.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when the collection has not been populated yet.</exception>
/// <note>
/// I realize that this isn't a very clean way of doing things, but it's the best I could come up with at the time.
/// </note>
/// <exception cref="InvalidOperationException">Thrown when the collection has not been populated yet.</exception>
public ObservableCollection<string> PossibleParameters
{
get {
Expand All @@ -32,47 +31,18 @@ public ObservableCollection<string> PossibleParameters
return _possibleParameters;
}
}

/// <summary>
/// Asynchronously loads the possible parameters for the specified command into the '_possibleParameters'
/// collection.
/// </summary>
/// <param name="commandObject">The PowerShell command object whose parameters are to be loaded.</param>
/// <returns>A task representing the asynchronous operation.</returns>

public async Task LoadCommandParametersAsync(Command? commandObject)
{
// commandObject 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 ActiveDirectoryCommandsList in
// MainWindowViewModel.cs, commandObject will be null, causing an exception to be thrown, crashing the program.
if (commandObject is null)
{
Trace.WriteLine("Error: commandObject is null");
_possibleParameters.Add("No valid command provided");
return;
}

if (_possibleParameters.Count == 0)
{
using var powerShell = System.Management.Automation.PowerShell.Create();
string commandString =
$"Get-Command {commandObject.CommandText} | Select -ExpandProperty Parameters | ForEach-Object {{ $_.Keys }}";

powerShell.Commands.Clear();
powerShell.AddScript(commandString);
PSDataCollection<PSObject> result = await powerShell.InvokeAsync();

foreach (PSObject cmd in result)
{
_possibleParameters.Add($"-{cmd}");
}
}
await LoadCommandParametersInternal(commandObject, true);
}

/// <summary>
/// Loads the possible parameters for the specified command into the '_possibleParameters' collection.
/// </summary>
/// <param name="commandObject">The PowerShell command object whose parameters are to be loaded.</param>
void ICommandParameters.LoadCommandParameters(Command? commandObject)
{
LoadCommandParametersInternal(commandObject, false).Wait();
}

private async Task LoadCommandParametersInternal(Command? commandObject, bool isAsync)
{
// commandObject 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 ActiveDirectoryCommandsList in
Expand All @@ -88,15 +58,26 @@ void ICommandParameters.LoadCommandParameters(Command? commandObject)
{
using var powerShell = System.Management.Automation.PowerShell.Create();
string commandString =
$"Get-Command {commandObject.CommandText} | Select -ExpandProperty Parameters | ForEach-Object {{$_.Keys }}";
$"Get-Command {commandObject.CommandText} | Select -ExpandProperty Parameters | ForEach-Object {{ $_.Keys }}";

powerShell.Commands.Clear();
powerShell.AddScript(commandString);
Collection<PSObject> result = powerShell.Invoke();

foreach (PSObject cmd in result)
if (isAsync)
{
PSDataCollection<PSObject> result = await powerShell.InvokeAsync();
foreach (PSObject cmd in result)
{
_possibleParameters.Add($"-{cmd}");
}
}
else
{
_possibleParameters.Add($"-{cmd}");
Collection<PSObject> result = powerShell.Invoke();
foreach (PSObject cmd in result)
{
_possibleParameters.Add($"-{cmd}");
}
}
}
}
Expand Down

0 comments on commit 099fea5

Please sign in to comment.