Get-AzureRmResource showing up the deleted resources also - powershell

I am creating a powershell script using Azure CLI where I am using Get-AzureRmResource command to fetch all the resources in a subscription.
This command is giving the details of deleted resources also , which are not longer in Azure portal as well. How can I get only those resources which are present in the portal

I have reproduced in my environment and got deleted results as you have got as below and I followed Microsoft-Document:
Then I used below command and then I have only got resources which are not deleted as below:
Get-AzResource | Where {$_.Name -Notlike 'failure*'} | ft
If you are still getting, then it might be soft deleted or not deleted, try to delete them and try the above command.

Related

How do I get a VM Image from Azure in Powershell?

Previously in Powershell I was using the Get-AzureVMImage cmd-let to retrieve a VM Image I had generated. I would store the image an as object to build a VM with New-AzureQuickVM. Below is a code snippet:
$image = Get-AzureVMImage | where { $_.Imagename -like $basicImageNames[$n] } | Sort-Object -Descending -Property PublishedDate | Select-Object -First 1 -OutVariable image
Now with the new AzureRM Powershell Module, the majority of the cmd-lets for Powershell management have been renamed or deprecated. Get-AzureVMImage.
I've attempted to use Get-AzureRmImage, but it doesn't list any of my VM Images.
I've also attempted to use Get-AzureRmVMImage, but this appears to be valid only for official published images, and not my user generated ones.
Does anyone know of an equivalent cmd-let to the deprecated Get-AzureVMImage?
According to your describe, I think you have created a Classic Image ,right?
As I known, Classic image can only be used to create classic VM. Also, you cannot using Azure RM powershell to get a Classic Image and to create a classic VM.
So, I suggest you can go to Azure portal > VM images(classic)> find your generated Image>Create VM . Or you can also use Azure SM powershell to login your Azure Classic Account and then use Get-Azure VM image.
See more details about Get-AzureVMImage in this document.
See more details about Get-AzureRmImage in this document.

Remove-AzureRmResourceGroup powershell cmdlet appears stuck

I am trying to delete a previously created RM resource group with powershell. The Remove-AzureRmResourceGroup command seems forever and control is not returned to the console. Following is the script used to delete the resource group.
Login-AzureRmAccount
$resourceGroupName="dummyResourceGroup"
Get-AzureRmSubscription –SubscriptionName "Visual Studio Enterprise: BizSpark"| Select-AzureRmSubscription
Remove-AzureRmResourceGroup -Name $resourceGroupName
I tried the following steps
Attempt to delete again from Azure portal, but the resource group had
delete button disabled with message "Deleting" in a orange bar
In a new powershell window Get-AzureRmResourceGroup -Name
$resourceGroupName command comes up with status
ResourceGroupName : dummyResourceGroup
Location : australiaeast
ProvisioningState : Deleting
attempt to create another resourcegroup with same name comes up with message
New-AzureRmResourceGroup : The resource group 'dummyResourceGroup' is in deprovisioning state and cannot perform this operation.
Its been almost 3 hours and I suspect that the operation is errored out and will not complete. Any pointers on how to resolve this are greatly appreciated.
Note: This is not exactly a solution to the problem, but the steps I took that got me out of the situation.
In the portal I noticed an alert about service disruption in australiaeast region which lasted for about 2 hours when I first started the deployment.
I continued to face the issue even after 12 hours. I was unable to delete the Resource Group either from portal nor from powershell
I gave up and created a new ResourceGroup and as if by magic the old ResourceGroup got deleted.

Showing all Assets in Azure portal, Azure Powershell

Is there a commandlet in Azure Powershell for showing all current assets being used in Azure , including subscriptions,VM's,webapps, storage accounts...etc so we can generate an updated report at anytime ?
If you're using Resource Manager (read New Portal), you can try the following to get a list of all the resources at the subscription level.
Get-AzureRmResource | Export-Csv -Path "C:\temp\all-resources.csv"
You will need to run this for all Subscriptions for which you want to fetch the data.

Can't get Move-AzureResource working

