[英]the term is not recognized as the name of a cmdlet, function, script file, or operable program
I am trying to run a Powershell command in my C# program but I get the error我正在尝试在我的 C# 程序中运行 Powershell 命令,但出现错误
the term is not recognized as the name of a cmdlet, function, script file, or operable program.
该术语未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。 Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。
I even copied this same exact command in Powershell manually and it worked.我什至在 Powershell 中手动复制了这个完全相同的命令并且它有效。
The code follows as代码如下
PowerShell ps = PowerShell.Create();
var cmd = "scp -i \"path\\to\\ssh_key\" \"path\\to\\source_file\" root@192.168.x.xxx:/data/user_storage";
ps.AddCommand(cmd);
ps.Invoke();
The .AddCommand()
PowerShell SDK method accepts the name or file path of a command, not an entire command line . .AddCommand()
PowerShell SDK 方法接受命令的名称或文件路径,而不是整个命令行。
In order to execute a command line as you would in an interactive PowerShell session, use the .AddScript()
method instead.为了像在交互式 PowerShell 会话中一样执行命令行,请改用
.AddScript()
方法。
Alternatively, pass only the executable name/path to .AddCommand()
and follow it with an .AddArgument()
call for each argument.或者,仅将可执行名称/路径传递给
.AddCommand()
并在.AddArgument()
为每个参数调用.AddArgument()
。
Note that you don't strictly need PowerShell in order to execute an executable with arguments (unless you need shell features such as >
).请注意,您并不严格需要 PowerShell 来执行带有参数的可执行文件(除非您需要shell功能,例如
>
)。
Try to use cmd with Administrator rights instead of PowerShell尝试使用具有管理员权限的 cmd 而不是 PowerShell
your work can be done你的工作可以完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.