Get ResourceName based on Resource ID (GUID) - Azure - azure

With the Partner center Powershell module I can get all the info of my customers billing profile with the command Get-CustomerSubsctionUsage.
Now This command doesn't give back the Resource name. So I tried getting the name from Get-AzureRmResource which will provide the name , resourcegroupname , ... It even can do that based on an Id Get-AzureRmResource -ResourceId /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testRG/providers/Microsoft.Compute/virtualMachines/testVM But The problem is, The Id that I can provide from the usage data is not the same as the id that the command needs.
I tried to get the resource uri from another command inside the partner center module. But because I really need to have the first command for the cost data. It was hard to get the resourceUri per resource I found inside the first command. That's why now I just want to get the resource name from the AzureRm module. But It can't seem to find any right way to get it properly. The reason why i want to do this is because the resourceID or the resource name that the partner center module provided is not presentable on a power bi report
TL;DR : I want to get the Azure Resource name based on the Resource GUID

Similar question has been asked on this forum related to Azure resource GUID and billing.
GUID(Resource Id) in this usage data does not refer to the Id of any resource like Virtual machine or storage. Look like its merely deployment GUID
ref.
How to get resource name from Azure Resource GUID?
https://social.msdn.microsoft.com/Forums/vstudio/en-US/01695990-a309-4a07-9c7e-e2cf0acfa3d6/resource-guid-in-azure-portal?forum=windowsazurepurchasing

Related

KQL Query to retrieve Azure Subscription Name, Resource Group, Resource Name, Resource Type, Tags and Location

I would like to retrieve all the resource names along with their types belonging to a particular subscription and resource group, along with the tags of the resources.
I should be able to dump them in a CSV file where the first column would be subscription, then resource group followed by resource name, type and tags. I should be able to filter the CSV as to what i need to see.
I need to run this for all my subscriptions in a particular tenant so that i get this information for all subscriptions in my tenant.
Can anyone please help me writing a KQL query for this so that i can run from the portal.
Thanks
had a similar challenge with KQL to provide a user friendly names for Subscriptions in Azure Workbooks. I found a solution on link
The trick is to list the subscriptionnames from the table resourcecontainers and then join the results with your resources query
The answer to your question will look like this:
resources
| join kind=inner (
resourcecontainers
| where type == 'microsoft.resources/subscriptions'
| project subscriptionId, subscriptionName = name)
on subscriptionId
| project subscriptionName, resourceGroup, name, type, tags
Using KQL in Azure Resource Graph is actually an ideal way to retrieve this information. You can run the KQL queries from the Azure Portal using Resource Graph Explorer then export (or use PowerShell with the Search-AzGraph cmdlet and pipe to Export-Csv).
Resource Graph allows queries to the ARM graph backend using KQL, which is an extremely powerful and preferred method to access Azure configuration data. All subscriptions in the tenant are in scope by default (if checked off).
Please review Resource Graph concepts and query samples in Microsoft's docs:
Explore your Azure resources with Resource Graph
Starter Resource Graph query samples
Advanced Resource Graph query samples
Query below; if you choose to export all subscriptions and RGs at once just remove the subscriptionId and resourceGroup where clauses:
resources | where subscriptionId == "subscription-id-here" | where resourceGroup == "rg-name-here" | project subscriptionId, resourceGroup, name, type, tags
Yes, #Ivan is right. KQL is certainly not meant for this purpose. Kusto query language, or KQL is the primary means of interaction with Azure Data Explorer and work with log data on Azure.
The simplest way to get this information about your Azure resources is from the Azure Portal itself, by viewing and filtering Azure resource information.
As your query spans across your Subscriptions, you could also run queries from Azure Resource Graph.
Azure PowerShell and Azure CLI would be other great ways to get detailed information about your Azure resources. Here is another post with a similar ask.

Get Azure Storage Account Key inside Powershell Function App

