Azure Advisor Recommendations using PowerShell - azure

Get-AzAdvisorRecommendation -Category Cost | Where-Object {$_.ImpactedField -eq "Microsoft.Compute/virtualMachines"}
I'm trying to get the Cost Property for Virtual MAchines on "Shut down or resize your virtual machine" but not able to get it similar with the Data shows in portal
Automate the advisor recommendations using PowerShell.

The command seems right. I don't have this Azure advice myself, but I can look for
Get-AzAdvisorRecommendation -Category Cost |
Where-Object {$_.ImpactedField -eq "Microsoft.Network/publicIPAddresses"}
and it gives me the corresponding advice. This should also work with "Microsoft.Compute/virtualMachines".
Are you sure the error is displayed when you omit the pipe?

Get-AzAdvisorRecommendation -Category Cost | Where-Object {$_.ImpactedField -eq "Microsoft.Compute/virtualMachines"} | Select-Object -ExpandProperty ExtendedProperties
I got the required answer with the query..
Thanks Casper!

Related

How to identify untagged resources forr tag name and value using azure powershell

I am looking for a PowerShell script for getting the list of resources not having specific tagname/value in Azure, am having the cmdlet for this as I mentioned below, but don’t know how to create a complete script using functions, parameters etc.
Please help.
Many thanks,
$Tags = #{"environment"="Terraform Demo"}
Get-AzResource | Where-Object $Tags -eq $null | Select-Object -Property Name, ResourceType
Please help me with this logic correct me if this not required. Thanks in advance.
If you want to receive resources that do not have any tags configured at all, you could use this query:
Get-AzResource | Where-Object {$_.Tags -eq $null}
If you want to receive resources that have the environment tag set to $nulll, you could use this query:
Get-AzResource | Where-Object {$_.Tags.environment -eq ''}
Contrarily, if you want to find resources that do not have the tag combination "environment" = "Terraform Demo" you could use:
Get-AzResource | Where-Object {$_.Tags.environment -ne 'Terraform Demo'}
You might also want to check switching to Resource Graph queries, since you would not have to change the context when searching across multiple subscriptions.
Checking for resources that do not have an environment tag configured would work like this:
Search-AzGraph -Query "Resources | where tags.environment=~'' | project name"
The latter requires the Az.ResourceGraph module. See here for details: https://learn.microsoft.com/en-us/azure/governance/resource-graph/samples/starter?tabs=azure-powershell#list-tag

Collect Azure Netapp Volume metrics

Right now I'm viewing the Azure NetApp volume metrics using Azure Portal metrics dashboard.I can see only one month old data. I'm planning to collect this data and save into SQL table. So that I have the history of this data (i.e. more than 30 days). Is there a powershell commands that I can use?
enter image description here
As per Azure NetApp Files: PowerShell One-Liners, you can use Get-AzMetric, provide StartTime and EndTime to get the history of data.
Try the following code snippet taken from the document, for example:
Get-AzResource | Where-Object {$_.ResourceType -like 'Microsoft.NetApp/netAppAccounts/capacityPools/volumes'}
| Get-AzNetAppFilesVolume | Select-Object #{Name='ShortName'; Expression={$_.Name.split('/')[2]}}, #{Name='SizeGiB';
Expression={$_.UsageThreshold / 1024 / 1024 / 1024}},
#{Name='ConsumedGiB';
Expression={[math]::Round($((Get-AzMetric -ResourceId $_.Id -MetricName 'VolumeLogicalSize'
-StartTime $(get-date).AddMinutes(-15) -EndTime $(get-date) -TimeGrain 00:5:00 -WarningAction SilentlyContinue
| Select-Object -ExpandProperty data | Select-Object -ExpandProperty Average) | Measure-Object -average).average / 1024 / 1024 / 1024, 2)}} | Format-Table
You can refer to PowerShell and CLI for Azure NetApp Files and Azure NetApp Files metrics

How to get the status of a list of VM in Azure using Powershell Get-Azvm

