Remove-AzVMDisksNICsPIPs.psm1

<#
 .Synopsis
  Deletes the selected Azure VM, and associated NICs, Public IP Addresses, Disks.
  
 .Description
  Deletes the selected Azure VM, and associated NICs, Public IP Addresses, Disks. It creates a log file in C:\Users\Username\AppData\Local\Temp\
  
 .Parameter ResourceGroupName
  Name of the Resource Group
  
 .Parameter Name
  name of the Virtual machine
  
 .Example
   # Deletes the selected Azure VM, and associated NICs, Public IP Addresses, Disks.
   Remove-AzVMDisksNICsPIPs -ResourceGroupName "name of the ResourceGroup" -Name "name of the Virtual machine"
 
 .Example
   # Deletes the selected Azure VM, and associated NICs, Public IP Addresses, Disks without any additional prompts
   Remove-AzVMDisksNICsPIPs -ResourceGroupName "name of the ResourceGroup" -Name "name of the Virtual machine" -Force
    
#>



#------------------------------------------------------------------------------
#
# THIS MODULE AND ANY ASSOCIATED INFORMATION ARE PROVIDED 'AS IS' WITHOUT
# WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
# FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR
# RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#
#------------------------------------------------------------------------------


Function Remove-AzVMDisksNICsPIPs ()
{

Param(

    [Parameter(Mandatory=$true,
    ValueFromPipelineByPropertyName=$true)]
    [String]$ResourceGroupName,

    [Parameter(Mandatory=$true,
    ValueFromPipelineByPropertyName=$true)]
    [String]$Name,

    [Parameter(Mandatory=$false)]
    [Switch]$Force

)


$a = Get-Random -Maximum 9999 -Minimum 1

$ResourceGroup = $ResourceGroupName
$VMName = $Name

$vm = Get-AzVM -ResourceGroupName $ResourceGroup -Name $VMName



Start-Transcript -Path "$env:temp\DeleteAzVM-$a-$VMName.log" -NoClobber


write-host "
#------------------------------------------------------------------------------
#
# THIS MODULE AND ANY ASSOCIATED INFORMATION ARE PROVIDED 'AS IS' WITHOUT
# WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
# FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR
# RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#
#------------------------------------------------------------------------------
Running this module will cause deletion of resources, which cannot be recovered. Run this module at your own risk.
"
 -ForegroundColor Red


if(!$VM){Break}

Else{


#NIC
$nicname = @()
$nicname += $vm.NetworkProfile.NetworkInterfaces.id | Split-Path -Leaf
$nicRG = @()
$nicRG += $vm.NetworkProfile.NetworkInterfaces.id | ForEach-Object {$_ -replace ".*/resourceGroups/" -replace "/.*"}

$GetNIC = @()
For ($i=0;$i -lt $nicname.Count; $i++){
$GetNIC += Get-AzNetworkInterface -Name $nicname[$i] -ResourceGroupName $nicRG[$i]
}
$Ipconfname = $getnic.IpConfigurations.id


# PiP Check
$PiPName = @()
$PiPRG = @()
if(!$getnic.IpConfigurations.publicipaddress.id){
Write-Host "No Public IP Found"}
Else{

$PiPName += $getnic.IpConfigurations.publicipaddress.id | Split-Path -Leaf
$PiPRG += $getnic.IpConfigurations.publicipaddress.id | ForEach-Object {$_ -replace ".*/resourceGroups/" -replace "/.*"}

$getpip = @()
for($i=0;$i -lt $PiPName.count; $i++){
$getpip += Get-AzPublicIpAddress -Name $PiPName[$i] -ResourceGroupName $PiPRG[$i]
}}


#Disks:
$OSDisk = $vm.StorageProfile.OsDisk.Name
$dataDisk = $vm.StorageProfile.DataDisks.name

#Disks RG
$OSDiskRG = $vm.StorageProfile.OsDisk.ManagedDisk.Id | ForEach-Object {$_ -replace ".*/resourceGroups/" -replace "/.*"}
$dataDiskRG = $vm.StorageProfile.DataDisks.ManagedDisk.Id | ForEach-Object {$_ -replace ".*/resourceGroups/" -replace "/.*"}
 
#Disk Details
$disk = @()
$disk += $OSDisk

if(!$dataDisk){$null
}
Else{
$disk += $dataDisk
}


$diskRG = @()
$diskRG +=  $OSDiskRG

if(!$dataDiskRG)
{$null}
Else{$diskRG +=$dataDiskRG}

$diskIDs = @()
$diskIDs += $($vm.StorageProfile.OsDisk.ManagedDisk.Id),$($vm.StorageProfile.DataDisks.ManagedDisk.Id)

$getdisk = @()
#$disk | ForEach-Object {Get-AzDisk -DiskName $_ | Remove-AzDisk }
For($i=0;$i -lt $disk.count; $i++){
$getdisk += Get-AzDisk -ResourceGroupName $diskRG[$i] -DiskName $disk[$i]
}


#"Following Items will be deleted press.

Write-Host "VM" -ForegroundColor Green 
$vm.Id
"`n"

Write-Host "NICs" -ForegroundColor Green
$($vm.NetworkProfile.NetworkInterfaces.id)
"`n"

Write-Host "Disks" -ForegroundColor Green
$disk
"`n"

Write-Host "IPConfigs" -ForegroundColor Green
$($getnic.IpConfigurations.id)
$GetNIC.ipconfigurations.privateipaddress
"`n"

Write-Host "Public IPs" -ForegroundColor Green
if(!$getnic.IpConfigurations.publicipaddress.id){
Write-Host "No Public IP Found"}
Else{
$($getnic.IpConfigurations.publicipaddress.id)
$getpip.IPaddress
"`n"
}

if($Force -ne $true){
Write-Host "Resources listed above will be deteled, and cannot be recovered. Select Yes to Delete the VM(s), No to abort" -ForegroundColor Red
#$conf = Read-Host "Enter Y to proceed, press any other key to stop"
$conf = [System.Windows.MessageBox]::Show('Select Yes to Delete the VM(s), No to abort','User Confirmation','YesNo')
if ($conf -ne 'Yes'){Break}

Else{

#Delay
Write-Host "You have selected $conf. Resources listed above will be deteled now"
$delay = (Get-Date).AddSeconds(10)
while($delay -gt $(get-date)){
$sec = $delay - $(Get-Date)
Write-Host "Deleting the resources in $($sec.Seconds) Seconds..."
Start-Sleep -Seconds 1
#Write-Progress -Activity "Deleting resources in" -SecondsRemaining $sec.Seconds
}
}
}
Else{Write-Host "Resources listed above will be deteled, and cannot be recovered"}

#Removing VM
Remove-AzVM -Name $VMName -ResourceGroupName $ResourceGroup -Force

#Removing Disks
For($i=0;$i -lt $disk.count; $i++){
Remove-AzDisk -ResourceGroupName $diskRG[$i] -DiskName $disk[$i] -Force
}

#Remove NIC
For ($i=0;$i -lt $nicname.Count; $i++){
Remove-AzNetworkInterface -Name $nicname[$i] -ResourceGroupName $nicRG[$i] -Force
}

#Remove IPConfig

#Remove PIP
for($i=0;$i -lt $PiPName.count; $i++){
Remove-AzPublicIpAddress -Name $PiPName[$i] -ResourceGroupName $PiPRG[$i] -Force
}


}

#}


Stop-Transcript

Start-Process $env:temp

}

Export-ModuleMember -Function Remove-AzVMDisksNICsPIPs