如何在C#中使用configfile和参数调用Powershell脚本 [英] How to call powershell script with configfile,parameters in c#
问题描述
你好,
我是C#中的新Powershell脚本.我有一个powershell脚本文件ps.ps1和powershell设置文件ConsoleSettings.psc1
C:\ Windows \ system32 \ WindowsPowerShell \ v1.0 \ powershell.exe -psconsolefile"D:\ e \ ConsoleSettings.psc1" -noexit -command." D:\ e \ ps.ps1"
运行并获取
Get-RST -SearchRoot``erd/user''-PasswordNeverExpires:$ false -PasswordNotChangedFor 60 -enabled
我的功能结果正确.
现在,我想在c#中获得此结果.我的代码是;
Hello,
I am new powershell script in c#. I have a powershell script file ps.ps1 and powershell settingfile ConsoleSettings.psc1
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -psconsolefile "D:\e\ConsoleSettings.psc1" -noexit -command ". ''D:\e\ps.ps1''"
run it and get
Get-RST -SearchRoot ''erd/user'' -PasswordNeverExpires:$false -PasswordNotChangedFor 60 -enabled
my function result correctly.
Now, i want to get this result in c# . My code is;
private void button1_Click(object sender, EventArgs e)
{
RunScript(LoadScript(@"d:\e\ps.ps1"));
}
private string RunScript(string scriptText)
{
PSConsoleLoadException x = null; ;
RunspaceConfiguration rsconfig = RunspaceConfiguration.Create(@"d:\e\ConsoleSettings.psc1", out x);
Runspace runspace = RunspaceFactory.CreateRunspace(rsconfig);
runspace.Open();
RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Get-RST -SearchRoot 'erd/user' -PasswordNeverExpires:$false -PasswordNotChangedFor 60 -enabled");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
private string LoadScript(string filename)
{
try
{
using (StreamReader sr = new StreamReader(filename))
{
StringBuilder fileContents = new StringBuilder();
string curLine;
while ((curLine = sr.ReadLine()) != null)
{
fileContents.Append(curLine + "\n");
}
return fileContents.ToString();
}
}
catch (Exception e)
{
string errorText = "The file could not be read:";
errorText += e.Message + "\n";
return errorText;
}
}
然后我出现一个错误:术语"Get-RST -SearchRoot" erd/user" -PasswordNeverExpires:$ false -PasswordNotChangedFor 60 -enabled"未被识别为cmdlet,函数,脚本文件的名称或可运行的程序.检查名称的拼写,或者是否包含路径,请验证路径是否正确,然后重试.
如何解决此问题,或如何使用configfile(在c#
中使用诸如Get-RST -SearchRoot``erd/user" -PasswordNeverExpires:$ false -PasswordNotChangedFor 60 -enabled)的配置文件调用Powershell脚本.
请帮助我...
And then i have a error : the term "Get-RST -SearchRoot ''erd/user'' -PasswordNeverExpires:$false -PasswordNotChangedFor 60 -enabled" is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
How to solve this problem, or how to call powershell script with configfile, parameter like (Get-RST -SearchRoot ''erd/user'' -PasswordNeverExpires:$false -PasswordNotChangedFor 60 -enabled) in c#
please help me...
推荐答案
false -PasswordNotChangedFor 60 -enabled
我的功能结果正确.
现在,我想在c#中获得此结果.我的代码是;
false -PasswordNotChangedFor 60 -enabled
my function result correctly.
Now, i want to get this result in c# . My code is;
private void button1_Click(object sender, EventArgs e)
{
RunScript(LoadScript(@"d:\e\ps.ps1"));
}
private string RunScript(string scriptText)
{
PSConsoleLoadException x = null; ;
RunspaceConfiguration rsconfig = RunspaceConfiguration.Create(@"d:\e\ConsoleSettings.psc1", out x);
Runspace runspace = RunspaceFactory.CreateRunspace(rsconfig);
runspace.Open();
RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Get-RST -SearchRoot 'erd/user' -PasswordNeverExpires:
false -PasswordNotChangedFor 60 -enabled"); 集合< PSObject>结果= pipeline.invoke(); runspace.Close(); StringBuilder stringBuilder = 新 StringBuilder(); foreach (PSObject obj in 结果) { stringBuilder.AppendLine(obj.ToString()); } 返回 stringBuilder.ToString(); } 私有 字符串 LoadScript(字符串文件名) { 尝试 { 使用(StreamReader sr = 新 StreamReader(文件名)) { StringBuilder fileContents = 新 StringBuilder(); 字符串 curLine; 同时((curLine = sr.ReadLine())!= 空) { fileContents.Append(curLine + " ); } 返回 fileContents.ToString(); } } 捕获(异常e) { 字符串 errorText = " ; errorText + = e.Message + " ; 返回 errorText; } }
false -PasswordNotChangedFor 60 -enabled"); Collection<PSObject> results = pipeline.Invoke(); runspace.Close(); StringBuilder stringBuilder = new StringBuilder(); foreach (PSObject obj in results) { stringBuilder.AppendLine(obj.ToString()); } return stringBuilder.ToString(); } private string LoadScript(string filename) { try { using (StreamReader sr = new StreamReader(filename)) { StringBuilder fileContents = new StringBuilder(); string curLine; while ((curLine = sr.ReadLine()) != null) { fileContents.Append(curLine + "\n"); } return fileContents.ToString(); } } catch (Exception e) { string errorText = "The file could not be read:"; errorText += e.Message + "\n"; return errorText; } }
然后我出现一个错误:术语"Get-RST -SearchRoot" erd/user" -PasswordNeverExpires:
And then i have a error : the term "Get-RST -SearchRoot ''erd/user'' -PasswordNeverExpires:
false -PasswordNotChangedFor 60 -enabled"未被识别为cmdlet,函数,脚本文件或可运行程序的名称.检查名称的拼写,或者是否包含路径,请验证路径是否正确,然后重试.
如何解决此问题,或如何使用配置文件调用Powershell脚本,该文件的参数为(Get-RST -SearchRoot``erd/user''-PasswordNeverExpires:
false -PasswordNotChangedFor 60 -enabled" is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
How to solve this problem, or how to call powershell script with configfile, parameter like (Get-RST -SearchRoot ''erd/user'' -PasswordNeverExpires:
这篇关于如何在C#中使用configfile和参数调用Powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!