gpt4 book ai didi

azure - 如何使用 PowerShell 7 获取 Azure 中的任何 Blob 属性

转载 作者:行者123 更新时间:2023-12-03 05:34:21 25 4
gpt4 key购买 nike

我使用以下代码来访问和检索 Azure 中的 blob 属性。我想让这个函数变得通用,这样我就可以使用任何“属性”名称来调用它,而不是像下面硬编码的那样仅检索“IsServerEncrypted”属性:

function GetBlobProperty   {
Param(
[parameter(Mandatory = $true)] [String] $blobProperty, # <<<<<=I want to retrieve any property
[parameter(Mandatory = $true)] [String] $storageAccountName,
[parameter(Mandatory = $false)] [String] $storageAccountKey,
[parameter(Mandatory = $false)] [String] $containerName,
[parameter(Mandatory = $false)] [String] $blobName
)
$ctx = GetStorageContext $storageAccountName $storageAccountKey
$Blobs = Get-AzStorageBlob -Container $containerName -Context $ctx
$retValue = ""

ForEach ($Blob in $Blobs){
#Write-Host $Blob.Name
if($Blob.Name.IndexOf($blobName) -ge 0)
{
Write-Host $Blob.Name
$retValue = $Blob.ICloudBlob.Properties.IsServerEncrypted #I want to pass $blobProperty here
break;
}


}
return $retValue
}

谢谢!

最佳答案

您可能只需执行 $Blob.ICloudBlob.Properties.$blobProperty 并检查该属性是否存在(不为 null)。

function Get-BlobProperty {
Param(
[parameter(Mandatory = $true)] [String] $blobProperty,
[parameter(Mandatory = $true)] [String] $storageAccountName,
[parameter(Mandatory = $false)] [String] $storageAccountKey,
[parameter(Mandatory = $false)] [String] $containerName,
[parameter(Mandatory = $false)] [String] $blobName
)
$ctx = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$Blobs = Get-AzStorageBlob -Container $containerName -Context $ctx
$retValue = $null

ForEach ($Blob in $Blobs){
#Write-Host $Blob.Name
if($Blob.Name.IndexOf($blobName) -ge 0)
{
Write-Host $Blob.Name
if ($null -ne $Blob.ICloudBlob.Properties.$blobProperty) {
$retValue = $Blob.ICloudBlob.Properties.$blobProperty
break;
}
}


}
return $retValue
}

虽然我更喜欢使用 Get-Member检查该属性是否存在:

function Get-BlobProperty   {
Param(
[parameter(Mandatory = $true)] [String] $blobProperty,
[parameter(Mandatory = $true)] [String] $storageAccountName,
[parameter(Mandatory = $false)] [String] $storageAccountKey,
[parameter(Mandatory = $false)] [String] $containerName,
[parameter(Mandatory = $false)] [String] $blobName
)
$ctx = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$Blobs = Get-AzStorageBlob -Container $containerName -Context $ctx
$retValue = $null

ForEach ($Blob in $Blobs){
#Write-Host $Blob.Name
if($Blob.Name.IndexOf($blobName) -ge 0)
{
Write-Host $Blob.Name

if (Get-Member -InputObject $Blob.ICloudBlob.Properties -Name $blobProperty -MemberType Property) {
$retValue = $Blob.ICloudBlob.Properties.$blobProperty
break;
}
}
}
return $retValue
}

关于azure - 如何使用 PowerShell 7 获取 Azure 中的任何 Blob 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63637048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com