public void LogProgress(RemoteComputer computer, ProgressRecord progressRecord) { lock (LogEntries) { var id = _progressEntries.GetOrAdd( progressRecord.ActivityId, i => { var progressEntry = new ProgressEntry(computer.Name); progressEntry.Activity = progressRecord.Activity; progressEntry.ActivityId = progressRecord.ActivityId; progressEntry.Status = progressRecord.StatusDescription; progressEntry.CurrentOperation = progressRecord.CurrentOperation; if (progressRecord.PercentComplete < 0) { progressEntry.Visibility = Visibility.Hidden; } else { progressEntry.Visibility = Visibility.Visible; } LogEntries.Add(progressEntry); return(LogEntries.Count - 1); }); ((ProgressEntry)LogEntries[id]).PercentComplete = progressRecord.PercentComplete; } }
private void WriteErrorRecord(RemoteComputer powershellComputer, ErrorRecord record) { powershellComputer.AddLogEntry(new LogEntry(powershellComputer.Name, record.ToString(), LogEntryType.Error)); }
private void WriteVerboseRecord(RemoteComputer powershellComputer, VerboseRecord record) { powershellComputer.AddLogEntry(new LogEntry(powershellComputer.Name, record.Message, LogEntryType.Verbose)); }
private void ProgressOnDataAdded( object sender, DataAddedEventArgs dataAddedEventArgs, RemoteComputer powershellComputer) { var progressRecord = ((PSDataCollection<ProgressRecord>) sender)[dataAddedEventArgs.Index]; powershellComputer.LogProgress(powershellComputer, progressRecord); }
private void VerboseOnDataAdded( object sender, DataAddedEventArgs dataAddedEventArgs, RemoteComputer powershellComputer) { var record = ((PSDataCollection<VerboseRecord>) sender)[dataAddedEventArgs.Index]; WriteVerboseRecord(powershellComputer, record); }
private void WriteWarningRecord(RemoteComputer powershellComputer, WarningRecord record) { powershellComputer.AddLogEntry(new LogEntry(powershellComputer.Name, record.Message, LogEntryType.Warning)); }
private void WarningOnDataAdded( object sender, DataAddedEventArgs dataAddedEventArgs, RemoteComputer powershellComputer) { var record = ((PSDataCollection<WarningRecord>) sender)[dataAddedEventArgs.Index]; powershellComputer.WarningCount++; WriteWarningRecord(powershellComputer, record); }
private void ErrorOnDataAdded( object sender, DataAddedEventArgs dataAddedEventArgs, RemoteComputer powershellComputer) { var record = ((PSDataCollection<ErrorRecord>) sender)[dataAddedEventArgs.Index]; powershellComputer.ErrorCount++; WriteErrorRecord(powershellComputer, record); }
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); } }); }