public async Task <Runspace> CreateRunspaceAsync() { return(await Task.Run( () => { var wSManConnectionInfo = PowerShellSupport.CreateConnectionInfo(_computer); Runspace runspace = null; try { runspace = RunspaceFactory.CreateRunspace(wSManConnectionInfo); runspace.Open(); return runspace; } catch { runspace?.Dispose(); throw; } })); }
private async Task RunPowershell(RemoteComputer powershellComputer) { var connectionInfo = PowerShellSupport.CreateConnectionInfo(powershellComputer); await Task.Run( () => { try { powershellComputer.JobStatus = PowerShellJobStatus.Connecting; cancellationTokenSource.Token.ThrowIfCancellationRequested(); using (var runspace = RunspaceFactory.CreateRunspace(connectionInfo)) { cancellationTokenSource.Token.ThrowIfCancellationRequested(); using (cancellationTokenSource.Token.Register( () => runspace.Close())) { runspace.Open(); } cancellationTokenSource.Token.ThrowIfCancellationRequested(); using (var powershell = PowerShell.Create()) { cancellationTokenSource.Token.ThrowIfCancellationRequested(); powershell.Runspace = runspace; foreach (var script in FileNames) { powershell.AddScript(File.ReadAllText(script)); } var input = new PSDataCollection <PSObject>(); var output = new PSDataCollection <PSObject>(); powershell.Streams.Error.DataAdded += (s, ev) => ErrorOnDataAdded(s, ev, powershellComputer); powershell.Streams.Debug.DataAdded += (s, ev) => DebugOnDataAdded(s, ev, powershellComputer); powershell.Streams.Progress.DataAdded += (s, ev) => ProgressOnDataAdded(s, ev, powershellComputer); powershell.Streams.Verbose.DataAdded += (s, ev) => VerboseOnDataAdded(s, ev, powershellComputer); powershell.Streams.Warning.DataAdded += (s, ev) => WarningOnDataAdded(s, ev, powershellComputer); powershellComputer.JobStatus = PowerShellJobStatus.Invoking; cancellationTokenSource.Token.ThrowIfCancellationRequested(); using (cancellationTokenSource.Token.Register( () => { powershell.InvocationStateChanged += (sender, args) => { switch (args.InvocationStateInfo.State) { case PSInvocationState.Failed: powershellComputer.JobStatus = PowerShellJobStatus.Failed; break; case PSInvocationState.Completed: powershellComputer.JobStatus = PowerShellJobStatus.Completed; break; case PSInvocationState.Stopping: powershellComputer.JobStatus = PowerShellJobStatus.Cancelling; break; case PSInvocationState.Stopped: powershellComputer.JobStatus = PowerShellJobStatus.Cancelled; break; } }; powershell.Stop(); })) { powershell.Invoke(input, output); } cancellationTokenSource.Token.ThrowIfCancellationRequested(); powershellComputer.JobStatus = PowerShellJobStatus.Completed; } } } catch (RemoteException e) { if (cancellationTokenSource.IsCancellationRequested) { powershellComputer.JobStatus = PowerShellJobStatus.Cancelled; return; } throw; } catch (PSRemotingDataStructureException ex) { if (ex?.ErrorRecord?.Exception?.Message == "The pipeline has been stopped.") { powershellComputer.JobStatus = PowerShellJobStatus.Cancelled; } } catch (PSRemotingTransportException ex) { if (ex.ErrorRecord != null) { WriteErrorRecord(powershellComputer, ex.ErrorRecord); } powershellComputer.JobStatus = PowerShellJobStatus.CouldntConnect; } catch (OperationCanceledException ex) { switch (powershellComputer.JobStatus) { case PowerShellJobStatus.Waiting: case PowerShellJobStatus.Invoking: case PowerShellJobStatus.Connecting: case PowerShellJobStatus.Cancelling: powershellComputer.JobStatus = PowerShellJobStatus.Cancelled; break; } } catch (Exception ex) { powershellComputer.JobStatus = PowerShellJobStatus.Failed; MessageBox.Show( "An internal error has occurred!\n" + ex, "Internal error", MessageBoxButton.OK, MessageBoxImage.Error); } }); }