Powershell脚本将文件从服务器复制到azure blob容器

问题描述 投票:0回答:1

我想将我服务器中的所有文件(azure vm)复制到容器(azure blob存储)是否可以通过powershell?我是powershell的新手请帮帮我在任何脚本中与你分享

azure-devops azure-powershell powershell-remoting
1个回答
0
投票

首先,确保已在VM中安装了Az powershell模块以及要运行该命令的位置。在我的示例中,我使用我的PC来运行命令。

尝试将下面的脚本存储在PC中,我使用C:\Users\joyw\Desktop\script.ps1,脚本将上传VM中C:\Users\xxxx\Desktop\test文件夹中的所有文件,您可以将其更改为您想要的路径。

script.ps1:

$context = New-AzStorageContext -StorageAccountName "<StorageAccountName>" -StorageAccountKey "<StorageAccountKey>"
$files = (Get-ChildItem "C:\Users\xxxx\Desktop\test" -recurse).FullName
foreach($file in $files){
    Set-AzStorageBlobContent -Context $context -File "$file" -Container "<container name>" -Blob "$file" 
}

然后运行命令:

Connect-AzAccount
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -Name 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'C:\Users\joyw\Desktop\script.ps1'

如果您想以非交互方式登录,请参阅此link

将参数更改为您想要的,将文件上传到容器后,它将显示为原始文件结构。

© www.soinside.com 2019 - 2024. All rights reserved.