Home > Blockchain >  How to find and Delete orphan public ip in azure using powershell
How to find and Delete orphan public ip in azure using powershell

Time:05-25

How to list and remove unused (orphanip) public ip address "such as search if the ip is not associated to any Vm or Networkinterface card find and then delete" in azure using powershell azure automation runbook.

CodePudding user response:

You can utilise Get-AzNetworkInterface to return all NICs within your current context.

You would have to filter the results to see which were not attached to a virtual machine.

# This will return NICs which aren't associated to a VM
$orphanedNics = Get-AzNetworkInterface | Where-Object VirtualMachine -eq $null

If you have a lot of resources to check then you could use Search-AzGraph from the Az.ResourceGraph module to perform the search.

$query = '
Resources
| where type has "microsoft.network/networkinterfaces"
| where properties !has "virtualmachine"'

$orphanedNics = Search-AzGraph -Query $query

Once you have those results and validated it's correct you can then use Remove-AzNetworkInterface to delete.

CodePudding user response:

Deleting Managed Disks with PowerShell

Run the following PowerShell commands in sequence:

  1. Set the value of the following variable to 0 if you just want to view the id of the disk. Set it to 1, if you want the disk to be deleted

$deleteOrphanedDisks=1

  1. Get details of managed disk using the following command

$managedDisks = Get-AzDisk

  1. Check for ManagedBy property associated with the disk. The value is null if the managed disk is not attached to the VM. Depending on the value set for $deleteOrphanedDisks, the disks will either be deleted or the Ids of the disks will be listed

`foreach ($disk in $managedDisks) {

if($disk.ManagedBy -eq $null){

    if($deleteOrphanedDisks -eq 1){

        Write-Host "Deleting orphaned Managed Disk with Id: $($disk.Id)"

        $disk | Remove-AzDisk -Force

        Write-Host "Deleted orphaned Managed Disk with Id: $($disk.Id) "

    }else{

        $disk.Id

    }

}
}

Deleting Unmanaged Disks with PowerShell

  1. Set the value of the following variable to $false if you just want to view the id of the disk. Set it to $true, if you want the disk to be deleted.

It is recommended to run the script with initially the value set to $false to see the disks and then run the commands again with value set to $true for deleting the disks:

$deleteOrphanedDisks=$false

  1. Get details of storage account by entering:

$storageAccounts = Get-AzStorageAccount

  1. Run the following code to get details of all page blobs with extension .vhd that can be used as unmanaged disks.The code will check the LeaseStatus property of the page blob. The status for orphaned disks that can be deleted will be unlocked:

`$storageKey

$containers = Get-AzStorageContainer -Context $context

foreach($container in $containers){

  $blobs = Get-AzStorageBlob -Container $container.Name -Context $context

  $blobs | Where-Object {$_.BlobType -eq 'PageBlob' -and $_.Name.EndsWith('.vhd')} | ForEach-Object { 

      if($_.ICloudBlob.Properties.LeaseStatus -eq 'Unlocked'){

              if($deleteOrphanedDisks){

                  Write-Host "Deleting unattached VHD with Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"

                  $_ | Remove-AzStorageBlob -Force

                  Write-Host "Deleted unattached VHD with Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"

              }

              else{

                  $_.ICloudBlob.Uri.AbsoluteUri

              }

      }

  }
  }

For further refer here and # Find and Delete Orphaned Public IP addresses in Azure Portal for more information.

  • Related