如何使用 PowerShell 7 在 Azure 中获取任何 Blob 属性
How To Get Any Blob Property in Azure using PowerShell 7
我正在使用以下代码访问和检索 Azure 中的 blob 属性。我想让这个函数通用,这样我就可以用任何 'property' 名称调用它,而不是因为它在下面硬编码以仅检索“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
并检查 属性 是否存在(不为空)。
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 中的 blob 属性。我想让这个函数通用,这样我就可以用任何 'property' 名称调用它,而不是因为它在下面硬编码以仅检索“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
并检查 属性 是否存在(不为空)。
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
}