C# 如何使用web API(cs或asp.net)调用Powershell脚本
下面是将服务器保持在SCOM维护模式的函数,我想通过cs或asp.net通过传递变量作为API调用来调用此函数C# 如何使用web API(cs或asp.net)调用Powershell脚本,c#,asp.net,powershell,C#,Asp.net,Powershell,下面是将服务器保持在SCOM维护模式的函数,我想通过cs或asp.net通过传递变量作为API调用来调用此函数 function set-scomderegister { param( [Parameter( Mandatory = $True, ValueFromPipeline = $true)][string] $SCOMServer, [Parameter( Mandatory = $True, ValueFromPip
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;
}
}
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();
}
希望这有帮助。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();
}
希望这有帮助。可能有用/您正在寻找的:可能有用/您正在寻找的:System.Management.Automation命名空间看起来是个不错的方法,如果有,请给出任何示例,我也想将属性传递给powershell脚本。有关更多详细信息,请参阅此MSDN链接:我还更新了问题中的示例。我已经在我的机器上执行了这项操作,它工作正常!希望这对你有用。非常感谢你的回复。实际上我还有一个问题。我正在开发web api,下面是我的post方法代码,C脚本在MaintenceModeService类中,但如何通过传递post中提供的参数来调用该服务。公共字符串Post[FromBody]MaintenanceMode值{string mgmtserver=value.mgmtserver;string NodeName=value.NodeName;}返回MaintenceModeServiceValue@杰克-如果这篇文章真的帮助你解决了你的问题,我建议你把它作为答案。然后请添加另一个问题,因为从注释中读取代码非常困难。我很乐意为您提供帮助。System.Management.Automation命名空间看起来是一个不错的方法,如果您有,请给出任何示例,并且我希望将属性传递给powershell脚本。有关更多详细信息,请参阅此MSDN链接:我还更新了问题中的示例。我已经在我的机器上执行了这项操作,它工作正常!希望这对你有用。非常感谢你的回复。实际上我还有一个问题。我正在开发web api,下面是我的post方法代码,C脚本在MaintenceModeService类中,但如何通过传递post中提供的参数来调用该服务。公共字符串Post[FromBody]MaintenanceMode值{string mgmtserver=value.mgmtserver;string NodeName=value.NodeName;}返回MaintenceModeServiceValue@杰克-如果这篇文章真的帮助你解决了你的问题,我建议你把它作为答案。然后请添加另一个问题,因为从注释中读取代码非常困难。我很乐意帮忙。