怎么用C#调用Powershell 向另外一台机器写txt文件
执迷的信徒 2012-03-23 02:24:52 大家好,我手上有个用powershell向远程机器写一个txt文件的脚本报错如下:
Test method BaiduTest.TestBaidu.BaiduTest1 threw exception:
System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.
原脚本如下:
SecureString secureString = new SecureString();
String password="password";
foreach (char pwd in password.ToCharArray())
{
secureString.AppendChar(pwd);
}
PSCredential psc = new PSCredential("server01", secureString);
Collection<PSObject> retVals = new Collection<PSObject>();
string sessionURI = string.Format("http://{0}:5985/wsman", "computername");
WSManConnectionInfo session = new WSManConnectionInfo(new Uri(sessionURI), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", psc);
session.AuthenticationMechanism = AuthenticationMechanism.Default;
session.ProxyAuthentication = AuthenticationMechanism.Negotiate;
session.SkipCACheck = true;
session.SkipCNCheck = true;
Runspace runspace = RunspaceFactory.CreateRunspace(session);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddScript("new-item -path c:\\lqm\\temp\\ -name test8.txt -type file -force");
还求高手帮忙解决啊!