3

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?

3 Answers 3

3

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"

Sign up to request clarification or add additional context in comments.

Comments

1

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/

Comments

0

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.

1 Comment

The app pool domain account being used is also member of the Administrators group on the machine. So on C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe the Administrators group has Read, Read & Execute permissions. Is that sufficient rights?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.