I need to get property like "DiskState", I am using cmd-let Get-AzureRMdisk, and what I got:
1) from my laptop, using connect-azurermaccount then get-azurermdisk, doesnt give expected property 2) I tried from Automatio account, created a runbook and then paste my code there, the same, I got more values but didn't get "diskstate" value 3) I see that I can get it from Azure Cloud Shell console. I would like to get this value from a script running as a runbook. Moreover I tried to use command like --> Get-AzureRmDisk | Where-Object ManagedBy -ne $null, but didnt get any interesting results. Is there any matheto do get this mentioned property "diskstate" using powershell runbook from Automation account?
Why not, the PowerShell command Get-AzureRMDisk show you the DiskState as you want:
Additional, I recommend you use the Az module of the PowerShell while the AzureRM module is Migrated to it. See Migrate Azure PowerShell from AzureRM to Az.
Update:
But in the Runbook Get-AzureRMDisk does not show the property, you need to use the Get-AzDisk to achieve the purpose after you import the Az module and it shows like this:
Related
I want to get app insights of all the subscriptions available in the portal but whenever I run the script
$resources = az monitor app-insights component show | ConvertFrom-Json
I get app insights only for the same subscription every time , even during the time when I change the subscription through the script
Set-AzContext -SubscriptionName "some-name"
the whole script goes like this
Set-AzContext -SubscriptionName "some-name"
$resources = az monitor app-insights component show | ConvertFrom-Json
So even if I change the subscription name to something else suppose "some-name1"
still I am getting the app-insights for subscription "some-name"
This is by design.
While you could switch the context in a script, searches across multiple subscriptions are easier and much, much faster using the Resource Graph.
PowerShell Query:
Search-AzGraph -Query "resources | where type =~ 'Microsoft.Insights/components'"
Azure CLI Query:
az graph query -q "resources | where type =~ 'Microsoft.Insights/components'"
Both options should get you all Application Insights resources across your tenant.
For more details, please see the Starter Resource Graph query samples.
On a side note I would also recommend to stick to either Azure CLI or Az PowerShell. While the choice of language is personal preference, sticking to one of the two decreases the dependencies. If you stick to Azure CLI, the only prerequisite is having the Azure CLI binaries installed. If you stick to Az Modules in PowerShell, you don't need Azure CLI but only the Az Modules. Mixing both makes the code more difficult to port to other machines.
So, if using the Az Modules was preferred, instead of...
$resources = az monitor app-insights component show | ConvertFrom-Json
I would recommend:
$resources = Search-AzGraph -Query "resources | where type =~ 'Microsoft.Insights/components'"
The issue you're experiencing with the Set-AzContext command is that it only sets the subscription context for the current PowerShell session.
The az monitor app-insights component show command is running in a separate process or thread, so it is not able to see the updated subscription context set by the Set-AzContext command.
To work around this, you can pass the -Subscription parameter to the az monitor app-insights component show command, like so:
$resources = az monitor app-insights component show --subscription "some-name1" | ConvertFrom-Json
This will ensure that the az command is running with the correct subscription context, and you will get the app insights for the correct subscription.
How can I write a PowerShell script to get the report of all azure VMs in an excel spreadsheet, which includes the All tags associated with that, RG, and subscription?
You can implement the Powershell Script by following steps:
Install az modules in your local machine (Install-Module -Name 'Az' -allowclobber -scope currentuser)
Connect to your Azure subscription using this cmd (connect-Azaccount).
Use Get-AzVm Get-AzVM (Az.Compute) | Microsoft Docs to get the list of VM under the subscription and assign the output to a variable and by using the for each loop about Foreach - PowerShell | Microsoft Docs iterate the list in the variable and project the output to csv file using the export-csv.
I am trying to nuke an azure subscription and I found this
https://www.frankysnotes.com/2016/12/need-to-nuke-azure-subscription.html
As I run the final part I got an error of
"Find-AzureRmResource : The term 'Find-AzureRmResource' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again."
Can someone help me how to fix this so I can nuke my Azure subscription?
Instead of using AzureRM module, use Az module as the former is now deprecated. To learn more about it, please see https://learn.microsoft.com/en-us/powershell/azure/migrate-from-azurerm-to-az?view=azps-7.5.0.
Simplest code to delete all resources from a subscription would be to list resource groups in that subscription and then delete them.
Your code would be something like:
Get-AzResourceGroup | Remove-AzResourceGroup
Please use Get-AzureRmResource instead of Find-AzureRmResource.
Find-AzureRmResource is depreacted.
Starting with version 6.0 of Azure PowerShell, Find-AzureRmResource have been removed and Get-AzureRmResource is supposed to be the workaround.
How to safely replace Find-AzureRmResource -ResourceType calls in Azure PowerShell 6.x+
I'm attempting to enable accelerated networking in Azure from the Azure Powershell Az module. However, I get an error stating that 'No registered resource provider found for location "my location" and API version "2019-07-01"'
I've found that the latest API version available is 2019-06-01. I was able to get a list of API versions, but they are in date format and the Az Install-Module seems to only accept numbers such as 2.2.0.
((Get-AzResourceProvider -ProviderNamespace Microsoft.Network).ResourceTypes | Where-Object ResourceTypeName -eq networkInterfaces).ApiVersions
The code that generates the error:
$nic = Get-AzNetworkInterface -ResourceGroupName "myResourceGroupName" -Name "myNicName"
I'm looking for a way via Az powershell cmdlets or a web site reference to get the actual version number so I can install the correct version to interface with my Azure environment.
The answer here is to use Cloud Shell directly from the Azure portal. This is no longer a coding question so the community can feel free to do what they want with this.
Check for your Powershell and AzModule versions. The Current API version available is '2019-08-01':
Update your Powershell/AzModule and the command will work just fine. Refer to the documentation:
https://learn.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-2.7.0#requirements
We use powershell script to start and stop the VMs using the build job. Please see the screenshot of the build job below
It is a simple powershell script to start an VM. The issue is when this job runs, we are getting an error and the build fails.
We get Parameter set cannot be resolved using the specified named parameters
But when we run it locally using the powershell console, the VMs get started.
Please find the error screenshot below
Am I missing something here.. Any help would be very much appreciated.
EDIT 1
Powershell script
$machines = #("machinename")
Select-AzureSubscription -Default "XXXYYYZZZ"
Foreach ($machine in $machines)
{
Try
{
Start-AzureVM -ServiceName $machine -Name $machine
}
Catch
{
}
}
I check your screenshot, I find you want to stop classic VMs. However, you logon Azure with cmdlet Add-AzureRmAccount and select subscription with cmdlet select-azurermsubscription, am I right? The two cmdlets are ARM mode cmdlets, you should use classic mode cmdlets.
Add-AzureAccount
According to your error, you could not use Select-AzureSubscription -Default "XXXYYYZZZ". Default could not add parameters. More information please refer to this link.
Try to use the following cmdlets.
Get-AzureSubscription
Select-AzureSubscription -SubscriptionName <YourSubscriptionName> -Default