User Tools

Site Tools


powershell_runner

PowerShell Runner

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
 
namespace PowerShellRunner
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args == null || args.Length == 0)
            {
                MessageBox.Show(
                      "No PowerShell script file was specified."
                    , "PowerShellRunner - Argument Error"
                    , MessageBoxButtons.OK
                    , MessageBoxIcon.Error
                );
 
                return;
            }
 
            if (!File.Exists(args[0]))
            {
                MessageBox.Show(
                      string.Format("The file '{0}' does not exist or can not be accessed.", args[0])
                    , "PowerShellRunner - Argument Error"
                    , MessageBoxButtons.OK
                    , MessageBoxIcon.Error
                );
 
                return;
            }
 
            var processStartInfo = new ProcessStartInfo
            {
                  FileName = "powershell"
                , Arguments = "-file " + string.Join(" ", args.Select(a => "\"" + a + "\""))
                , WindowStyle = ProcessWindowStyle.Hidden
            };
 
            var process = Process.Start(processStartInfo);
 
            process.WaitForExit();
 
            if (process.ExitCode != 0)
            {
                MessageBox.Show(
                    string.Format("PowerShell script returned an error code ({0}).", process.ExitCode)
                    , "PowerShellRunner - Script Error"
                    , MessageBoxButtons.OK
                    , MessageBoxIcon.Error
                );
            }
        }
    }
}
powershell_runner.txt · Last modified: 2017/01/01 20:05 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki