概述
针对ASM环境的脚本:
param(
# SrcDir
[Parameter(Mandatory = $true)]
[string]$SrcDir,
# Subscription Name
[Parameter(Mandatory = $true)]
[string]$SubscriptionName,
# StorageAccountName
[Parameter(Mandatory = $true)]
[string]$StorageAccountName,
# Container Name
[Parameter(Mandatory = $true)]
[string]$ContainerName
)
function Upload ($folder, $SrcDirLen)
{
$subItems = Get-ChildItem $folder;
foreach ($item in $subItems)
{
if ($item.Mode -eq "d-----")
{
Upload $item.FullName $SrcDirLen;
} else {
$fileName = "$folder$item";
$destpath = $folder.Substring($SrcDirLen);
$blobName = "$destpath/$item";
Write-Host "Copying $fileName to $blobName";
[void](Set-AzureStorageBlobContent -File $filename -Container $ContainerName -Blob $blobName -Context $storageAccount.Context -Force);
}
}
}
$cred = Get-Credential -Message "Use your Azure account to login";
[void](Add-AzureAccount -Environment AzureChinaCloud -Credential $cred);
[void](Select-AzureSubscription -SubscriptionName $SubscriptionName);
$storageAccount = Get-AzureStorageAccount -StorageAccountName $StorageAccountName;
upload $SrcDir $SrcDir.Length;
Write-Host "All files in $SrcDir uploaded to $containerName
针对ARM环境的脚本:
param(
# SrcDir
[Parameter(Mandatory = $true)]
[string]$SrcDir,
# Subscription Name
[Parameter(Mandatory = $true)]
[string]$SubscriptionName,
# ResourceGroupName
[Parameter(Mandatory = $true)]
[string]$ResourceGroupName,
# StorageAccountName
[Parameter(Mandatory = $true)]
[string]$StorageAccountName,
# Container Name
[Parameter(Mandatory = $true)]
[string]$ContainerName
)
function Upload ($folder, $SrcDirLen)
{
$subItems = Get-ChildItem $folder;
foreach ($item in $subItems)
{
if ($item.Mode -eq "d-----")
{
Upload $item.FullName $SrcDirLen;
} else {
$fileName = "$folder$item";
$destpath = $folder.Substring($SrcDirLen);
$blobName = "$destpath/$item";
Write-Host "Copying $fileName to $blobName";
[void](Set-AzureStorageBlobContent -File $filename -Container $ContainerName -Blob $blobName -Context $storageAccount.Context -Force);
}
}
}
$cred = Get-Credential -Message "Use your Azure account to login";
[void](Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $cred);
[void](Select-AzureRmSubscription -SubscriptionName $SubscriptionName);
$storageAccount = Get-AzureRmStorageAccount -Name $StorageAccountName -ResourceGroupName $ResourceGroupName;
Upload $SrcDir $SrcDir.Length;
Write-Host "All files in $SrcDir uploaded to $ContainerName.";
测试效果:
PS C:UsersDanielHX> &C:UsersDanielHXDesktop[ARM]upload_local_dir_to_storage.ps1 -SubscriptionName Internal-005 -StorageAccountName daneaststorag
e -ContainerName demo -SrcDir D:test -ResourceGroupName daneastresourcegroup1
Copying D:testtest11.txt to test1/1.txt
Copying D:testtest12.txt to test1/2.txt
Copying D:testtest2test41.txt to test2test4/1.txt
Copying D:testtest21.txt to test2/1.txt
Copying D:testtest22.txt to test2/2.txt
Copying D:testtest23.txt to test2/3.txt
Copying D:testtest31.txt to test3/1.txt
Copying D:testtest32.txt to test3/2.txt
Copying D:testtest33.txt to test3/3.txt
Copying D:testtest34.txt to test3/4.txt
Copying D:test1.txt to /1.txt
All files in D:test uploaded to demo.
PS C:UsersDanielHX> &C:UsersDanielHXDesktop[ASM]upload_local_dir_to_storage.ps1 -SrcDir D:test -SubscriptionName Internal-005 -StorageAccountNa
me danieleaststorage -ContainerName test
Copying D:testtest11.txt to test1/1.txt
Copying D:testtest12.txt to test1/2.txt
Copying D:testtest2test41.txt to test2test4/1.txt
Copying D:testtest21.txt to test2/1.txt
Copying D:testtest22.txt to test2/2.txt
Copying D:testtest23.txt to test2/3.txt
Copying D:testtest31.txt to test3/1.txt
Copying D:testtest32.txt to test3/2.txt
Copying D:testtest33.txt to test3/3.txt
Copying D:testtest34.txt to test3/4.txt
Copying D:test1.txt to /1.txt
All files in D:test uploaded to test.
最后
以上就是柔弱花生为你收集整理的[Azure]使用Azure Powershell上传本地目录中的文件文件(递归)的全部内容,希望文章能够帮你解决[Azure]使用Azure Powershell上传本地目录中的文件文件(递归)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复