I'm trying to move some of my resources (Azure Web Apps, Azure SQLs, Redis caches) from one resource group to another. I'm using the Azure Resource Manager PowerShell cmdlets.
Here's what I've tried:
PS C:\> Move-AzureResource -DestinationResourceGroupName NewResourceGroup -ResourceId "/subscriptions/someguid/resourceGroups/Default-Web-WestEurope/providers/Microsoft.Web/sites/somesite"
Or:
PS C:\> Get-AzureResource -ResourceName somesite | Move-AzureResource -DestinationResourceGroupName NewResourceGroup
Or:
just Move-AzureResource, hitting enter and supplying the parameters one by one.
None of the commands seems to work. They just don't do anything. No error, no output.
When I changed the debug preference to $DebugPreference = "Continue" I got only the following:
DEBUG: 12:16:06 - MoveAzureResourceCommand begin processing with ParameterSet '__AllParameterSets'.
DEBUG: 12:16:06 - using account id 'my#account.tld'...
Please note that I'm able to create a new resource group (New-AzureResourceGroup), list resource groups (Get-AzureResourceGroup), list resources (Get-AzureResource), etc.
Note: you have to call Switch-AzureMode AzureResourceManager before you can use the cmdlets. The authentication is done by Add-AzureAccount.
Articles I've been referring to:
Moving resources between Azure Resource Groups
Move-AzureResource
Using Azure PowerShell with Azure Resource Manager
GitHub - Using Azure PowerShell with Azure Resource Manager
Reading this azure forum it looks like they have implemented the cmdlet but not all resources support being moved yet.
We have released a new powershell cmdlet to move resources across resource groups. Not all resources have support yet, but the "main" ones do like hosted services, virtual machines & storage accounts.
Looking back at the example I was following, this does only use VM's. So based on this I think websites aren't supported yet. That fact that no error or warning is returned for unsupported resources is a bit poor.
Though not all resources are currently supported, I understand the current version - 0.9.1 - does have a bug which means that even a supported resource may not be moved with the symptoms as seen by the author of the question. I understand this is being worked on for the next release, but in the interim (as a temp. work around) the previous powershell cmdlets release of 2 versions ago should work fine. https://github.com/Azure/azure-powershell/releases
The original issue is fixed in the 0.9.4 release. I just tried and it works.
FYI. To move a VM using Move-AzureResourceGroup you need to move the containing cloud service and all its VMs at the same time. For example:
Get-AzureResource -ResourceGroupName OriginalResourceGroup | where { $_.ResourceType -match 'Microsoft.ClassicCompute' } | Move-AzureResource -DestinationResourceGroupName NewResourceGroup
By default, the resources in a cloud service are put in a resource group with the same name as the DNS name of the cloud service.
For some reason, Azure PowerShell Version 1.0 has trouble moving over web apps from one Resource Group to another. If you follow the instrctions below, you will be able to move the web app over via powershell.
Download Azure PowerShell Version 1. The below instructions only work for this version. Type the commands below in order.
1) **Login-AzureRmAccount** (a login window will pop up asking for your azure credentials, type them in)
2) **Get-AzureRmResource -ResourceGroupName "NameOfResourceGroup" -ResourceName "WebAppName"** (if you are moving over a website, you will see 2 files, you need the one that is a resource type of Microsoft.Web/sites)
3) **Get-AzureRmResource -ResourceGroupName "NameOfResourceGroup" -ResourceName "WebAppName" -ResourceType "Microsoft.Web/sites"**
4) Assign value 3 to a variable of your name choice. I Chose $a, so **$a = Get-AzureRmResource -ResourceGroupName "NameOfResourceGroup" -ResourceName "WebAppName" -ResourceType "Microsoft.Web/sites"**
5) **Move-AzureRmResource -DestinationResourceGroup "DestinationResourceGroup" -ResourceId $a.ResourceId**
6) It will ask you if you are sure type "Y" and hit enter.

Remove-AzureDisk throws error, not sure why

