如何运行PowerCli并通过c#运行powershell脚本命令 [英] how to run PowerCli and run powershell script commands through c#
本文介绍了如何运行PowerCli并通过c#运行powershell脚本命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用以下代码连接到powercli并在其中运行powershell脚本,当我尝试这样做时,在
pipeline.Invoke(); $ p $收到错误p>
错误消息: System.Management.Automation.dll中出现'System.Management.Automation.PSSecurity'
附加信息:文件'C:\ script \ resources.ps1'未被识别为cmdlet,函数,脚本或可操作程序的名称。检查名称的拼写,或者如果包含路径,验证路径是否正确并再试一次,我不知道如何解决它
以下是代码:
private void Powercli_Click( object sender,EventArgs e)
{
RunspaceConfiguration config = RunspaceConfiguration.Create() ;
Runspace runspace = RunspaceFactory.CreateRunspace(config);
// 打开它
runspace.Open();
尝试
{
RunspaceInvoke scriptinvoker = new RunspaceInvoke(运行空间);
// import PowerCLI ViCore snapin
PSSnapInException警告;
config.AddPSSnapIn( VMware.VimAutomation.Core, out 警告);
if (警告!= null )
{
throw 警告;
}
// 创建管道并将其提供给脚本文本
Pipeline pipeline = runspace.CreatePipeline();
// 创建命令以设置Na选项
var connectVI = new 命令( 连接-VIServer跨度>);
connectVI.Parameters.Add( Server, 192.168.123.108);
connectVI.Parameters.Add( Protocol, https);
connectVI.Parameters.Add( User, root);
connectVI.Parameters.Add( 密码, assign);
命令mycommand = 新命令( C :\\script\\resource.ps1\" 跨度>);
// 将命令添加到管道
pipeline.Commands。加入(connectVI);
pipeline.Commands.Add(mycommand);
// pipeline.Commands.Add(mycommand1);
// 通过调用执行
pipeline.Invoke();
if (pipeline.HadErrors)
{
MessageBox.Show(pipeline.Error.ToString(),< span class =code-string> 发生错误);
}
}
最后
{
runspace.Close();
}
}
解决方案
这应该可以帮到你。
http://stackoverflow.com/questions/11405384/how-to-pass-a-parameter-from-c-sharp-to-a-powershell-script-file [ ^ ]
Am using the below code to connect to powercli and run a powershell script in it and when i try to do so am getting an error at
pipeline.Invoke();
error message as : 'System.Management.Automation.PSSecurity' occured in System.Management.Automation.dll
Additional information: File 'C:\script\resource.ps1' is not recognised as the name of cmdlet, function, script, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again , I have no idea how to solve it
Here is the code:
private void Powercli_Click(object sender, EventArgs e)
{
RunspaceConfiguration config = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(config);
// open it
runspace.Open();
try
{
RunspaceInvoke scriptinvoker = new RunspaceInvoke(runspace);
// import PowerCLI ViCore snapin
PSSnapInException warning;
config.AddPSSnapIn("VMware.VimAutomation.Core", out warning);
if (warning != null)
{
throw warning;
}
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
// Create Command to Set Na Option
var connectVI = new Command("Connect-VIServer");
connectVI.Parameters.Add("Server", "192.168.123.108");
connectVI.Parameters.Add("Protocol", "https");
connectVI.Parameters.Add("User", "root");
connectVI.Parameters.Add("Password", "assign");
Command mycommand = new Command("C:\\script\\resource.ps1");
// Add Command to Pipeline
pipeline.Commands.Add(connectVI);
pipeline.Commands.Add(mycommand);
//pipeline.Commands.Add(mycommand1);
//Execute by invoking
pipeline.Invoke();
if (pipeline.HadErrors)
{
MessageBox.Show(pipeline.Error.ToString(), "Error occurred");
}
}
finally
{
runspace.Close();
}
}
解决方案 This should help you.
http://stackoverflow.com/questions/11405384/how-to-pass-a-parameter-from-c-sharp-to-a-powershell-script-file[^]
这篇关于如何运行PowerCli并通过c#运行powershell脚本命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文