Search code examples
powershellvirtual-machinerdpwinrmazure-vm

Send and Extract zip file to azure VM using powershell


I am trying to send and extract the zip file to azure VM but unable make the connection to the remote Azure VM.

Code

$cred = Get-Credential
$SO = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
$session = New-PSSession -ConnectionUri 'http://xx.xx.xxx.xxx:3389' -Credential $cred -SessionOption $SO

Send-File -Path C:\testArchive.zip -Destination C:\ -Session $session
Expand-Archive -Path C:\testArchive.zip -DestinationPath C:\ -Session $session

Error

New-PSSession : [xx.xx.xxx.xxx] Connecting to remote server xx.xx.xxx.xxx 
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 accepting 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.
At line:4 char:12
+ $session = New-PSSession -ConnectionUri 'http://xx.xx.xxx.xxx:3389' - ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:Re 
   moteRunspace) [New-PSSession], PSRemotingTransportException
    + FullyQualifiedErrorId : CannotConnect,PSSessionOpenFailed

Below is the output when i run 'winrm quickconfig' command on azure VM

WinRM service is already running on this machine.
WinRM is already set up for remote management on this computer.

When I run the 'Enter-PSSession -ComputerName LoadTestVm -Port 3389 -Credential qa-admin'

Enter-PSSession : Connecting to remote server LoadTestVm failed with the following error 
message : The WinRM client cannot process the request because the server name cannot be 
resolved. For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:1
+ Enter-PSSession -ComputerName LoadTestVm -Port 3389 -Credential qa-ad ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (LoadTestVm:String) [Enter-PSSession], PSRem 
   otingTransportException
    + FullyQualifiedErrorId : CreateRemoteRunspaceFailed

Solution

  • WINRM will run on either ports 5985 and 5986. The port 5985 is for HTTP and 5986 is for HTTPS. By default, it uses port 5985 if you have not specified it with -port. You should specify port 5985 instead of 3389, also enable it in your NSG if you have. So you could run Enter-PSSession -ComputerName "PublicIPaddress of VM" -Port 5985 -Credential $cred.

    This works on my side.

    Copy-Item -Path D:\nancy\4.zip -Destination C:\ –ToSession $session
    
    Invoke-Command -Session $session -ScriptBlock { Expand-Archive -Path C:\4.zip -DestinationPath C:\ }
    

    More references:

    https://www.assistanz.com/access-azure-windows-vm-through-powershell/

    https://geekdudes.wordpress.com/2016/11/16/enabling-remote-powershell-connection-to-azure-virtual-machine/

    https://mohitgoyal.co/2016/11/10/enable-powershell-remoting-on-azure-rm-virtual-machines/