首页 > 解决方案 > 通过 SSH 使用 CMD 运行 PowerShell 命令

问题描述

我在 Windows Server 2019 上使用命令提示符 (CMD)运行以下PowerShell命令,它可以正常工作:

powershell -command "Get-Childitem -Path [my source path] -Include *.bak -File -Recurse | Sort-Object -Descending -Property LastWriteTime | Select-Object -First 1 | Compress-Archive -Force -DestinationPath [my target path].zip"

但是当我想通过SSH运行它时:

ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null [username]@[server] powershell -command "Get-Childitem -Path [my source path] -Include *.bak -File -Recurse | Sort-Object -Descending -Property LastWriteTime | Select-Object -First 1 | Compress-Archive -Force -DestinationPath [my target path].zip"

我得到错误:

'Sort-Object' 不是内部或外部命令、可运行程序或批处理文件。

标签: windowspowershellshellcmdcommand-line

解决方案


推荐阅读