怎么用C#调用Powershell 向另外一台机器写txt文件

执迷的信徒 执迷的信徒 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
大家好,我手上有个用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");


还求高手帮忙解决啊!
...全文

相关推荐

OpenAPI fka Swagger规范代码生成器支持C PowerShell Go Java Nodejs Typ.zip

python

看海听风心情棒

Windows 11 升级 PowerShell 7:从安装到脚本迁移完整指南

命令行工具是现代系统管理与自动化运维的基础。Windows 11 内置的 PowerShell 5.1 虽然稳定,但其基于 .NET Framework 的架构在跨平台、JSON 解析和并发处理上已显不足。PowerShell 7 作为新一代脚本环境,基于 .NET,支持 Linux/macOS、默认 UTF-8 编码,并引入 `&&`、三元运算符、`ForEach-Object -Parallel` 等现代语法,能显著提升批量部署和 REST API 调用效率。在 Windows 11 上升级 Power

weixin_33842304

Windows命令行深度解析:CMD与PowerShell的核心差异与实战应用

命令行是操作系统与用户交互的重要接口,其本质是通过文本指令驱动计算机执行任务。在Windows生态中,命令行环境经历了从面向文本到面向对象的演进,这直接影响了系统管理、自动化脚本和开发运维的效率。命令提示符(CMD)作为传统工具,采用文本流驱动模型,通过管道传递纯文本数据,适合执行简单的顺序化任务。而PowerShell作为现代自动化平台,革命性地引入了对象管道技术,命令输出的是结构化的.NET对象而非纯文本,这使得数据处理、系统配置和复杂脚本编写能力得到质的飞跃。从技术价值看,对象模型避免了脆弱的文本解析

weixin_30367873

EmbedXrpc-单片机开发资源

GoogleGRPC,RPCRPCRPCC# IDL+csscript++QQ134161401

沐知全栈开发

MSBuild安装软件-下载即用.zip

下载代码方式:https://pan.quark.cn/s/9ed4377e58e6 MSBuild的软件部署

xinshenghuo22
评论
收藏 已收藏
分享
返回顶部
执迷的信徒
C# 111,129 成员 642,540 内容