簡體   English   中英

如何從 nushell 運行 powershell 命令

[英]how to run powershell command from nushell

在我的 windows 環境中安裝 nushell ( https://www.nushell.sh/ ) 后,我想結合 powershell 使用 nushell 的強大功能。

例如,我在日常工作中需要調用常見的 powershell 命令,例如

> get-service *myservice -computername remote | restart-service

但是如何從 nu shell 調用這樣的命令(使用管道)?

我需要從 powershell 開始 nu shell 還是反之亦然? 此外,帶有 ^ 符號的調用似乎不起作用。 我總是得到“找不到命令”,並且可以肯定它不是 DOS 命令。

如果能在 windows 上獲得兩個世界的力量就好了... nu shell 和 powershell

您可以使用-command標志在 PowerShell 中運行一次性命令。

所以從 Nu(或 cmd.exe 或任何其他 shell),像這樣的東西會起作用:

pwsh -command "echo 'Hello from PowerShell'"

(如果你想要舊的 Windows Powershell 版本,你可能需要用powershell替換pwsh

鑒於您的示例,我不確定您可以使用多少來組合 PowerShell 和 Nushell。 在這種特殊情況下,您只需從 Nushell 內部運行 PowerShell 命令,如前一個答案中所述,例如:

pwsh -c "get-service *myservice -computername remote | restart-service"

但是,假設您要將結構化的 PowerShell output 轉換為結構化的 Nushell 對象。 為此,您可以使用 JSON。 例如,在 Nu 中:

(pwsh.exe -c "Get-Service -ErrorAction Ignore | ConvertTo-Json -WarningAction Ignore") | from json | where Status == 1

但是請注意,您可能很快就會遇到我在這個問題 Unix 和 Linux 中描述的問題,因為 JSON 結果通常以 Nushell 列表而不是表格結束。

可以從 Nu 內部啟動管道,調用 PowerShell,反之亦然。

暫無
暫無

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

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