How can i verify that some Storage Account name (received as input) belongs to some specific subscription
$storageAccountName = <something from input>
$storageAccountKey = <something from input>
$subscriptionName = "MySubscription"
$subscription = Get-AzureSubscription -SubscriptionName $subscriptionName
$StorageAccountContext = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
Now what?
how can i verify that the given storage account really belongs to this specific subscription?
(In case it doesn't i want to exit the script and now allow to continue.)
For me the answer to this is: Which resource group is it in, and in which subscription is the resource group then located:
$sa = Get-AzureRmStorageAccount -Name <input storage account name>
$sa
StorageAccountName ResourceGroupName Location SkuName Kind AccessTier CreationTime ProvisioningState EnableHttpsTrafficOnly
------------------ ----------------- -------- ------- ---- ---------- ------------ ----------------- ----------------------
sa001 rg080 westeurope StandardLRS StorageV2 Hot 02/09/2019 15:16:15 Succeeded False
Get-AzureRmResourceGroup -Name $sa.ResourceGroupName -Location $sa.Location
ResourceGroupName : rg080
Location : westeurope
ProvisioningState : Succeeded
ResourceId : /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/rg080
From there on you will be able to match the resourceid with the subscriptionid that you can find from
get-azurermsubscription
Something like so:
(Get-AzureRmResourceGroup -Name $sa.ResourceGroupName -Location $sa.Location).ResourceId.Split('/')[2] -like (Get-AzureRmSubscription | where name -like $subscriptionName).SubscriptionId
Acccording to my test, we can get all the storage account name in one subscription then we ensure if these names contain the name you provide. For example:
Connect-AzAccount
$SubscriptionName=""
$Subscription=Get-AzSubscription -SubscriptionName
Select-AzSubscription -Subscription $Subscription
#get all storage account name in the subscription
$name = Get-AzStorageAccount | Select-Object StorageAccountName
$StorageAccountName="test"
$value=$name.StorageAccountName.IndexOf($StorageAccountName)
if($value -ne -1) {
Write-Host " exist"
}else {
Write-Host "does not exist"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.