【问题标题】:Powershell script not executing through Asp.net web pagePowershell 脚本未通过 Asp.net 网页执行
【发布时间】:2019-01-24 10:27:49
【问题描述】:

当我通过命令窗口运行时,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();

【问题讨论】:

    标签: c# asp.net powershell


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-10-11
      • 1970-01-01
      • 2021-06-18
      • 2012-09-18
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多