I am trying to get a list of Azure vm statuses (Allocated,Deallocated). I am able to get it one at a time for a single vm. But when i have a list of VM's or when using wildcard's it fails to get the vm status. Any tips
$ResGrp= "resvmtest"
$action="start"
$vmList = Get-AzVM -ResourceGroupName $ResGrp -Name * -Status
foreach($vm in ($vmList | Select-Object #batch)){
Write-Host $vm.Statuses[1].DisplayStatus
}
I see that you were trying to display the VMs in batches. Unless you have VMs in the thousands, that's not necessary. I also no longer see the Statuses property in Get-AzVM cmdlet. Just try this:
$vmList = Get-AzVM -ResourceGroupName $ResGrp -Status
$vmList | Select-Object Name, PowerState, ProvisioningState # or any other properties you want to display
Either that or I'm not understanding your question.
Just try this :
Get-AzVm -ResourceGroupName <rg name> -Status | Select-Object Name, PowerState
This behaviour appears to have changed. Using the Status parameter now creates an object named Status and within that object.
One way to show the output:
select Name, #{n='New';e={($_.Statuses | where Code -eq 'PowerState/running').DisplayStatus}}

What am I Missing in PowerShell in Azure Automation that prevents Start-AzureVM from working?

I have the following PS script that runs fine locally:
Get-AzureVM | Where-Object { $_.Name -eq "my-server-selector" } | select name | ForEach-Object {
Write-Output $_.Name
Start-AzureVM $_.Name $_.Name
}
In the context of my local PS console, I add my subscription info and the code executes without a problem; all VMs are printed to the output and the servers are started up.
When I move it to the cloud I need to do a few other things, namely, bring the subscription in scope. I do that by creating the credential asset in the portal, adding the account to my script via said credentials, then selecting the correct subscription in the script. I also wrap it in a workflow (there are aspects I intent to parametrize at a later date).
The final code is as follows:
workflow StartServer
{
$credential = GetAutomationPSCredential -Name "credential-asset-name"
Add-AzureAccount -Credential $credential
Select-AzureSubscription -SubscriptionName "subscription-name"
Write-Output "Starting the server."
Get-AzureVM | Where-Object { $_.Name -Contains "my-server-selector" } | select name | ForEach-Object {
Write-Output $_.Name
Start-AzureVM $_.Name $_.Name
}
Write-Output "Execution Complete."
}
If I remove the Start-AzureVM command, the workflow runs as expected. I get a listing of all the matching VMs printed out. If I attempt to put the command back in, I get the following error:
Parameter set cannot be resolved using the specified named parameters.
So, things I think I know:
the credentials are working as I'm getting the correct list of VMs
the subscription is being correctly set, as it's dumped to the output
the inner part of the script works on a local powershell console without any changes
Can anyone provide any ideas as to what needs to be done differently in an Azure Automation workflow to get this to work?
The fix was to be more explicit in the naming of parameters, both in the filter for the Where-Object as well as in the call to Start-AzureVM. I'm not sure why this would make a difference; as I said, the call to write the names of the servers worked without the explicit parameter name, but low and behold, here it works with it set.
The final code of the inner block is as follows:
Get-AzureVM | Where-Object -FilterScript { $_.Name -Contains "my-server-selector" } | select name | ForEach-Object {
Write-Output $_.Name
Start-AzureVM -ServiceName $_.Name -Name $_.Name
}
Thanks to #DexterPOSH on Twitter for the direction on -FilterScript.
Please take a look at http://azure.microsoft.com/blog/2014/11/25/introducing-the-azure-automation-script-converter/ which talks about this exact issue. When authoring Powershell in the ISE to move into Azure Automation, make sure you are testing / writing as Powershell Workflow in the ISE, since Powershell workflow has some differences vs Powershell script.
Or, if you need to take PS script and use it in Azure Automation, make sure you import the script, not copy paste it in. Azure Automation will then convert the PS script to PS Workflow for you. The link above has more details on this.

Start-AzureVM: Getting struck continously

Can anyone please tell me if there are any change for "Start-AzureVM" API?
It is getting struck with following command
Start-AzureVM -ServiceName (get-azurevm | where {$_.name -eq $VMName}).ServiceName -NAME $VMName
Also help me on where to look for latest azure API changes?
Make sure that you select a valid Azure Subscription using Select-AzureSubscription cmdlet before you use Start-AzureVM cmdlet.
When you do not have a valid Azure Subscription, there is a potential chance for all other Azure cmdlets behave awkwardly like you mentioned in your question.

Resources