I am trying to get the Key of a Storage Account from inside a Powershell Function App under the same Resource Group "rg-mobileplans".
I am certain I have the correct Azure Context and when listing all the Storage Accounts I see the one I am trying to retrieve the key from - "stmobileplansstaging".
Write-Host ((Get-AzContext).Subscription)
Write-Host (Get-AzStorageAccount -ResourceGroupName rg-mobileplans).StorageAccountName
Output:
<Subscription ID Hidden for Privacy>
stmobileplansstaging storageaccountrgmob83d8
But when I try to get the key itself I get an error message that the Storage Account Could not be found.
Get-AzStorageAccountKey -ResourceGroupName rg-mobileplans -AccountName stmobileplansstaging
Output:
ERROR: Get-AzStorageAccountKey : The Resource 'Microsoft.Storage/storageAccounts/stmobileplansstaging ' under resource group 'rg-mobileplans' was not found. For more details please go to https://aka.ms/ARMResourceNotFoundFix
Keep in mind that I am running these commands from a Function App. Running them from Powershell works.
I have created a System Managed Identity for the Function App and gave it "Owner" access to the entire "rg-mobileplans" Resource Group and the "stmobileplansstaging" Storage Account.
What am I missing?
I just realized what the problem is. In the script the storage account name is given through a tag. The name had a space at the end. When running the get / list storage account commands it's OK and the commands succeed because the name of the account is probably at the end, but for getting the key, underneath a POST call is being made and the URL looked something like "http://.../storage/stmobileplansstaging /rest-of-the-uri". Notice the space in the middle of the URI which broke everything.

How to Get-AzPublicIpAddress based on ResourceId?

