在 Linux 中透過 PowerShell 遠端連線到 Windows

在 Linux 中透過 PowerShell 遠端連線到 Windows

我正在嘗試透過 Linux 電腦中的 PowerShell 連接到 Windows 電腦以獲取一些系統信息,但我需要在不使用 WinRm 的情況下實現它。

首先,我按照說明安裝了 PowerShell這裡。啟動 PowerShellpwsh工作正常。

然後我嘗試使用以下命令透過 WMI 獲取一些資訊:

Get-WmiObject -Class Win32_Process -Impersonation 3 -ComputerName IP_ADDRESS

回報是Get-WmiObject : The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program..

變更日誌對於 PowerShell 核心的 6.0 版本,我發現 Get-Wmi* 函數應該替換為 Get-Cmi* 等效函數。咱們試試:

Get-CimInstance -Class Win32_Process -Impersonation 3 -ComputerName IP_ADDRESS

回報是:Get-CimInstance : The term 'Get-CimInstance' is not recognized as the name of a cmdlet, function, script file, or operable program.

尋找命令列表,鍵入Get-然後Tab,返回確實沒有顯示任何Wmi或Cim函數。

我可以使用 cmdlet 連接到同一台計算機Invoke-Command,但正如我所說,我需要在不使用 WinRm 的情況下實現它,而且顯然,情況並非如此調用命令

經過大量谷歌搜索,我只找到一個類似的問題在SO這裡,但它使用 cmdlet Enter-PSSession,這也使用WinRM如果我理解正確的話。

最後,我找到了這個部落格文章就像手套一樣適合我的需求。但是,它建議使用 cmdlet ,Invoke-WmiMethod並且Invoke-CimMethod令人驚訝的是,這些命令不是可識別的命令。

我的問題是:是否有辦法在 Linux 電腦中執行 WMI 查詢以透過 PowerShell 取得一些 Windows 資訊而不使用 WinRm?

註1:我能夠在 Windows 電腦 PowerShell 中運行Get-Wmi*Get-Cim*cmdlet(例如,透過 RPC 連接);

筆記2: 我認識一個解決方法在沒有PowerShell的Linux中處理它,它應該適用於有類似問題的人,但由於無法解決(至少目前是這樣),它對我不起作用編碼問題;

資訊

  • 作業系統:Debian 8.10

  • $PSVersionTable.PS版本:6.0.1

  • 遠端Windows:W10 Pro

答案1

改用 SSH 上的 PoSH。

請參閱此處的步驟:

透過 SSH 進行 PowerShell 遠端處理

概述

PowerShell 遠端處理通常使用 WinRM 進行連線協商和資料傳輸。選擇 SSH 來實現此遠端處理,是因為它現在可用於 Linux 和 Windows 平台,並允許真正的多平台 PowerShell 遠端處理。但是,WinRM 也為 PowerShell 遠端會話提供了強大的託管模型,而此實作尚未做到這一點。這表示此實作尚不支援 PowerShell 遠端端點配置和 JEA(Just Enough Administration)。

PowerShell SSH 遠端處理可讓您在 Windows 和 Linux 電腦之間執行基本的 PowerShell 會話遠端處理。這是透過在目標電腦上建立 PowerShell 託管進程作為 SSH 子系統來完成的。最終,這將更改為更通用的託管模型,類似於 WinRM 的工作方式,以支援端點配置和 JEA。

New-PSSession、Enter-PSSession 和 Invoke-Command cmdlet 現在有新的參數集來促進這種新的遠端連接

https://github.com/PowerShell/PowerShell/tree/master/demos/SSHRemoting

更新了 Anthony Geoghegan 的評論

至於——

“這看起來很有希望,但 GitHub 存儲庫的鏈接不再有效。”

----該連結直接指向 MS PS 儲存庫,因此,是的,發生 404 很奇怪。無論哪種方式,您都可以使用 PowerShell 直接在系統上取得它。

反正 …

Find-Module -Name '*ssh*' | Format-table -AutoSize

# Results


Version Name                         Repository Description                                                                                                
------- ----                         ---------- -----------                                                                                                
2.1     Posh-SSH                     PSGallery  Provide SSH and SCP functionality for executing commands against remote hosts.                             
0.0.2.0 OpenSSHUtils                 PSGallery  Utilities and functions for configuring OpenSSH on Windows.                                                
2.1.3   SSHSessions                  PSGallery  Svendsen Tech's SSH-Sessions module provides SSH session creation, management and interaction from Power...
1.0.0   SSH                          PSGallery  Provides a PowerShell-based SSH client based on SSH.net  http://sshnet.codeplex.com/                       
0.0.75  PSSharedGoods                PSGallery  Module covering functions that are shared within multiple projects                                         
1.1.3   PowerSSH                     PSGallery  This module detects the first use of an SSH command, automatically runs the SSH agent, keeps the SSH aut...
0.9.5   WinSSH                       PSGallery  Install OpenSSH-Win64, optionally install ssh-agent and sshd Services. Also includes functions to help c...
1.0.1   ssh-wrapper                  PSGallery  Exposes ssh from WSL by wrapping: bash -c "ssh $args". Requires Windows Subsystem for Linux on Windows 10. 
0.3.1   posh-sshell                  PSGallery  Provides integration with ssh-agent and pageant from within Powershell                                     
2.0.1.8 SkypeForBusinessHybridHealth PSGallery  Uses on-premises modules such as Skype For Business and SkypeOnlineConnector to validate basic requireme...
1.0.4   PSShortcut                   PSGallery  This module eases working with Windows shortcuts (LNK and URL) files.                                      
1.1.4   PowerSSH-Legacy              PSGallery  This module detects the first use of an SSH command, automatically runs the SSH agent, keeps the SSH aut...
1.0     cEPRSSharepoint              PSGallery  DSCModule helps in installing & configuring the sharepoint site, Farm etc.,                                
0.0.3   dockeraccesshelper           PSGallery  Allow a user account to access the docker engine without elevated access rights                            
0.3     PSShareFile                  PSGallery  A PowerShell module to manipulate various objects in Citrix's ShareFile service                            
0.0.1   PSSherpaDesk                 PSGallery  PowerShell module for interacting with the SherpaDesk API  


Find-Module -Name '*Posh-SSH' | 
Save-Module -Path "$env:USERPROFILE\Documents\WindowsPowerShell\Modules" 
Install-Module -Name 'Posh-SSH'

使用此連結進行 SSHRemoting 演示

我剛剛擊中了它,所以我知道它有效。

請觀看這些 YouTube 影片 PowerShell SSH 遠端處理的實際應用。

相關內容