简体   繁体   中英

How to copy file to VM using Powershell?

Sounds very simple, but most of the answers given on the internet assume both computers are on the same network. What if they are not, eg I want to copy a file to Azure VM. There is a thread Windows Azure Powershell Copying file to VM , but its four years old and the answers require many steps.

You could use Azure File Share (AFS) , and then:

  1. Mount the AFS as a drive on the VM
  2. Use the REST Api or a library to upload the files to the AFS

Agree with Rasmusgude, you can upload file to Azure File Share then mount Azure File Share to that VM.

Are you administrator of that VM?

If yes, you can enable WinRM and use WinRM to upload files to it.

About enable Azure VM WinRM, you should add port 5985 to Azure VM's NSG inbound rules and add port 5985 to windows firewall inbound rules.

Then use this script to create a session:

$username = 'user'
$pass = ConvertTo-SecureString -string 'password' -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $pass
$s = New-PSSession -ConnectionUri 'http://xx.xx.xx.xx:5985' -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)

About upload files to that VM, you can use this PowerShell command Send-File -Path C:test.xml -Destination C: -Session $session .

Here a blog about sending files over WinRM, please refer to it.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM