【问题标题】:Powershell command works not on c# [closed]Powershell命令不适用于c#[关闭]
【发布时间】:2018-02-22 19:15:43
【问题描述】:

我尝试使用 Powershell 重命名映射的网络驱动器。 在 Powershell Script 中运行命令就可以了。但是当我在 c# 中尝试使用 Powershell 进程时出现错误:参数不能为 NULL。 有人知道我的代码出了什么问题吗?

Process p2 = new Process();
p2.StartInfo.FileName = "powershell.exe";
p2.StartInfo.Verb = "runas";
p2.StartInfo.RedirectStandardOutput = true;
p2.StartInfo.RedirectStandardError = true;
p2.StartInfo.UseShellExecute = false;
p2.StartInfo.CreateNoWindow = true;
p2.StartInfo.Arguments = "$sh=New-Object -com Shell.Application" + " sh.NameSpace('" + webDrive + "').Self.Name = '" + newLabel + "'";   
p2.Start();
string errors = p2.StandardError.ReadToEnd();
MessageBox.Show(errors);

【问题讨论】:

  • 错误在哪一行? 真的知道这一点很有帮助
  • 您是否尝试调试它以确保webDrivenewLabel 具有正确的值?

标签: c# powershell process.start


【解决方案1】:

可能是(您在 sh 变量和 2 条指令之间的分号上忘记了 $):

p2.StartInfo.Arguments = "$sh=New-Object -com Shell.Application;" + " $sh.NameSpace('" + webDrive + "').Self.Name = '" + newLabel + "'";  

【讨论】:

  • 你应该解释你的答案在做什么。只是粘贴一些代码对任何人没有帮助
  • 哇,你说得对。分号不见了。感谢 1000 次。
  • 欢迎您,玩得开心;)
猜你喜欢
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-09
  • 1970-01-01
  • 2016-01-31
  • 1970-01-01
  • 2021-02-21
相关资源
最近更新 更多