本文說明如何使用 PowerShell 連線至 Windows 虛擬機器 (VM) 執行個體。
事前準備
-
如果尚未設定驗證,請先完成設定。
「驗證」是指驗證身分的程序,確認您有權存取 Trusted Cloud by S3NS 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Trusted Cloud console to access Trusted Cloud by S3NS services and APIs, you don't need to set up authentication.
gcloud
-
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。 登入後,執行下列指令初始化 Google Cloud CLI:
gcloud init
- Set a default region and zone.
使用 PowerShell 連線
如果您的 Windows 工作站內含 PowerShell,就可以透過遠端 PowerShell 工作階段連線至 Windows Server 執行個體。
如果您尚未在遠端 Windows 執行個體上建立使用者名稱和密碼,請建立或重設 Windows 密碼。
新增防火牆規則,在 Windows Server 執行個體所在的 Trusted Cloud 虛擬私有雲網路上開啟通訊埠
5986
。在本機工作站上,開啟 PowerShell 終端機。
選用:您可以初始化一個變數來保存您的使用者憑證,這樣您就不需要每次連線至執行個體時都輸入使用者憑證。如果略過這個步驟,系統之後會提醒您輸入使用者名稱與密碼。
$credentials = Get-Credential
建立互動式 PowerShell 工作階段
如要建立 PowerShell 工作階段,請執行下列指令:
Enter-PSSession -ComputerName IP_ADDRESS -UseSSL -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -Credential $credentials
將
IP_ADDRESS
替換為您要連線的執行個體的外部 IP 位址、DNS 名稱或 Windows 電腦名稱。連線之後,指令提示字元會變更為包含遠端 Windows 執行個體的 IP 位址。現在,您可以使用終端機在遠端 Windows Server 執行個體上執行 PowerShell 指令。
從遠端在 Windows Server VM 上叫用指令
除了
Enter-PSSession
指令以外,您也可以搭配-ScriptBlock
旗標執行Invoke-Command
,以便在遠端執行個體上執行 PowerShell 指令,而不建立互動工作階段。Invoke-Command -ComputerName IP_ADDRESS -ScriptBlock { SCRIPT } -UseSSL -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -Credential $credentials
更改下列內容:
IP_ADDRESS
:要連線的執行個體的 IP 位址、DNS 名稱或 Windows 電腦名稱。SCRIPT
:要在遠端執行個體上執行的一或多個指令。例如,指定Get-EventLog -log "Windows PowerShell"
可取得記錄事件的清單。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-19 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-19 (世界標準時間)。"],[[["This document guides you through connecting to a Windows virtual machine (VM) instance using PowerShell, enabling remote management and command execution."],["Before connecting, ensure you have set up authentication via the Google Cloud CLI or are using the Google Cloud console, and have created or reset your Windows password on the remote instance."],["Connecting to a Windows Server instance requires opening port 5986 in your Google Cloud VPC network's firewall rules, in order to accept remote PowerShell connections."],["You can establish either an interactive PowerShell session using `Enter-PSSession` or remotely invoke commands on the VM without an interactive session by using the `Invoke-Command` method."],["When a user connects to a VM, they can utilize all IAM permissions granted to the service account attached to that specific VM."]]],[]] -