Find Instance ID for Azure Servers - azure

In AWS we have instance IDs to identify machines. What's the equivalent in Azure and how do I find that?
I'd like to list the instance ID in powershell so I can put that into a script.

If I have not misunderstood, I suppose you want to get the InstanceId of the VM in the Azure VM Scale set.
You could try to use Get-AzureRmVmssVM to get it.
Get-AzureRmVmssVM -ResourceGroupName <ResourceGroupName> -VMScaleSetName <VMScaleSetName>
Update:
If you want to get the ResourceId of azure resource, you could use Get-AzureRmResource to do it, just specify the properties what you want.
For example:
1.Get ResourceId of the specific resource:
$resource = Get-AzureRmResource -ResourceGroupName <ResourceGroupName> -ResourceType <ResourceType> -ResourceName <ResourceName>
$resource.ResourceId
2.Get ResourceIds of all resources in the specific resource group
$resource = Get-AzureRmResource -ResourceGroupName <ResourceGroupName>
$resource.ResourceId
3.Get ResourceIds of all resources under your subscription.
$resource = Get-AzureRmResource
$resource.ResourceId
The result will like(actual result will decided by your properties):

Related

Get all APIs from Microsoft.ApiManagement/service together with TAGS by REST API

I'm tring to find a way how to get APIs stored on Azure in Microsoft.ApiManagement/service
I have tried different endpoints but none gave mi API with tags. Is there a way to do so?
Thank You!
Below are the PowerShell cmdlets for getting the list of APIs registered in an APIM Instance along with their individual API tags:
$ResourceGroupName = "resource_group_name"
$APImg = "APIM_Instance_Name"
$ApiMgmtContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $APImg
$Apis = Get-AzApiManagementApi -Context $ApiMgmtContext
foreach($Api in $Apis){
$ApiId = $Api.ApiId
$ApiName = $Api.Name
$tags = (Get-AzResource -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.ApiManagement/service/apis/tags -ResourceName "$APImg/$ApiId" -ApiVersion 2018-01-01).Name
Write-Host "Tags of" $ApiName : $tags
}
Result:
To get the list of APIs along with their tags using the REST API, below is the syntax:
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apisByTags?api-version=2021-08-01
Thanks to Microsoft Docs as they provided the sample response how it lists the APIs along with their tags for the above cmdlet:
Refer to the Azure API Management Rest APIs for more information.

How to get Storage Account containers' role assignments from Access Control tab

i am trying to get all objects from Access Control tab from Storage Account container using powershell.
Using command:
Get-AzRoleAssignment -ResourceGroupName 'devtest' -ResourceName 'sa-name' -ResourceType 'Microsoft.Storage/storageAccounts'
I am get all objects from:
Storage Account
Containers
As you can see, using this command im getting scopes for Storage Account and Container in the same call.
I tried using command like:
Get-AzRoleAssignment -ResourceGroupName 'devtest' -ResourceName 'SA-name' -ResourceType 'Microsoft.Storage/storageAccounts' | Where-Object -Property Scope -Like '*containers/container-name'
But i am not happy with the results because i am not getting for example Owner of this container because it is inheritate from diffrent resource
My question is, how to get Role Assignments objects from IAM blade for specific container using powershell, CLI or REST API with all objects?
You can list the RBAC on a specific container by listing all role assignments for the storage account and excluding all containers except for the one you want to see:
Get-AzRoleAssignment -ResourceGroupName "<your-resource-group-name>" -ResourceType "Microsoft.Storage/storageAccounts" -ResourceName "<your-storage-account-name>" | Where-Object { $_.Scope -like '*/containers/<your-container-name>' -or -not ($_.Scope -like '*/storageAccounts*/default/containers/*') }

How to fetch the Cost/Month for the VMSize using AZ Powershell?

I need to fetch the Cost/Month for the VMSize using AZ Powershell.
In the Azure Portal, We can get all the details of Cost/Month,IOPS,vCPU,RAM,etc... when we select the VMSize.
Using cmdlet "Get-AZVMSize -Location "" we get only VMSizeName, no.ofCores,MemoryinMB,MaxDataDiskCount,OSDiskSizeinMB,ResourceDiskSizeinMB only.
The Cost is not coming out. Any AZ Powershell cmdlet which can provide me the Cost/Month for the VMSize based on location?
There is no such cmdlet Out of Box for cost as such. Here is the PowerShell script that uses Get-AzConsumptionUsageDetail to get cost of a VM:
$costTotal=0
$RGName ="RGNAME"
$StartDay="2019-12-01"
$EndDay="2019-12-06"
$VMName="vm1"
$Consumption = Get-AzConsumptionUsageDetail -ResourceGroup $RGName -StartDate
$StartDay -EndDate $EndDay -InstanceName $VMName
$Costs = $Consumption.PretaxCost
foreach ($Cost in $Costs) { $CostTotal += $Cost}
$CostTotal

