Getting Azure VM Instance ID from within a VM and linking it with imported Azure VM instances - azure

I am developing a client-server application where I want the client (Azure VM) to send its instance ID to the server. The server will use this instance ID to link it with the corresponding VM from the list of imported Azure instances (using Azure cloud credentials).
I am facing some problems:
I am using the web endpoint which the client talks to , to get its instance ID. However, as mentioned in the article (https://azure.microsoft.com/en-us/blog/what-just-happened-to-my-vm-in-vm-metadata-service/), I get empty info returned. How do I get the instance ID?
From the imported VMs, I don't get any attribute which is the instance ID but rather just instance Name and Private ID along with a bunch of other information.
I didn't have this problem for AWS as I clearly get instance ID on both sides.

Maybe you could use something like:
curl -H Metadata:true "http://169.254.169.254/metadata/instance/compute/vmId?api-version=2017-08-01&format=text"
As described here:
https://learn.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service

Which instance ID is this? (i.e. how do you get the list of ID's and what is their format?) Is it the one mentioned in the article below? If so, then the article below shows how to get it :).
https://azure.microsoft.com/en-us/blog/accessing-and-using-azure-vm-unique-id/

How are You acquiring this data, by means of which tools (xPlat Cli, PowerShell, MAML, REST API...?) maybe look here too https://azure.microsoft.com/en-us/blog/accessing-and-using-azure-vm-unique-id/

Related

Subscription-id, resourceGroupName and name of the App from inside the web-app PowerShell

I have an application hosted in Azure PAAS. The connection string for the application is stored under 'Configuration' -> 'Connection strings'
My application has a PowerShell instance. I want to iterate through all the Connection strings present under 'Configuration' -> 'Connection strings'
I have seen the Azure document. As my application itself is the app, can there be a way to skip the details like 'subscriptionId', 'resourceGroupName' and 'name'?
This will help to make the code more generic.
As my application itself is the app, can there be a way to skip the
details like 'subscriptionId', 'resourceGroupName' and 'name'?
AFAIK, Its not possible to acquire the connection strings using Rest API, or PowerShell of an Azure web application without providing Resource group name or subscription.
The MS DOCUMENT you have followed is to list the connection strings which is correct but we need to pass those credentials to achieve the same.
If my understanding is correct as its your own application and if its publicly hosted then anyone will not be able to get the resource group name, application name(If you are using custom domain) or subscription details.
Alternatively, we can use the Az cli by providing the resource group only :-
For more information please refer the below links:-
SO THREAD|Get the list of azure web app settings to be swapped using PowerShell
If you are going to use the REST API calls for your code, then the simple answer is just: No.
I think in all cases the answer is going to be no honestly..
You can't drop those unique IDs, because those are required parameters to retrieve the correct data.
If you want to make the code more generic, then you should write the code to retrieve the values for those parameters. Instead of hardcoding the values.
Your powershell code will always need to authenticate, or use a Managed Identity, and the identity used to authenticate will always have the subscriptionid as value in its object. As for the rest, well i think you get the gist of what im suggesting.

How to retrieve App Service Plan instance name (RDXXXXX)?

I am having an issue trying to retrieve all the instance names in an Azure App Service Plan.
In Azure Monitor, if you specify a "Scope" to "App Service Plan" and look at the Metric "CPU Percentage" and then add a filter to specify the "Instance" property, you can see which instance uses the most CPU. I am trying to do a PowerShell script to get these values. Unfortunatly I have not found any Azure REST Api that would give me this information so that my script would be 100% dynamic. I looked at the AzureRm or Az PowerShell modules but did not find anything there.
Any ideas how I can retrieve this list? The instances names looks like this :RD123456.
Thanks for you help !
Suppose you want to get the web instance name, if that's right you could get it from instance process with Web Apps - Get Instance Process.
And under the environment_variables there is a COMPUTERNAME suppose this is what you want.

Cross-resource query with app() expression from the portal, “The following application isn’t available anymore”

Is it possible to use the cross-resource query (with 2 subscriptions) from the an Application-Insights resource's, Logs(Analytics) blade in the Azure portal?
I've managed to use the full qualified name for the same app I'm using!
i.e. using
app("/subscriptions/sub1/resourceGroups/rg1/providers/microsoft.insights/components/apptelemetry1").requests in apptelemetry1 returns the same result as the query requests. But trying to use a similar query for an app in another subscription doesn't work (I've double checked the resources names many times).
I've Tried using the all of the options listed in app-expression,
cross-workspace-query.
(Resource name, Qualified Name, ID, Azure Resource ID).
The only response I'm getting is: "The following application isn’t available anymore".
Since you cannot use this query app("/subscriptions/sub1/resourceGroups/rg1/providers/microsoft.insights/components/apptelemetry1").requests in the application insights instance apptelemetry1 itself, there must be some problem with the instance apptelemetry1.
You can check if you have the read access to the application.
And if it's still cannot after assign read access, you can submit a support ticket where you can get professional help from Microsoft.

Get VM name for an instance of Azure web app from REST API or PowerShell

I am trying to find a REST API / PowerShell cmdlet to retrieve the VM name(starts with RD) for an instance of Azure web app.
I found out that I can list the process Ids by Instance Id and then get Instance Process with Azure App Service. This object has details including the Machine Name. I'm wondering if there is a better way of doing this, something with lesser API calls perhaps.
Background:
I need to restart a specific instance of a Azure Web app programmatically. Users calling restart can only see the VM name or Role Instance of a web App in Application insights.
I found APIs to list instances but nothing to find mapping between instance Id and VM name directly.
Do you try this API Virtual Machines - List?
By Using this api, you can get id and name from values[]
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines?api-version=2018-06-01
Result example :
{
"value": [
{
// ignore
"id": "/subscriptions/{subscriptionsID}/resourceGroups/test-resource-group/providers/Microsoft.Compute/virtualMachines/my-vmname",
"name": "my-vmname"
}
]
}
You can try to use the REST API Web Apps - List Instance Identifiers to get all scale-out instances of an app.
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/instances?api-version=2016-08-01
Actually, the instances for a WebApp by scaling out is for its App Service Plan. For example, I scaled up the instance number of my webapp to 4 on Azure portal, then the instance number of its App Service Plan as the figures below.
And I got the json response from the REST API, as below.

Get Instance Count of Azure Web App with C#

I've been looking around and I can't find a concise example around getting this metric. I've installed WindowsAzure.Management.Compute and now I don't know where to begin. How can this be done?
I would recommend you to take a look at Azure Resource Explorer: https://resources.azure.com. You could find the instances request URL as following:
For the Authorization header please have a look at this article. We can use C# HttpRequest to write the code. Here is the result I tested in fildder.
the endpoint:
https://management.azure.com/subscriptions/<subscription id>/resourceGroups/jatestgroup/providers/Microsoft.Web/sites/testcore1/instances?api-version=2015-08-01
Result:
We can calculate the instance number from the response json. In addition, it will need a long time in this rest API to show all instances when scale the instance in azure portal.
[Update]
According with my comment, I tested with the article:http://blog.amitapple.com/post/2014/03/access-specific-instance/#.V9tLKyh95hF
The following is my result:
Please download the library at here. Refer to this article for more information about Windows Azure Management Certificates.
Here is the code snipped:
var cert = new X509Certificate2();
cert.Import(Convert.FromBase64String(""));
var _client = new Microsoft.WindowsAzure.Management.WebSites.WebSiteManagementClient(new Microsoft.WindowsAzure.CertificateCloudCredentials("****", cert));
var ids= await _client.WebSites.GetInstanceIdsAsync("EastAsiawebspace", "testcore1");
You cannot get the instance count from within one instance of the Web App. But you can get it from the portal, or from the Azure ARM API (it's the numberOfWorkers property on the Web Hosting Plan object).
Also, note that WindowsAzure.Management.Compute does not apply to Azure Web App.

Resources