Deployment script needs to enumerate existing public IP addresses from public IP prefix. Public IP Prefix object contains an array of resource identifies of individual public IP address.
I'd like to retrieve individual public ip addresses using provided resource identifier. Something like this:
Get-AzPublicIpAddress -ResourceId $resourceId;
Unfortunately, such signature doesn't exist. Get-AzPublicIpAddress expects ip address name as input parameter.
I understand that I can do:
Call Get-AzResource and get needed information from returned object [it means extra network call]
Parse needed information out of resource identifier [would like to avoid implementing this logic in PowerShell]
Question - are these the only options? Or maybe Az provides a built-in way of parsing resource identifiers?
You can use Get-AzResource -resourceId xxx -ExpandProperties | fl * for such requests. its a generic cmdlet that will work for any resource.
How is calling Get-AzResource an extra network call, compared to Get-AzPublicIpAddress? Its 1 call vs 1 call.
No, Az doesnt provide any parsing capabilities, they are not needed. You have tools to use resource id as well as individual names.
getting resource name out of resource id is fairly easy:
$resourceId -split '/' | Select-Object -Last 1
I don't think you can pass the -ResourceId directly to the command, all the built-in parameters are here : https://learn.microsoft.com/en-us/powershell/module/az.network/get-azpublicipaddress?view=azps-1.4.0. The most close way I can find is your option 2.
Not sure why you want to use Get-AzPublicIpAddress via ResourceId, even if we use -Name and -ResourceGroupName i.e. Get-AzPublicIpAddress -Name <publicIpName> -ResourceGroupName <ResourceGroupName>, it essentially also passes them into the request url of the rest api which the command called.
GET https://management.azure.com/subscriptions/xxxxxxxxx/resourceGroups/joywebapp/providers/Microsoft.Network/publicIPAddresses/joyVM-ip?api-version=2018-10-01
Actually, you could find /subscriptions/xxxxxxxxx/resourceGroups/joywebapp/providers/Microsoft.Network/publicIPAddresses/joyVM-ip is the ResourceId.
So I think it should be not too difficult for Microsoft to add the -ResourceId as a bulit-in parameter of the Get-AzPublicIpAddress command, if you want to improve it, you could give the feedback here.
Update:
Microsoft has replied to the this issue, see : https://github.com/Azure/azure-powershell/issues/8704#issuecomment-470604852
thanks for opening this feature request -- to provide more insight as to why the issues you mentioned above were closed: all new cmdlets that we ship in Az must conform to the pattern of having parameter sets that allow the user to do the following:
Provide the components of a resource (e.g., resource group name, resource name, etc.)
Provide the resource id of a resource
Provide the object representation of the resource (some cmdlets won't use this, like Get-*)
Later this year, we will begin generating our cmdlets using AutoRest (see this blog post for more details), and the above patterns will be enforced in the generator. Our goal then is to generate cmdlets for existing Azure services and replace our existing cmdlets with the generated ones.

Is there a way to get which Log Analytics Workspace an Azure Resource belongs to using Powershell?

I'm looking for a way to obtain which OMS workspace my APIM Services resource is logging to using PowerShell. I want to then configure my Application Gateway to log to the same workspace.
When I go to "Logs" under the APIM service, it shows all available workspaces in my subscription.
I'm not sure how to get just the workspace that it is logging to.
This is not really a PowerShell Code errors / functionality issue, but how do I find and match up my AzureRM resources and extract information.
However, since you are here...
If the property is not there, then you are not looking at the correct resource cmdlet / object. I don't have this type of scenario deployed in my sub, so no way to dig at it. Take a look at this
Workspaces - Get
URI Parameters Name In Required Type Description
subscriptionId path True string Gets subscription credentials
which uniquely identify Microsoft Azure subscription. The subscription
ID forms part of the URI for every service call. resourceGroupName
path True string The resource group name of the workspace.
workspaceName path True string Name of the Log Analytics
Workspace. api-version query True string Client Api Version.
And use the provided properties to cross reference, specifically the resourceGroupName and or workspaceName.

Azure Windows VM - how can I find out what Resource Group it is in?

I have C# code running on an Azure Windows VM. Is there a way for me to find out what Resource Group this VM is in?
VM has been deployed with Azure Resource Management API (new, not classic)
The following will guarantee the ability to distinguish between vm's with the same name across different resource groups:
From your C# code, find the vmId (involves running one of the commands at the following link or possibly using an Azure SDK: https://azure.microsoft.com/en-us/blog/accessing-and-using-azure-vm-unique-id/). If using a Linux VM, be sure to take into account the different endian-ness, otherwise the vmId will not match.
Once you have the vmId, you can use either CLI or Powershell (or potentially an Azure SDK) to list all of the VM's in the subscription, then search through the list to find which VM has the vmId you got from the machine. Then you should be able to parse out the resource group name from the "id" field of the json for that VM (which, as Gaurav mentioned, is a string with the resource group in it). For an example, try the following:
azure vm list --json -vv
This command will show you the url's it is using to make the requests and the response body. In this body you will find the "vmId" and "id" field. For instance, one of the requests it sends is:
https://management.azure.com/subscriptions/{my-subscription-id}/providers/Microsoft.Compute/virtualMachines?api-version=2015-06-15
and the response body for this is the json with the relevant entries. Hope this helps! :)
One way to find out the resource group is to list all the virtual machines in your Azure Subscription. The URL you would use for that would be:
https://management.azure.com/subscriptions/[subscription-id]/providers/Microsoft.Compute/virtualMachines?api-version=2015-06-15
It will return you a list of all Virtual Machines in your Azure Subscription in JSON format where each item represents a Virtual Machine. You can first filter by the name property to find the matching Virtual Machine. Then the property which is important to you there is id which is always of the format:
/subscriptions/[subscription-id]/resourceGroups/[resource-group-name]/providers/Microsoft.Compute/virtualMachines/[virtual-machine-name]
You could simply parse this to get the resource group name.
How did you deploy the VM? Through portal.azure.com? Through CLI? Powershell? In any of these cases, usually you are required to specify a resource group name. In fact, in the portal, if you click on "Virtual Machines", it should say the resource group:
resource group of VM in portal
write-host(Get-AzVm -name "hostname").ResourceGroupName

Resources