Reputation: 611
I am using the below code to access and retrieve blob properties in Azure. I want to make this function generic so that I can call it with any 'property' name instead of as it is hard coded below to retrieve only the "IsServerEncrypted" property:
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
}
Thanks!
Upvotes: 0
Views: 641
Reputation: 26315
You could probably just do $Blob.ICloudBlob.Properties.$blobProperty
and check if the property exists(not 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
}
Although I prefer using Get-Member
to check if the property exists:
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
}
Upvotes: 2