Connecting to a Windows VM via PowerShell
PowerShell Remoting Protocol (PSRP) with access via HTTPS is enabled for images of all versions and editions of the Windows operating system prepared for Yandex Cloud. You can connect to a virtual machine with the RUNNING
status over PSRP. Some time may be required to initialize all the services after the VM starts. If there is a connection error, retry after a few minutes.
Virtual machine security groups must allow incoming TCP traffic to port 5986.
To do this, specify its public IP address or fully qualified domain name (Fully Qualified Domain Name, FQDN
To connect to the VM:
-
Open the PowerShell console.
-
Create an object named
Credentials
replacing the<password>
with the one of theAdministrator
user you specified when creating the VM:$myUserName = "Administrator" $myPlainTextPassword = "<password>" $myPassword = $MyPlainTextPassword | ConvertTo-SecureString -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential($MyUserName, $myPassword)
-
Make sure that the username and password entered in the object are correct:
$networkCredential = $credential.GetNetworkCredential() $networkCredential | Select-Object UserName, Password
Result:
UserName Password -------- -------- Administrator <password>
-
Create a variable for the VM's IP address:
$ipAddress = "<IP_address>"
-
Create an object named
SessionOption
. In the object, specify the checks to skip:$sessionOption = New-PSSessionOption ` -SkipCACheck ` -SkipCNCheck ` -SkipRevocationCheck
-
Connect to an interactive session:
$psSession = @{ ComputerName = $ipAddress UseSSL = $true Credential = $credential SessionOption = $sessionOption } Enter-PSSession @psSession
Result:
[<IP_address>]: PS C:\Users\$myUserName\Documents>
Terminate the session:
Exit-PSSession
-
Create a session for non-interactive command execution:
$session = New-PSSession @psSession
Get a list of open sessions:
Get-PSSession
Result:
Id Name ComputerName ComputerType State ConfigurationName Availability -- ---- ------------ ------------ ----- ----------------- ------------ 2 WinRM2 <IP_address> RemoteMachine Opened Microsoft.PowerShell Available
Run the command on a remote VM:
$scriptBlock = { Get-Process } $invokeCommand = @{ ScriptBlock = $scriptBlock Session = $session } Invoke-Command @invokeCommand
Result:
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName PSComputerName ------- ------ ----- ----- ------ -- -- ----------- -------------- 249 13 4248 16200 0.11 4176 2 conhost <IP_address> 283 12 1888 4220 0.20 420 0 csrss <IP_address> ...