I have an Azure VM and I'm trying to delete it using Powershell. I also want to remove the disk that that VM OS was on (there are no data disks).
I assume I'm going to need the following two cmdlets:
Remove-AzureVM
Remove-AzureDisk
Here's my code:
$VMs = Get-AzureVM $svcName
foreach ($VM in $VMs)
{
$OSDisk = ($VM | Get-AzureOSDisk)
if ($VM.InstanceStatus -eq "ReadyRole") {
Stop-AzureVM -Name $VM.Name -ServiceName $svcName
}
remove-azurevm -ServiceName $svcName -Name $VM.Name
Remove-AzureDisk -DiskName $OSDisk.DiskName
}
When I execute this the call to Remove-AzureVM returns successfully but the call to Remove-AzureDisk returns an error:
Remove-AzureDisk : BadRequest: A disk with name
XXX is currently in use
by virtual machine YYY running within hosted service
ZZZ, deployment XYZ.
Strange thing is, I can issue the same call to Remove-AzureDisk just a few moments later and it returns successfully.
Its as if the call to Remove-AzureVM is returning too quickly. i.e. Its reporting success before the VM has been fully removed, or before the link to the disk has been removed at any rate.
Can anyone explain why this might be and also how I might work around this problem?
thanks in advance
Jamie
What's happening here is that the Disk that is stored in BLOB storage is locked when in use by a VM. You are removing the VM, but it takes a few moments for the Lease on the BLOB to release. That's why you can remove it a few moments later.
There are a few folks who have written PowerShell to break the lease, or you could use PowerShell to use the SDK (or make direct REST API calls) to check lease status.
I ended up writing a script that creates a VM, clones it, then deletes the clones. As part of that I needed to wait until the lease was released hence if you're experiencing this same problem you might want to check my blog post at http://sqlblog.com/blogs/jamie_thomson/archive/2013/11/04/clone-an-azure-vm-using-powershell.aspx as it'll have some code that might help you.
Regards
Jamie
I puzzled at this for quite a while. Ultimately, I found a different command to do what I thought I was doing with this command. I would recommend the remove-azuredatadisk command to delete a disk, as it automatically breaks the lease.
Get-AzureVM -ServiceName <servicename> -name <vmname> |Remove-AzureDataDisk -lun <lun#> -deletevhd | Update-AzureVM
It will spin for a couple of minutes, but it will give you a success/failure output at the end.
This command just does it, and doesn't give you any feedback about which drive was removed. I would recommend tossing in a get-azuredatadisk first just to be sure of what you deleted.
Get-AzureVM -ServiceName <servicename> -name <vmname> | Get-AzureDataDisk
This is related to Windows Azure: Delete disk attached to non-existent VM. Cross-posting my answer here:
I was unable to use the (2016) web portal to delete orphaned disks in my (classic) storage account. Here is a detailed walk-through for deleteing these orphaned disks with PowerShell.
PowerShell
Download and install PowerShell if you haven't already. (Install and configure Azure PowerShell.) Initial steps from this doclink:
Check that the Azure PowerShell module is available after installing:
Get-Module –ListAvailable
If the Azure PowerShell module is not listed, you may need to import it:
Import-Module Azure
Login to Azure Resource Manager:
Login-AzureRmAccount
AzurePublishSettingsFile
Retreive your PublishSettingsFile.
Get-AzurePublishSettingsFile
Get-AzurePublishSettingsFile launches manage.windowsazure.com and prompts you to download an XML file that you can be saved anywhere.
Reference: Get-AzurePublishSettingsFile Documentation
Import-AzurePublishSettingsFile and specify the path to the file just saved.
Import-AzurePublishSettingsFile -PublishSettingsFile '<your file path>'
Show and Remove Disks
Show current disks. (Reference: Azure Storage Cmdlets)
Get-AzureDisk
Quickly remove all disks. (Credit to Mike's answer)
get-azuredisk | Remove-AzureDisk
Or remove disks by name. (Credit to Remove-AzureDisk Documentation)
Remove-AzureDisk -DiskName disk-name-000000000000000000 -DeleteVHD