Connecting to a Windows VM via PowerShell
Images of all Windows versions and editions for Yandex Cloud deployments come with enabled PowerShell Remoting Protocol (PSRP) with HTTPS access. You will be able to connect to a VM with the RUNNING
status via PSRP. It may take some time for all services to initialize after the VM starts. If you get a connection error, try again in a few minutes.
VM security groups must allow incoming TCP traffic on port 5986.
To connect via PSRP, specify the public IP address or fully qualified domain name (FQDN
To connect to a VM:
-
Start PowerShell.
-
Create an object named
Credentials
, replacing<password>
withAdministrator
password 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 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 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 non-interactive session:
$session = New-PSSession @psSession
Get a list of active sessions:
Get-PSSession
Result:
Id Name ComputerName ComputerType State ConfigurationName Availability -- ---- ------------ ------------ ----- ----------------- ------------ 2 WinRM2 <IP_address> RemoteMachine Opened Microsoft.PowerShell Available
Run this 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> ...