/// <summary> /// Run custom PowerShell scripts /// </summary> private async void BtnDoPS_Click(object sender, EventArgs e) { if (LstPS.CheckedItems.Count == 0) { MessageBox.Show(Locale.msgPSSelect, BtnDoPS.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } for (int i = 0; i < LstPS.Items.Count; i++) { if (LstPS.GetItemChecked(i)) { LstPS.SelectedIndex = i; string psdir = @"scripts\" + LstPS.SelectedItem.ToString() + ".ps1"; var ps1File = psdir; var equals = new[] { "Silent" }; var str = TxtPSInfo.Text; BtnDoPS.Text = Locale.statusDoPSProcessing; PnlPS.Enabled = false; // Silent if (equals.Any(str.Contains)) { var startInfo = new ProcessStartInfo() { FileName = "powershell.exe", Arguments = $"-executionpolicy bypass -file \"{ps1File}\"", UseShellExecute = false, CreateNoWindow = true, }; await Task.Run(() => { Process.Start(startInfo).WaitForExit(); }); } else // Create ConsoleWindow { var startInfo = new ProcessStartInfo() { FileName = "powershell.exe", Arguments = $"-executionpolicy bypass -file \"{ps1File}\"", UseShellExecute = false, }; await Task.Run(() => { Process.Start(startInfo).WaitForExit(); }); } BtnDoPS.Text = Locale.statusDoPSApply; PnlPS.Enabled = true; // Done! MessageBox.Show("Script " + "\"" + LstPS.Text + "\" " + Locale.msgPSSuccess, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
/// <summary> /// Run custom PowerShell scripts /// </summary> private void BtnDoPS_Click(object sender, EventArgs e) { if (LstPS.CheckedItems.Count == 0) { MessageBox.Show(_psSelect, BtnDoPS.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } for (int i = 0; i < LstPS.Items.Count; i++) { if (LstPS.GetItemChecked(i)) { LstPS.SelectedIndex = i; BtnDoPS.Text = "Processing"; PnlPS.Enabled = false; //TxtOutputPS.Clear(); using (PowerShell powerShell = PowerShell.Create()) { powerShell.AddScript(TxtConsolePS.Text); powerShell.AddCommand("Out-String"); Collection <PSObject> PSOutput = powerShell.Invoke(); StringBuilder stringBuilder = new StringBuilder(); foreach (PSObject pSObject in PSOutput) { stringBuilder.AppendLine(pSObject.ToString()); } TxtOutputPS.Text = stringBuilder.ToString(); BtnDoPS.Text = "Apply"; PnlPS.Enabled = true; } // Done! MessageBox.Show("Script " + "\"" + LstPS.Text + "\" " + _psSuccess, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } }