簡體   English   中英

該術語未被識別為 cmdlet、函數、腳本文件或可運行程序的名稱

[英]the term is not recognized as the name of a cmdlet, function, script file, or operable program

我正在嘗試在我的 C# 程序中運行 Powershell 命令,但出現錯誤

該術語未被識別為 cmdlet、函數、腳本文件或可運行程序的名稱。 檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確並重試。

我什至在 Powershell 中手動復制了這個完全相同的命令並且它有效。

代碼如下

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();

.AddCommand() PowerShell SDK 方法接受命令的名稱文件路徑,而不是整個命令行

為了像在交互式 PowerShell 會話中一樣執行命令行,請改用.AddScript()方法。

或者,僅將可執行名稱/路徑傳遞給.AddCommand()並在.AddArgument()為每個參數調用.AddArgument()

請注意,您並不嚴格需要 PowerShell 來執行帶有參數的可執行文件(除非您需要shell功能,例如> )。

嘗試使用具有管理員權限的 cmd 而不是 PowerShell

你的工作可以完成

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM