Orphan NICs state in VM turnoff mode

2022-07-21T11:33:40.143+00:00

Hi Team,

Please suggest me on the below usecase to get the Orphan NICs in azure.

I am having some VMs in Azure and attached the NICs and Disks to the VM.Here When VM power off it will get delete from azure based on auto scale process.Here issue is when vm is poweroff then NICs of the vm are in Orphan state.
Please suggest any process to know identify real unused NICs.

Thanks
Satya

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,773 questions
0 comments No comments
{count} votes

Accepted answer
  1. KapilAnanth-MSFT 49,611 Reputation points Microsoft Employee Moderator
    2022-07-22T06:04:54.063+00:00

    Hi @Venkata satyanarayana Machari veera (CIS) ,

    Welcome to the Microsoft Q&A Platform. Thank you for reaching out & I hope you are doing well.
    I understand that you are looking for a way to delete the orphaned NICs.

    I see @Pierre-Luc Giguere has shared the Microsoft Learn on how to achieve this.
    https://learn.microsoft.com/en-us/previous-versions/azure/virtual-machines/linux/find-unattached-nics

    The only challenge in the mentioned CLI script is that this will also list the NICs associated with private End Point.
    Anyways, it will throw an error and will not delete these NICs (private endPoint), and will continue to delete the rest of the NICs
    If you rarely have to clean up the NICs, you can go ahead with the CLI script.

    Now, if you don't want to list the private EndPoint NICs, you can use the below PowerShell script.

    To View the NICs
    $nics=Get-AzNetworkInterface -ResourceGroupName "YourResourceGroupName" | Where-Object {($.Name -notmatch '.nic.') -and ($.VirtualMachine -eq $null)}
    $WarningPreference = 'SilentlyContinue'
    $nics | Format-Table -Property Name, ResourceGroupName, Location, Id

    To Delete the NICs
    $nics=Get-AzNetworkInterface -ResourceGroupName "YourResourceGroupName" | Where-Object {($.Name -notmatch '.nic.') -and ($.VirtualMachine -eq $null)}
    $WarningPreference = 'SilentlyContinue'
    foreach ($nic in $nics)
    {
    $WarningPreference = 'SilentlyContinue'
    Remove-AzNetworkInterface -Name $nic.Name -ResourceGroupName $nic.ResourceGroupName
    }

    Reference :
    https://learn.microsoft.com/en-us/powershell/module/az.network/remove-aznetworkinterface?view=azps-8.1.0

    P.S :

    • The script works on the fact that Private EndPoint NICs, by default have ".nic." on their name.
    • If you are creating a NIC that has ".nic." in it's name, then the above script will omit this NIC in the listing.
    • However, I do not think this would be the case for the majority of the times

    Kindly let me know if this helps, In case there are follow-up questions, please do let us know.

    Thanks,
    Kapil

    ----------------------------------------------------------------------------------------------------------------

    Please don’t forget to close the thread by clicking "Accept the answer" wherever the information provided helps you, as this can be beneficial to other community members.


1 additional answer

Sort by: Most helpful
  1. Pierre-Luc Giguere 1,076 Reputation points
    2022-07-21T12:53:49.757+00:00

    Hi,

    Please note that like most peope here, I do not work for Microsoft.

    This article is from 2018 but I still use a similar script on a regular basis

    https://learn.microsoft.com/en-us/previous-versions/azure/virtual-machines/linux/find-unattached-nics

    Hope it helps.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.