怎么用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");


还求高手帮忙解决啊!
...全文
224 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