简体   繁体   中英

How to get subscription id from the VM in AZURE

Is there a way to get the subscription id from the running (LINUX)VM instance in AZURE?

Can WALinuxAgent read the subscription ID from the internal server ?

This can be achieved using the Azure Instance Metadata Service . Calling this service from your VM will return a JSON with SubscriptionId among other useful data. Sample Microsoft bash script for calling the metadata service (with updated version in the request):

sudo apt-get install curl
sudo apt-get install jq
curl -H Metadata:True "http://169.254.169.254/metadata/instance?api-version=2017-08-01&format=json" | jq .

See "Response" section in provided link for sample response, with subscriptionId.

You can use powershell to achieve this. First of all. What kind of VM deployment model?

ARM

In this case it very simple.

$vm = Get-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName
$vm.Id

You'll see - "/subscriptions/{subscriptionId}/..."

Classic

If you know resource group VM was deployed to, use following:

$resource = Get-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.ClassicCompute/virtualMachines -Name $vmName
$resource.ResourceId

Same - you"ll see "/subscriptions/{subscriptionId}/..."

Way to find resourceGroupName, if unknown (in case you write some automative script):

$vm = Get-AzureVM | Where {$_.Name -eq $vmName}
$service = Get-AzureService -ServiceName $vm.ServiceName
$service.ExtendedProperties.ResourceGroup

Hope it helps

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM