我正在尝试为更改Azure存储Blob层创建Azure自动化,但它无法正常工作。 [英] I am trying to create Azure Automation for Change Azure Storage Blob Tiers but it is not working.
问题描述
Hi Support,
我正在尝试为更改Azure存储Blob层创建Azure自动化,但无法在下面的脚本中填充我的参数,但它在我的环境中不起作用 
<预类= "x_x_powershell" 风格= ""> <跨度类= "x_x_powerShell__keyword" 样式="颜色:#006699;字体 - 重量:粗体 "> PARAM 跨度>(&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP; [参数(强制性= <跨度类=" x_x_powerShell__variable"样式= "颜色:#aa7700"> $真< /跨度>)]鸟;&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP; [字符串]鸟<跨度类= "x_x_powerShell__variable" 样式= "颜色:#aa7700"> $ AzureSubscriptionID 跨度>&NBSP; =&NBSP; <跨度类= "x_x_powerShell__string" 样式= "颜色:蓝"> [使用&NBSP; *默认&NBSP;天青&NBSP;认购*&NBSP;变量&NBSP;值" 跨度>,&NBSP;&NBSP;
&NBSP; &NBSP;&NBSP;&NBSP; [参数(强制性= <跨度类= "x_x_powerShell__variable" 样式= "颜色:#aa7700"> $真跨度>)]鸟;&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP ; [String] $ StorageAccountName = " mystorageaccountname" ,
[参数(强制= $ true )]
[String] $ StorageTier = " Hot / Cool / Archive" ,
[参数(强制= $ true )]
[int] $ DaysOld
)
$ ServicePrincipalConnection = 获取 - 跨度> AutomationConnection&NBSP; <跨度类=" x_x_powerShell__operator"样式= "颜色:灰色"> - 跨度>名称&NBSP; <跨度类= "x_x_powerShell__string" 样式= "颜色:蓝">" AzureRunAsConnection'跨度>&NBSP;
$ null = 添加 - 跨度> AzureRmAccount&NBSP;`&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; <跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - 跨度> ServicePrincipal&NBSP;`&NBSP;
- TenantId $ ServicePrincipalConnection 跨度> .TenantId&NBSP;`&NBSP;
- ApplicationId $ ServicePrincipalConnection 跨度> .ApplicationId&NBSP;`&NBSP;
- CertificateThumbprint $ ServicePrincipalConnection 跨度> .CertificateThumbprint&NBSP;
# 选择 Azure 订阅
&NBSP;&NBSP;&NBSP;&NBSP;写<跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - 跨度>输出&NBSP; <跨度类= "x_x_powerShell__string" 样式= "颜色:蓝"> "选择&NBSP;天青&NBSP;订阅..." 跨度>&NBSP;
$ Subscription = 获取 - AzureRmSubscription - SubscriptionId $ AzureSubscriptionID - TenantId $ ServicePrincipalConnection 跨度> .TenantId&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP; <跨度类= "x_x_powerShell__alias" 样式= "颜色:#006699;字型重量:大胆">选择跨度> <跨度类= "x_x_powerShell__operator" 样式="颜色:grey"> - AzureRmSubscription - 订阅 $订阅跨度>&NBSP;
# 设置 存储 连接
<跨度类= "x_x_powerShell__variable" 样式= "颜色:#aa7700"> $ StgAcct 跨度>&NBSP; =&NBSP;获得<跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - AzureRmStorageAccount |
其中<跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - 跨度>对象&NBSP; {(<跨度类= "x_x_powerShell__variable" 样式= "颜色:#aa7700"> $ _ 跨度> .StorageAccountName - eq $ StorageAccountName )  ; <跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - 和&NBSP;(<跨度类= "x_x_powerShell__variable" 样式= "颜色:#aa7700"> $ _ 跨度> .sku.tier&NBSP ; <跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - 跨度>当量&NBSP; <跨度类= "x_x_powerShell__string" 样式= "颜色:蓝"> '标准'跨度>)&NBSP; <跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - 和&NBSP;(<跨度类= "x_x_powerShell__variable" 样式= "颜色:#aa7700"> $ _ 跨度> .Kind&NBSP; <跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - 跨度>当量&NBSP; <跨度类= "x_x_powerShell__string" 样式= "颜色:蓝"> 'StorageV2'跨度>&NBSP; <跨度类=" x_x_powerShell__operator"style ="color:gray"> - 或 $ _ .Kind - eq 'BlobStorage')}
写<跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - 跨度>输出&NBSP; <跨度类= "x_x_powerShell__string" 样式= "颜色:蓝">"存储&NBSP;帐户&NBSP;名称&NBSP;是:&NBSP; $($ StgAcct.StorageAccountName)" 跨度>&NBSP;
<跨度类= "x_x_powerShell__keyword" 样式= "颜色:#006699;字型重量:粗体">如果跨度>&NBSP;(<跨度类= "x_x_powerShell__variable" 样式= "颜色:#aa7700"> $ StgAcct 跨度>)&NBSP; {&NBSP;
$ StgAcctKey = (获取 - AzureRmStorageAccountKey - ResourceGroupName ( $ StgAcct ).ResourceGroupName&NBSP;`&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < span class ="x_x_powerShell__operator"style ="color:grey"> - 名称 ( $ StgAcct )。StorageAccountName).Value [0]鸟
$ StgAcctContext = 新 - AzureStorageContext - StorageAccountName ( $ StgAcct ) .StorageAccountName&NBSP;`&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; - StorageAccountKey $ StgAcctKey
<跨度类= "x_x_powerShell__variable" 样式= "颜色:#aa7700"> $ StorageContainers 跨度>&NBSP; =&NBSP;获得<跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - AzureStorageContainer - 上下文 $ StgAcctContext
<跨度类= "x_x_powerShell__com" 样式= "颜色:绿">#&NBSP;循环&NBSP;通过&NBSP;全部共个斑点&NBSP;在&NBSP;所述&NBSP;存储&NBSP;帐户跨度>&NBSP;
$ Blobs = @()
foreach ( $ StorageContainer 跨度>&NBSP; <跨度类= "x_x_powerShell__keyword" 样式= "颜色:#006699;字型重量:粗体">中跨度>&NBSP; <跨度类= "x_x_powerShell__variable" 样式= "颜色:#aa7700" > $ StorageContainers 跨度>){&NBSP;
<跨度类= "x_x_powerShell__variable" 样式= "颜色:#aa7700"> $斑点跨度>&NBSP; <跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> + 跨度> =&NBSP ;获取<跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - 跨度> AzureStorageBlob&NBSP; <跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - 跨度>上下文&NBSP; <跨度类= "x_x_powerShell__variable"style ="color:#aa7700"> $ StgAcctContext - Container ( $ StorageContainer 跨度>)名称&NBSP;
}
# 日期 逻辑
$ UTCDate = (获取-日期跨度>)ToUniversalTime()&NBSP;
$ RetentionDate = $ UTCDate .AddDays( - $ DaysOld )  ;
$ EarmarkedBlobs = $ Blobs < /跨度>&NBSP; <跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> | 跨度>&NBSP;其中<跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - 跨度>对象&NBSP; { $ _ .lastmodified.DateTime - le $ RetentionDate }
# 更改 blob
<跨度类= "x_x_powerShell__keyword" 样式= "颜色:#006699;字型重量:粗体"> FOREACH 跨度>(<跨度类= "x_x_powerShell__variable" 样式= "颜色:#aa7700"> $斑点跨度>&NBSP; <跨度类= "x_x_powerShell__keyword" 样式= "颜色:#006699;字型重量:粗体">中跨度>&NBSP; <跨度类= "x_x_powerShell__variable" 样式= "颜色:#aa7700" > $ EarmarkedBlobs 跨度>){&NBSP;
#设置所有 blob 以及所需的 层
$ blob .icloudblob.SetStandardBlobTier( $ StorageTier < /跨度>)&NBSP;
}
写<跨度类= "x_x_powerShell__operator" 样式= "颜色:灰色"> - 跨度>输出&NBSP; <跨度类= "x_x_powerShell__string" 样式= "颜色:蓝">"`n`nDone .. &NBSP;完成&NBSP;检查&NBSP;您的&NBSP;博客&NBSP;层级&NBSP;在&NBSP;所述&NBSP; portal.`n" 跨度>&NBSP;
} else {
<跨度类= "x_x_powerShell__cmdlets" 样式= "颜色:#006699;字型重量:粗体">写错误跨度>&NBSP;"你&NBSP;选择&NBSP;存储&NBSP;帐户&NBSP;不能&NBSP;被&NBSP;使用,&NBSP;&最NBSP;&可能NBSP;&因为NBSP;这是&NBSP;`&NBSP;
不&NBSP; A&NBSP; V2&NBSP;存储和NBSP;账户,&NBSP; A&NBSP; BLOB&NBSP;存储&NBSP;帐户&NBSP;或&NBSP;这是&NBSP;不&NBSP; A&NBSP;标准&NBSP;存储和NBSP;帐户];&NBSP;
}
您好PK_TECHNET,
想知道您要更改哪个Blog级别?热/冷/存档?
我建议尝试以下方法查看是否有效。例如。如果您要设置为存档。使用如下内容:
[String]
StorageTier= " Archive"
如果您还没有看到这个,请参阅
类似的MSDN帖子作为指导 - 相关答案剪下面。
Hi Support,
I am trying to create Azure Automation for Change Azure Storage Blob Tiers but unable to fill my parameters in below script and it is not working in my environment.
param( [parameter(Mandatory=$true)] [String] $AzureSubscriptionID = "Use *Default Azure Subscription* Variable Value", [parameter(Mandatory=$true)] [String] $StorageAccountName = "mystorageaccountname", [parameter(Mandatory=$true)] [String] $StorageTier = "Hot / Cool / Archive", [parameter(Mandatory=$true)] [int]$DaysOld ) $ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection' $null = Add-AzureRmAccount ` -ServicePrincipal ` -TenantId $ServicePrincipalConnection.TenantId ` -ApplicationId $ServicePrincipalConnection.ApplicationId ` -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint # Select Azure Subscription Write-Output "Select Azure subscription…" $Subscription = Get-AzureRmSubscription -SubscriptionId $AzureSubscriptionID -TenantId $ServicePrincipalConnection.TenantId Select-AzureRmSubscription -Subscription $Subscription # Setup the Storage Connection stuff $StgAcct = Get-AzureRmStorageAccount | Where-Object {($_.StorageAccountName -eq $StorageAccountName) -and ($_.sku.tier -eq 'Standard') -and ($_.Kind -eq 'StorageV2' -or $_.Kind -eq 'BlobStorage')} write-output "Storage Account Name is: $($StgAcct.StorageAccountName)" if ($StgAcct) { $StgAcctKey = (Get-AzureRmStorageAccountKey -ResourceGroupName ($StgAcct).ResourceGroupName ` -Name ($StgAcct).StorageAccountName).Value[0] $StgAcctContext = New-AzureStorageContext -StorageAccountName ($StgAcct).StorageAccountName ` -StorageAccountKey $StgAcctKey $StorageContainers = Get-AzureStorageContainer -Context $StgAcctContext # Cycle through all blobs in the storage account $Blobs = @() foreach($StorageContainer in $StorageContainers){ $Blobs += Get-AzureStorageBlob -Context $StgAcctContext -Container ($StorageContainer).Name } # Date Logic $UTCDate = (Get-Date).ToUniversalTime() $RetentionDate = $UTCDate.AddDays(-$DaysOld) $EarmarkedBlobs = $Blobs | Where-Object {$_.lastmodified.DateTime -le $RetentionDate} # Change the Tier on the blobs Foreach($Blob in $EarmarkedBlobs){ #Set tier of all blobs to desired tier $blob.icloudblob.SetStandardBlobTier($StorageTier) } Write-Output "`n`nDone... Complete. Check your Blog Tiers in the portal.`n" } else { Write-Error "Your selected Storage account cannot be used, most likely because it's ` not a V2 Storage account, a blob storage account or it's not a Standard Storage account" }
Hi PK_TECHNET,
Curious to know which Blog Tier you are trying to change to? Hot/Cold/Archive?
I suggest trying the below to see if it works. eg. if you are trying to set to Archive. use something like:
[String]
StorageTier= "Archive"
If you haven't seen this already, refer to this similar MSDN post for guidance - relevant answer snipped below.
这篇关于我正在尝试为更改Azure存储Blob层创建Azure自动化,但它无法正常工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!