-1

i'm absolutely beginner in power-shell scripting and windows automation.so any help would be appreciated. i have access to a Azure-VM in my local pc at office. There are some tasks that i need to do daily on my local and VM machine. i am trying to automate these tasks with powershell. At one step i need to copy some CSV files from my VM to local and this is where i'm stuck. How do i connect to provided AzureVM from my local machine?(i'm not an admin for azure portal) I have User-Name , Password & my VM name(****.cloudapp.net) to connect in Remote Desktop Connection. i want to know if i can connect to provided VM and copy some files if yes then how! i have been searching for ways to connect but cant find any solution. i have installed azure module for powershell, tried Get-AzureVM but this does not work. As i said earlier any help would be really appreciated. thanks

Usman
  • 11
  • 1
  • 2
  • just confirm the issue. You can rdp to azure vm, but do not know how to copy files from azure vm to local with powershell? – Nancy Xiong Jun 14 '18 at 07:43
  • Yes, i can RDP to VM , i can manually copy files but i want to be able to do this in a powershell script runing on local machine. – Usman Jun 14 '18 at 08:43
  • It seems that `copy-item` can do that, referring to https://blogs.technet.microsoft.com/pstips/2016/04/04/copy-files-to-an-azure-vm-through-psremoting/ – Nancy Xiong Jun 14 '18 at 09:44
  • what then would be port parameter to New-PSSession when using copy-item fromsession ? My VM name ends with **".cloudapp.net"** not **".cloudapp.azure.com"** as given in example of the link you provided .it gives an error "Connecting to remote server ****.cloudapp.net failed with the following error message : The WinRM client cannot process the request" – Usman Jun 14 '18 at 11:20
  • Please look at my answer below. – Nancy Xiong Jun 14 '18 at 17:01
  • Is my answer below helpful? – Nancy Xiong Jul 17 '18 at 07:10

1 Answers1

0

First, please check port 5985 is listening on Azure VM using

netstat -ano

Then you need to add an inbound rule to allow the port 5985 on the NSG associated with the Azure VM in the azure portal. For the error message

The WinRM client cannot process the request

you need to add the remote Azure VM to the local machine's TrustedHosts list by executing the following command in the commands prompt as an administrator:

winrm set winrm/config/client @{TrustedHosts="Azure VM public IP address"}

I can copy a file folder named test from Azure VM to a local folder named tesp with powershell commands as below:

$c = Get-Credential
$sess =New-PSSession -ComputerName ip address -Port 5985 -Credential $c
Copy-Item -FromSession $sess -Path d:\test -Destination c:\tesp -Recurse

More information about Powershell Remoting.

Nancy Xiong
  • 21,523
  • 1
  • 8
  • 18
  • I checked port 5985 on VM and it is listening but Cannot add remote Acure VM to local machine's Trusted Hosts list by the command you gave adding my VM's public Ip address. Command prompt shows "WSManFault 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" – Usman Aug 06 '18 at 07:47
  • Can you `telnet vmpip 5985` from local. Post your specific command using winrm here. – Nancy Xiong Aug 06 '18 at 07:56