简体   繁体   English

如何使用Powershell将文件复制到VM?

[英]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. 如果不是,该怎么办,例如,我要将文件复制到Azure VM。 There is a thread Windows Azure Powershell Copying file to VM , but its four years old and the answers require many steps. Windows Azure Powershell有一个将文件复制到VM的线程,但是它已有4年的历史,而答案需要很多步骤。

You could use Azure File Share (AFS) , and then: 您可以使用Azure文件共享(AFS) ,然后:

  1. Mount the AFS as a drive on the VM 将AFS作为驱动器安装在VM上
  2. Use the REST Api or a library to upload the files to the AFS 使用REST Api或将文件上传到AFS

Agree with Rasmusgude, you can upload file to Azure File Share then mount Azure File Share to that VM. 同意Rasmusgude,您可以将文件上传到Azure文件共享,然后将Azure文件共享安装到该VM。

Are you administrator of that VM? 您是该VM的管理员吗?

If yes, you can enable WinRM and use WinRM to upload files to it. 如果是,则可以启用WinRM并使用WinRM将文件上传到其中。

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. 关于启用Azure VM WinRM,应将端口5985添加到Azure VM的NSG入站规则,并将端口5985添加到Windows防火墙入站规则。

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 . 关于将文件上传到该VM,可以使用此PowerShell命令Send-File -Path C:test.xml -Destination C: -Session $session

Here a blog about sending files over WinRM, please refer to it. 这里是有关通过WinRM发送文件的博客 ,请参考它。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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