| name | winrm-remoting |
| description | Remote PowerShell execution via WinRM. Use for managing remote Windows hosts, running commands on multiple machines, and deploying scripts remotely. |
| version | 1.0.0 |
WinRM Remoting Skill
適用場景
- 在遠端 Windows 主機執行 PowerShell
- 批次管理多台伺服器
- 遠端部署腳本或設定
前置條件
# 目標主機啟用 WinRM
Enable-PSRemoting -Force
# 驗證連線
Test-WSMan -ComputerName "target-host"
基本遠端執行
# 單次執行
$result = Invoke-Command -ComputerName "server01" -ScriptBlock {
Get-Service | Where-Object Status -eq "Running"
} -Credential (Get-Credential)
# 持續工作階段
$session = New-PSSession -ComputerName "server01" -Credential (Get-Credential)
try {
Invoke-Command -Session $session -ScriptBlock { Get-Process }
Copy-Item -Path "C:\local\script.ps1" -Destination "C:\remote\" -ToSession $session
}
finally {
Remove-PSSession $session
}
多主機平行執行
$servers = @("server01", "server02", "server03")
$cred = Get-Credential
$results = Invoke-Command -ComputerName $servers -Credential $cred -ScriptBlock {
[PSCustomObject]@{
Host = $env:COMPUTERNAME
CPU = (Get-CimInstance Win32_Processor).LoadPercentage
Memory = [math]::Round((Get-CimInstance Win32_OperatingSystem).FreePhysicalMemory/1MB, 2)
}
}
$results | Format-Table -AutoSize
安全注意
- 優先使用 HTTPS(port 5986),避免明文(5985)
- 使用
CredSSP 僅在必要時(Kerberos 雙跳問題)
- 遠端工作階段用完立即
Remove-PSSession
- 避免在腳本中硬編碼憑證
參考資料
references/sessions.md — 工作階段管理
references/credentials.md — 憑證安全處理