首页 > 解决方案 > 通过 ssh 进行的 Powershell 会话。开始工作

问题描述

我编写脚本以使用 PowerShell 远程控制 WindowsDefender。这是我第一次见到PS。我所拥有的:我有 Ubuntu 并在其上安装了 PS (v. 7) 客户端。我通过 ssh 设置 PS 会话到远程 Win10 机器。我尝试使用不同的命令,一切正常。

但我不明白接下来:

Terminal1:我打开了会话:

$session = New-PSSession -HostName user@host -KeyFilePath /home/user/.ssh/id_rsa

 Id Name            Transport ComputerName    ComputerType    State    ConfigurationName     
  2 Runspace1       SSH       host            RemoteMachine   Opened        De…

接下来我开始扫描WinDef:

Invoke-Command -Session $session -ScriptBlock {start-mpscan -asjob}

并从 ms 示例开始简单的后台作业

Invoke-Command -Session $session -ScriptBlock {Start-Job -ScriptBlock { Get-Process -Name pwsh }}

我可以得到所有的工作:

Invoke-Command -Session $session -ScriptBlock {get-job} 

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                   PSComputerName
2      Job2            RemoteJob       Completed     True            localhost             param($name, $boundPara… host
4      Job4            BackgroundJob   Completed     True            localhost             Get-Process -Name pwsh   host

但是,如果我离开此会话或尝试连接到同一主机,我将无法开始我的工作。我的 MS 文档说(如果我理解正确的话)会话在远程机器上打开,我可以回到它。我想早点开始工作以停止这项工作。

我尝试使用命令Enter-PSSession- 相同。

对于我的 linux pwsh 客户端,某些命令不起作用,例如

Connect-PSSession: The term 'Connect-PSSession' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

我很困惑。

我做错了什么?我怎样才能打开会话?我使用了错误的命令吗?还是我应该做一些设置并设置权限?

标签: powershellpowershell-core

解决方案


推荐阅读