繁体   English   中英

Powershell脚本无法通过Asp.net网页执行

[英]Powershell script not executing through Asp.net web page

当我通过命令窗口运行时,Powershell脚本工作得很好。 但是通过asp.net网页却没有。 任何人都可以给出故障排除的提示...运行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();

AddScript需要代码。 您可以通过以下方法来解决此问题:将脚本读入内存,将其传递,然后使用AddParameter传递-FilePath 您没有对运行空间做任何花哨的操作,因此我减少了代码的可读性:

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM