1

Powershell script works perfectly fine, when i run through command windows. But through asp.net web page it doesn't. Can anyone give pointers for troubleshooting ... running windows 10

using System.Management.Automation;
using System.Management.Automation.Runspaces;

            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript("powershell.exe -ExecutionPolicy Unrestricted -file D:\\Files\\Scripts\\Script20180817.ps1 -FilePath " + file);
            pipeline.Invoke();
            runspace.Close();
0

1 Answer 1

0

AddScript expects code. You can remedy this by reading the script into memory, passing it, then using AddParameter to pass the -FilePath. You're not doing anything fancy with the runspaces, so I reduced the code for readability:

using System.IO;
using System.Management.Automation;

string scriptPath = @"D:\Files\Scripts\Script20180817.ps1";
string file = "TBD?";

string script = File.ReadAllText(scriptPath);
using (PowerShell ps = PowerShell.Create())
{
    ps.AddScript(script).AddParameter('FilePath', file);
    ps.Invoke();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.