使用C#代码中的参数和多个命令执行PowerShell脚本 [英] Executing PowerShell script with parameters and multiple commands from C# code
问题描述
嗨
我正在尝试使用C#代码从ASP.Net应用程序执行PowerShell脚本。脚本未执行,我也无法跟踪脚本未执行的原因。下面是代码和脚本。
I am trying to execute a PowerShell Script from ASP.Net application using C# code. The script is not executed, and also I am not able to trace why the script was not executed. Below is the code and script.
下面的脚本重命名位于"C:\Scripts\TestScript\Temp"的文本文件;
Below script rename the text files located at "C:\Scripts\TestScript\Temp"
Scripts1.ps1
功能脚本1
{
param
(
ServerName txt文件的#Path
[参数(位置= 0,Mandatory = $ true,HelpMessage =" FolderLocation重命名文件的路径")]
[别名(") ; PathofFolder")]
[string] $ ServerfilePath
)
Get-ChildItem $ ServerfilePath -Filter * .txt -Recurse | Rename-Item -NewName {$ _.Directory.Name +'_'+ $ _。Name}
}
Function Scripts1
{
param
(
#Path of the ServerName txt file
[Parameter(Position=0,Mandatory=$true,HelpMessage="Path of the FolderLocation to rename the files")]
[Alias("PathofFolder")]
[string]$ServerfilePath
)
Get-ChildItem $ServerfilePath -Filter *.txt -Recurse | Rename-Item -NewName { $_.Directory.Name+'_'+$_.Name}
}
C#代码
使用System;
使用System.Collections.Generic;
使用System .Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Management.Automation;
使用System.Text;
使用System.IO;
使用System。诊断;
使用System.Collections.ObjectModel;
使用System.Management.Automation.Runspaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Management.Automation;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Management.Automation.Runspaces;
protected void ExecuteScript(string script)
      {
             //创建Powershell运行空间
              Runspace runspace = RunspaceFactory.CreateRunspace();
              runspace.Open();
              RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
              runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
              //创建一个管道并将其提供给脚本文本
              Pipeline pipeline = runspace.CreatePipeline();
             命令scriptCommand = new命令(script,true,false);
              if(!string.IsNullOrEmpty(serverNames))
              {
                  scriptCommand.Parameters.Add(new CommandParameter(" ServerfilePath",@" C:\ Scripts\TestScript\Temp"));
              }¥b $ b               pipeline.Commands.Add(scriptCommand);
$
              pipeline.Invoke();
$
              pipeline.Stop();
              runspace.Close();
protected void ExecuteScript(string script)
{
//create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
Command scriptCommand = new Command(script, true, false);
if (!string.IsNullOrEmpty(serverNames))
{
scriptCommand.Parameters.Add(new CommandParameter("ServerfilePath", @"C:\Scripts\TestScript\Temp"));
}
pipeline.Commands.Add(scriptCommand);
pipeline.Invoke();
pipeline.Stop();
runspace.Close();
}
}
}
方法 ExecuteScript 将一个参数作为脚本内容,该内容位于上面提到的powershell脚本文件Scripts1.ps1中。
The Method ExecuteScript takes one parameter as the script content which is there in powershell script file Scripts1.ps1 mentioned above.
如果我遗漏了C#代码中的任何内容,请告诉我。我需要执行powershell sript从C#代码中获取参数。
Please let me know if i am missing anything in the C# code . I need to execute the powershell sript taking parameters from C#code .
推荐答案
嗨Biztalkist,
Hi Biztalkist,
>>如果我在C#代码中遗漏了任何内容,请告诉我。我需要执行powershell sript从C#代码中获取参数。
我已经制作了一个示例来执行powershell脚本。您可以参考以下代码。
I have made a sample to execute the powershell script. You can refer the following code.
powershell sript(AddItUp.ps1):
powershell sript(AddItUp.ps1):
[CmdletBinding()]
param (
[string]
param1
)
#begin
function AddStuff(
param1 ) # begin function AddStuff(
x)
{
x) {
这篇关于使用C#代码中的参数和多个命令执行PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!