2011-07-22 60 views
4
PowerShell powershell = PowerShell.Create(); 
PSCommand command = new PSCommand(); 
command.AddScript("[System.Net.ServicePointManager]::ServerCertificateValidationCallback+={$true}"); 

是吗?
下一步是什么?使用C#来运行PowerShell脚本

+0

[?command.Invoke()](http://msdn.microsoft.com/en-us/library/dd144526(V = VS.85)的.aspx) –

+0

感谢您的回复,但命令没有Invoike()方法。 –

+0

是的,它应该是'powershell.Invoke()'完成Coincoin的建议之后。 –

回答

6

MSDN

PSCommand command = new PSCommand(); 
command.AddScript( 
    "[System.Net.ServicePointManager]::ServerCertificateValidationCallback+={$true}" 
); 

PowerShell powershell = PowerShell.Create(); 
powershell.Commands = cmd; 

var results = powershell.Invoke(); 
+0

不应该'powershell.Commands = cmd;'是'powershell.Commands = command;'? –

+0

请注意,PowerShell是IDisposable,所以它应该在一个使用块中。另外请注意,您的系统上可能有多个版本的System.Management.Automation.dll,并且您需要这些类的PowerShell 2.0或更高版本。在我的系统上,正确的文件位于C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ WindowsPowerShell \ v1.0 \ System.Management.Automation.dll - Program Files中的文件已旧。 – TrueWill