【问题标题】:How to call Powershell script using web API (cs or asp.net)如何使用 Web API(cs 或 asp.net)调用 Powershell 脚本
【发布时间】:2019-06-28 15:21:59
【问题描述】:

以下是使服务器保持在 SCOM 维护模式的函数,我想通过 cs 或 asp.net 通过传递变量来调用此函数作为 API 调用。

function set-scomderegister {    
    param(
        [Parameter( Mandatory = $True,  ValueFromPipeline = $true)][string]
        $SCOMServer,
        [Parameter( Mandatory = $True,  ValueFromPipeline = $true)]
        $Computername
    )

    ForEach($Comp in $Computername)
    {
      New-SCManagementGroupConnection -ComputerName $SCOMServer
      $numberOfMin = 100
      $ReasonComment = "Server got docomissioned "
      $Instance = Get-SCOMClassInstance -Name $Comp 
      $Time = ((Get-Date).AddMinutes($numberOfMin))
      Start-SCOMMaintenanceMode -Instance $Instance -EndTime $Time -Comment $ReasonComment -Reason PlannedOther;    
    }
}

【问题讨论】:

标签: c# asp.net powershell


【解决方案1】:

System.Management.Automation 命名空间对您很有用。

您可以安装一个 nuget 包“System.Management.Automation”。 安装后,您将拥有此命名空间。

您可以调用带有参数的脚本,如下所示:

public void RunWithParameters()
{
    // create empty pipeline
    PowerShell ps = PowerShell.Create();

    // add command
    ps.AddCommand("test-path").AddParameter("Path", Environment.CurrentDirectory); ;

    var obj = ps.Invoke();
}

private string RunScript(string scriptText)
{
    // create Powershell runspace

    Runspace runspace = RunspaceFactory.CreateRunspace();

    // open it

    runspace.Open();

    // create a pipeline and feed it the script text

    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript(scriptText);

    // add an extra command to transform the script
    // output objects into nicely formatted strings

    // remove this line to get the actual objects
    // that the script returns. For example, the script

    // "Get-Process" returns a collection
    // of System.Diagnostics.Process instances.

    pipeline.Commands.Add("Out-String");

    // execute the script

    Collection<psobject /> results = pipeline.Invoke();

    // close the runspace

    runspace.Close();

    // convert the script result into a single string

    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)
    {
        stringBuilder.AppendLine(obj.ToString());
    }

    return stringBuilder.ToString();
}

还有另一个选项可以使用 Process.Start 来启动 powershell 提示符。然后将文件路径传递给进程。

public static int RunPowershellScript(string ps)
{
    int errorLevel;
    ProcessStartInfo processInfo;
    Process process;

    processInfo = new ProcessStartInfo("powershell.exe", "-File " + ps);
    processInfo.CreateNoWindow = true;
    processInfo.UseShellExecute = false;

    process = Process.Start(processInfo);
    process.WaitForExit();

    errorLevel = process.ExitCode;
    process.Close();

    return errorLevel;
}

希望这会有所帮助。

【讨论】:

  • System.Management.Automation 命名空间看起来是个不错的方法,如果您有任何示例,请您举个例子,并且我想将属性传递给 powershell 脚本。
  • 更多详情请参考此 MSDN 链接:docs.microsoft.com/en-us/dotnet/api/…
  • 我还更新了问题中的示例。我已经在我的机器上执行了这个并且它有效!希望这对你有用。
  • 非常感谢您的回复,其实我还有一个问题。我正在开发 web api,下面是我的 post 方法代码,C# 脚本在 MaintennceModeService 类中,但是如何通过传递 post 中提供的参数来调用该服务。公共字符串 Post([FromBody]MaintenanceMode 值) { 字符串 mgmtserver = value.mgmtserver;字符串节点名称 = 值。节点名称; } 返回维护模式服务(值);
  • @Jack - 我建议将此标记为答案(如果这篇文章真的帮助您解决了问题)。然后请添加另一个问题,因为从 cmets 读取代码非常困难。我很乐意提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-12
  • 2012-03-07
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
相关资源
最近更新 更多