dmuun
dmuun

Reputation: 1228

ASP.NET Process Start PowerShell Script IIS 7.5

All works fine on my dev machine but if deployed to IIS the process doesn't get started. I am starting a powershell script by

    private void RunScript()
    {
        Process process = null;
        try
        {
            int timeout = 1800000;
            var startInfo = new ProcessStartInfo
            {
                FileName = @"powershell.exe",    
                Arguments = string.Format("{0} {1}", "\path\toscript", "myParam"),
                UseShellExecute = false,
                CreateNoWindow = true
            };                   

            process = Process.Start(startInfo);
            process.WaitForExit(timeout);
        }
        finally
        {
            if (!process.HasExited)
            {
                if (process.Responding)
                    process.CloseMainWindow();
                else
                    process.Kill();
            }
            if (process != null)
            {
                process.Close();
                process.Dispose();
            }
        }  
    }

Here's what's configured for the app pool this is running under.

Process Model
->Identity = domain user who is a Domain Admin.
->Load User Profile = True

Web App
Authentication is Windows

What else do I need to configure to so that I can run the Process?

Upvotes: 3

Views: 1690

Answers (3)

Start-Automating
Start-Automating

Reputation: 8367

It's much better to embed the PowerShell APIs the call the .exe

Here's an old link that will get you a PowerShell runspace embedded in ASP.NET per user:

http://powershellpipeworks.com/

Upvotes: 1

dmuun
dmuun

Reputation: 1228

As Start-Automating suggested I eventually ended up doing this:

            using (Runspace runSpace = RunspaceFactory.CreateRunspace())
        {
            try
            {
                runSpace.Open();
                RunspaceInvoke scriptInvoker = new RunspaceInvoke(runSpace);
                scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); 
                using (Pipeline pipeLine = runSpace.CreatePipeline())
                {                        
                    var myCommand = new Command(scriptPath);                           
                    var myParam1 = new CommandParameter("-paramName", "someValue"); 
                    myCommand.Parameters.Add(myParam1);
                    pipeLine.Commands.Add(myCommand);       
                    pipeLine.Commands.Add("Out-String");
                    Collection<PSObject> returnObjects = pipeLine.Invoke();
                    runSpace.Close();

                    return returnObjects;                                              
                }
            }
            finally 
            {
                runSpace.Close();
            }
        }

On the IIS server I executed the following powershell command "Set-ExecutionPolicy RemoteSigned"

Upvotes: 3

rick schott
rick schott

Reputation: 21137

Check the permissions of the file system where powershell.exe lives.

Also, check the Security Log in the Event Viewer for authentication errors and access violations.

Upvotes: 0

Related Questions