private void btnInvokeScriptFile_Click(object sender, RoutedEventArgs e) { // Create OpenFileDialog Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog(); // Set filter for file extension and default file extension dialog.DefaultExt = ".txt"; dialog.Filter = "PowerShell Script (.ps1)|*.ps1"; // Display OpenFileDialog by calling ShowDialog method bool?dialogResult = dialog.ShowDialog(); // Get the selected file name and display in a TextBox if (dialogResult == true) { // Open document string filename = dialog.FileName; var results = ps.InvokeScriptFileSynchronous(filename); foreach (var result in results) { var item = new PowerShellItem(DataType.Data, result.ToString()); lstOutput.Items.Add(item); } } }
async private void btnParallelStartScript_Click(object sender, RoutedEventArgs e) { Duration duration = new Duration(TimeSpan.FromMilliseconds(100)); DoubleAnimation doubleanimation = new DoubleAnimation(0, duration); pgbStatus.BeginAnimation(System.Windows.Controls.Primitives.RangeBase.ValueProperty, doubleanimation); btnCancal.IsEnabled = true; tabMain.IsEnabled = false; cts = new CancellationTokenSource(); ((System.Windows.Controls.Button)sender).IsEnabled = false; var sb = new StringBuilder(); sb.AppendLine(string.Format("1..{0} | ForEach-Object {{", txtParallelNumberOfIterations.Text)); sb.Append(txtParallelCmd.Text); sb.AppendLine("}"); var task = ps.Invoke(sb.ToString(), UpdateError, UpdateWarning, UpdateVerbose, UpdateDebug, UpdateProgress); try { await task.HandleCancellation(cts.Token, ps.Stop); var results = task.Result; foreach (var result in results) { var item = new PowerShellItem(DataType.Data, result.ToString()); lstOutput.Items.Add(item); } } catch (OperationCanceledException) { lstOutput.Items.Add(new PowerShellItem(DataType.Error, "Processing cancelled by user request")); duration = new Duration(TimeSpan.FromMilliseconds(100)); doubleanimation = new DoubleAnimation(0, duration); pgbStatus.BeginAnimation(ProgressBar.ValueProperty, doubleanimation); return; } catch (Exception ex) { lstOutput.Items.Add(new PowerShellItem(DataType.Error, string.Format("Error executing the script: '{0}'", ex.Message))); return; } finally { ((System.Windows.Controls.Button)sender).IsEnabled = true; btnCancal.IsEnabled = false; tabMain.IsEnabled = true; } }
private void btnSerialStartScript_Click(object sender, RoutedEventArgs e) { Duration duration = new Duration(TimeSpan.FromMilliseconds(100)); DoubleAnimation doubleanimation = new DoubleAnimation(0, duration); pgbStatus.BeginAnimation(ProgressBar.ValueProperty, doubleanimation); btnCancal.IsEnabled = true; tabMain.IsEnabled = false; cts = new CancellationTokenSource(); ((Button)sender).IsEnabled = false; var sb = new StringBuilder(); sb.AppendLine(string.Format("1..{0} | ForEach-Object {{", txtSerialNumberOfIterations.Text)); sb.Append(txtSerialCmd.Text); sb.AppendLine("}"); try { var results = ps.InvokeSynchronous(sb.ToString()); foreach (var result in results) { var item = new PowerShellItem(DataType.Data, result.ToString()); lstOutput.Items.Add(item); } } catch (Exception ex) { lstOutput.Items.Add(new PowerShellItem(DataType.Error, string.Format("Error executing the script: '{0}'", ex.Message))); return; } finally { ((System.Windows.Controls.Button)sender).IsEnabled = true; btnCancal.IsEnabled = false; tabMain.IsEnabled = true; } }
void UpdateDebug(object sender, DataAddedEventArgs e) { var item = new PowerShellItem(DataType.Debug, ((PSDataCollection <DebugRecord>)sender)[e.Index].ToString()); lstOutput.Dispatcher.Invoke(new Action(() => lstOutput.Items.Add(item))); }