Automation to delete unused VMs and their resources in Azure by last power on date

I need to create an automation that will delete VMs that were not started in the last two weeks and their associated resources (for example a Network interface or a Disk etc..) inside a single resource group. I thought about using a Powershell runbook in an automation account but I have some problems with that, I couldn't find a Powershell command to check last start date of all VMs in a resource group or a Powershell command to delete a VM and all its' associated resources.
If I had these two I could make a Powershell runbook that will check last start time of a VM and if the date exceeds two weeks it'd automatically delete it and its' associated resources.
Anyone knows how to accomplish these two things or maybe knows a different way to do this?
there is no easy way to do that (so no cmdlet that would do either of things you require). You'd need to script those 2 operations.
You'd probably need to use Get-AzVm and parse the output to figure out when was it powered on (not sure this is even exposed in the api) along with something like this https://adamtheautomator.com/remove-azure-virtual-machine-powershell/
I went through some searching on this and ended up creating this script that does the job:
$rgName = "resource group name"
$VMs = Get-AzVM -ResourceGroupName $rgName | ? {$_.Tags.Keys -notcontains "DontDelete"}
#$VMs = $VMs | ? {$_.Name -eq 'ePO'}
foreach ($VM in $VMs)
{
$vmName = $VM.Name
$vmID = $VM.Id
Get-AzVM -VMName $vmName | Stop-AzVM -Force
####################################################################################
$nicID = $VM.NetworkProfile.NetworkInterfaces.id
####################################################################################
$diskID = $VM.StorageProfile.OsDisk.ManagedDisk.Id
$snapshotConfig = New-AzSnapshotConfig -SourceUri $diskID -Location $VM.Location -CreateOption copy
$snapshot = New-AzSnapshot -Snapshot $snapshotConfig -SnapshotName "$vmName-snapshot" -ResourceGroupName $VM.ResourceGroupName
####################################################################################
Remove-AzResource -ResourceId $vmID -Force
Remove-AzResource -ResourceId $nicID -Force
Remove-AzResource -ResourceId $diskID -Force
}
Decided that instead of last powered on date I'll use a "DontDelete" tag for the VMs I don't want to delete and the rest will be deleted as well as their associated resources.
I added this script to a runbook in an automation account and ran it and it works perfectly.

Hot to get Azure Search Service query key

I need list query keys from Microsoft.Search/searchServices using PoweShell or API.
What I have investigate till now is:
Get-AzureRmResource -ResourceType "Microsoft.Search/searchServices/listQueryKeys" -ResourceGroupName 'resource-group-name' -ResourceName 'resource-name' -ApiVersion '2015-08-19'
Returns the array of objects with only names:
#{Name=a}
#{Name=query-key-created-from-powershell}
I have not found the API to get query keys.
There is easy way to get admin key:
Invoke-AzureRmResourceAction -Action listAdminKeys -ResourceType "Microsoft.Search/searchServices" -ResourceGroupName 'resource-group-name' -ResourceName 'resource-name' -ApiVersion 2015-08-19
But no way I can get QueryKeys. This is not working:
Invoke-AzureRmResourceAction -Action listQueryKeys -ResourceType "Microsoft.Search/searchServices" -ResourceGroupName 'resource-group-name' -ResourceName 'resource-name' -ApiVersion 2015-08-19
The docs refer only to create or delete Query Key.
How can I get Query Keys as name-key collection?
Try to use the following cmdlet:
Get-AzureRmSearchQueryKey -ResourceGroupName "resourceGroupName" -ServiceName "serviceName"
https://learn.microsoft.com/en-US/powershell/module/azurerm.search/get-azurermsearchquerykey?view=azurermps-6.11.0
Check that you have the correct version of Azure PowerShell.
To get the query key of the Azure Search service, #Victor Silva 's solution will work fine.
Get-AzureRmSearchQueryKey -ResourceGroupName "resourceGroupName" -ServiceName "serviceName"
I have download in my AzureDevOps task 'xxxx' 6.11.0 but AzureRM.Search is not part of it
To your further issue, because the AzureRM.Search module is in the preview version, it is not be included in AzureRM 6.11.0 module, refer to the Package Details in this link. So if you want to use this command Get-AzureRmSearchQueryKey, you need to install the AzureRM.Search independently, use Install-Module -Name AzureRM.Search -AllowPrerelease, refer to this link.
Update:
If you want to list query keys via API, you could use this REST API.
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Search/searchServices/{searchServiceName}/listQueryKeys?api-version=2015-08-19

Resources