I have an application which requires a website to be able to make an HTTP call which turns on and logs in to an Azure VM. I am trying to do this with a PowerShell Azure Function. I can successfully turn the VM on/off with the function, using:
$secpasswd = ConvertTo-SecureString $env:SP_PASSWORD -AsPlainText -Force; $mycreds = New-Object System.Management.Automation.PSCredential ($env:SP_USERNAME, $secpasswd) Add-AzureRmAccount -ServicePrincipal -Tenant $env:TENANTID -Credential $mycreds; $context = Get-AzureRmContext; Set-AzureRmContext -Context $context; # Start VM Start-AzureRmVM -ResourceGroupName myResourceGroup -Name myDevice | Out-String
But I'm getting permission denied errors when trying to log into the VM. I have tried the following methods:
$password = ConvertTo-SecureString "myPassword" -AsPlainText -Force $cred= New-Object System.Management.Automation.PSCredential ("myUsername", $password ) #Enter-PSSession -ConnectionUri https://<public_ip> -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck) -Authentication Negotiate
$Server="<public_ip>" $User="myUsername" $Password="myPassword" cmdkey /generic:TERMSRV/$Server /user:$User /pass:$Password mstsc /v:$Server
When logging in with the standard Remote Desktop GUI, I also need to dismiss the certificate prompts. What do I need to do to log in automatically using an Azure Function?
The VM is running Windows 10.
EDIT: I've set up the ports, made a local certificate and made sure that WinRM is setup for remote management on the VM, and also that it's listening on HTTPS. However, when I try the command on my local machine:
Enter-PSSession -ComputerName 22.214.171.124 -Credential $cred -UseSSL -SessionOption $so
I get the error:
Enter-PSSession : Connecting to remote server 126.96.36.199 failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is acce pting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.
EDIT: I have verified that the port is open and accessible from the Azure Function using
New-Object Net.Sockets.TcpClient "<public-ip>", 5986