public async Task ExecuteAsync() { var results = await _powerShellContext.ExecuteCommandAsync <TResult>( _psCommand, _errorMessages, _executionOptions); var unusedTask = Task.Run(() => _resultsTask.SetResult(results)); }
public async Task CanRegisterAndInvokeCommandWithCmdletName() { await extensionService.PowerShellContext.ExecuteScriptStringAsync( TestUtilities.NormalizeNewlines("function Invoke-Extension { $global:extensionValue = 5 }\n") + "Register-EditorCommand -Name \"test.function\" -DisplayName \"Function extension\" -Function \"Invoke-Extension\""); // Wait for the add event EditorCommand command = await this.AssertExtensionEvent(EventType.Add, "test.function"); // Invoke the command await extensionService.InvokeCommandAsync("test.function", this.commandContext); // Assert the expected value PSCommand psCommand = new PSCommand(); psCommand.AddScript("$global:extensionValue"); var results = await powerShellContext.ExecuteCommandAsync <int>(psCommand); Assert.Equal(5, results.FirstOrDefault()); }
public async Task <string> InvokeReadLineAsync(bool isCommandLine, CancellationToken cancellationToken) { _readLineCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); var localTokenSource = _readLineCancellationSource; if (localTokenSource.Token.IsCancellationRequested) { throw new TaskCanceledException(); } try { if (!isCommandLine) { return(await _consoleReadLine.InvokeLegacyReadLineAsync( false, _readLineCancellationSource.Token)); } var result = (await _powerShellContext.ExecuteCommandAsync <string>( new PSCommand() .AddScript(ReadLineScript) .AddArgument(_readLineCancellationSource.Token), null, new ExecutionOptions() { WriteErrorsToHost = false, WriteOutputToHost = false, InterruptCommandPrompt = false, AddToHistory = false, IsReadLine = isCommandLine })) .FirstOrDefault(); return(cancellationToken.IsCancellationRequested ? string.Empty : result); } finally { _readLineCancellationSource = null; } }