远程开启服务器的RDP,首先服务器要开启WimRM服务,服务器版的 Windows 系统默认是启动 WinRM 服务的,而普通版的系统默认则没有启动该服务。
如何开启WimRM服务请参考:
远程开启RDP
$new_vm_name = "XXX.XXX.XXX.XXX"
$server_User = "admin"
$server_PWord = ConvertTo-SecureString –String "password" –AsPlainText -Force
$server_Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $server_User, $server_PWord
$new_vm_session = New-PSSession -ComputerName $new_vm_name -Credential $server_Credential
invoke-command -session $new_vm_session -scriptblock{
set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server'-name "fDenyTSConnections" -Value 0
}
远程关闭RDP
$new_vm_name = "XXX.XXX.XXX.XXX"
$server_User = "admin"
$server_PWord = ConvertTo-SecureString –String "password" –AsPlainText -Force
$server_Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $server_User, $server_PWord
$new_vm_session = New-PSSession -ComputerName $new_vm_name -Credential $server_Credential
invoke-command -session $new_vm_session -scriptblock{
set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server'-name "fDenyTSConnections" -Value 1
}