## Chawn Limited - WVDHostBuilder V1.1 - Delete Virtual Machine ## No Liability Accepted - Use at your own risk ## v1.1 added timer ## Specify required parameters below ## Specify VMName parameter on the command line ## Connect-AzAccount ## .\DeleteWVDvm.ps1 "NPWVD001" # Virtual Machine Name is CaSe Specific ## This will delete a single VM ######################################## ## To delete multiple VMs ## Define all paremeters below except for the VM name, then run ## Connect-AzAccount ## Connect-AzureAD ## $NPWVD001=Start-Job -Name "NPWVD001" -FilePath .\DeleteWVDvm.ps1 -ArgumentList "NPWVD001" ## $NPWVD002=Start-Job -Name "NPWVD002" -FilePath .\DeleteWVDvm.ps1 -ArgumentList "NPWVD002" ## You can get feedback at any time by typing ## $NPWVD001 ## You can get detailed feedback at any time by typing ## receive-job -Job $NPWVD001 -keep ### VARIABLES ################## param( [String]$VMName = "VMName", # Set the VM Name [String]$RemoveWVD="Y", # Remove Virtual Machine from WVD Host Pool [String]$HostPool="HostPool_Name", # Set the WVD HostPool name [String]$HostPoolResourceGroup="HostPool_ResourceGroup", # Set the WVD HostPool Resource Group name [String]$ADDomain="domain.local", # Set the Active Directory Domain Name [String]$DelAzureVM="Y", # Delete Azure Virtual Machine [String]$DelAzureADDevice="Y", # Delete Azure AD Device [String]$DelADComputer="Y" # Delete Active Directory Computer Account ) ### END OF VARIABLES ################## $d1=get-Date %{ Write-host `n"Deleting Virtual Machine "`t`t`t $VMName Write-host "Leave HostPool: "`t`t`t`t $HostPool " " $RemoveWVD Write-host "Delete Virtual Machine:"`t`t`t $DelAzureVM Write-host "Delete Azure AD Device:"`t`t`t $DelAzureADDevice Write-host "Delete Active Directory Computer Account: "`t $DelADComputer } ### Pre-Reqs %{ if (get-module -listavailable -Name Az.Compute) {write-host -ForegroundColor Green `n"Az Powershell module available"} Else {write-host -ForegroundColor Red `n"Az Powershell module NOT available." write-host -ForegroundColor Yellow `n"Please install Azure Powershell: https://docs.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-5.5.0" break} } %{ if ($DelAzureADDevice -eq "Y") { if (get-module -listavailable -Name AzureAD) {write-host -ForegroundColor Green `n"AzureAD Powershell module available"} Else {write-host -ForegroundColor Red `n"AzAzureAD Powershell module NOT available." write-host -ForegroundColor Yellow `n"Please install AzureAD Powershell: https://docs.microsoft.com/en-us/powershell/azure/active-directory/install-adv2?view=azureadps-2.0" break} } } ### End of Pre-Reqs # Login to Azure Compute $d1=get-date %{ $AccessToken=Get-AzAccessToken -ErrorAction SilentlyContinue if ($AccessToken.ExpiresOn -lt $d1) {Connect-AzAccount -ErrorAction SilentlyContinue} $AccessToken=Get-AzAccessToken -ErrorAction SilentlyContinue if (-not ($AccessToken)) {Write-Host -ForegroundColor Red `n"Not logged into Azure. Exit" break} } # Login to AzureAD if ($DelAzureADDevice -eq "Y") { %{ try{$AZADAccessToken=Get-AzureADCurrentSessionInfo -ErrorAction silentlycontinue} catch{write-host -ForegroundColor Red `n "Not authenticated to Azure AD" Connect-AzureAD} try{$AZADAccessToken=Get-AzureADCurrentSessionInfo -ErrorAction silentlycontinue} catch{write-host -ForegroundColor Red `n "Not authenticated to Azure AD. Will not attempt to delete virtual machine. Exit" break} } } # Check VM is in Drain mode and no sessions are active if ($RemoveWVD -eq "Y") { $HPName=$VMname + "." + $ADDomain try {$HPVM=Get-AzWvdSessionHost -ResourceGroupName $HostPoolResourceGroup -HostPoolName $HostPool -Name $HPName -ErrorAction stop write-host -ForegroundColor Green `n"Session host name: " $HPName " is valid."} catch {write-host -ForegroundColor Red `n"Virtual Machine " $HPName " Does not Exist in WVD hostpool. Exit" `n$_ $HPName="null" break} # Remove the VM from the WVD HostPool if ($HPName -ne "null") { if ($HPVM.session -lt 1) { try {Remove-AzWvdSessionHost -ResourceGroupName $HostPoolResourceGroup -HostPoolName $HostPool -Name $HPName -ErrorAction stop write-host -ForegroundColor Green `n"Removed virtual machine " $HPName " from Hostpool."} catch {write-host -ForegroundColor Red `n"Could not remove Remove virtual machine from Hostpool." `n$_ break} } Else {write-host `n$HPName write-host "Allow Session:" $HPVM.AllowNewSession write-host "Current Sessions:" $HPVM.Session write-host -ForegroundColor Red `n"Not removing " $HPName " from hostpool becasue there are still active sessions. Exit" break} } } # Delete Virtual Machine if ($DelAzureVM -eq "Y") { write-host `n"Delete Virtual Machine:" $VMName if ($VM=get-azvm -Name $VMName) { try{$nic=get-aznetworkinterface -name $vm.NetworkProfile.NetworkInterfaces.id.Split("/")[-1] -ErrorAction SilentlyContinue write-host -ForegroundColor Green `n"Got the NIC."} catch{write-host -ForegroundColor Red n"Could not get the NIC for : " $VMName "Will not be able to delete the NIC" `n $_} try {$VM | remove-azvm -force -ErrorAction SilentlyContinue write-host -ForegroundColor Green "Deleted Virtual Machine: " $VMName} catch {write-host -ForegroundColor Red `n"Error encountered deleting Virtual Machine: " `n$VMName + $_} } Else {write-host -ForegroundColor Red `n"Virtual Machine does not exist: " $VMName `n $_} } #Delete Nic write-host `n"Delete Virtual Machine NIC:" $Nic.id if ($nic) { try {$nic | remove-AzNetworkInterface -force -ErrorAction SilentlyContinue write-host -ForegroundColor Green "Deleted Virtual Machine NIC: " $nic.id} catch {write-host -ForegroundColor Red `n"Error encountered deleting Virtual Machine NIC: " `n $nic.id + $_} } Else {write-host -ForegroundColor Red `n"Virtual Machine NIC does not exist: " `n $nic.id `n $_} # Delete Azure AD Computer Account if ($DelAzureADDevice -eq "Y") { write-host `n"Delete Azure AD Device:" $VMName %{ if ($AzADVM=Get-AzureADDevice -Filter "displayname eq '$VMName'") { try {$AzADVM | remove-AzureADDevice -ErrorAction stop write-host -ForegroundColor Green "Deleted Azure AD computer account: " $VMName} catch {write-host -ForegroundColor Red `n"Error encountered deleting Azure AD computer account: " `n$VMName + $_} } Else {write-host -ForegroundColor Red `n"Azure AD computer account does not exist: " `n$VMName `n $_} } } # Delete On-Prem ACTIVE DIRECTORY Account if ($DelADComputer -eq "Y") { %{ if (get-module -listavailable -Name ActiveDirectory) {write-host -ForegroundColor Green `n"AzureAD Powershell module available"} Else {write-host -ForegroundColor Red `n"Active Directory Powershell module NOT available. " write-host -ForegroundColor Red `n"Cannot delete AD Computer Object:" $VMName write-host -ForegroundColor Yellow `n"Please install Active Directory Powershell Module: https://docs.microsoft.com/en-us/powershell/module/activedirectory/?view=winserver2012-ps" break} } write-host `n"Delete AD Computer Object:" $VMName try{$ADComputer=Get-ADComputer -Identity $VMName write-host -ForegroundColor Green "Located AD Computer Object:" $VMName} catch{write-host -ForegroundColor Red `n"Cannot locate AD Computer Object:" $VMName `n$_ break} $ComputerFQDN=$ADComputer.DistinguishedName try{Remove-ADObject -Identity $ComputerFQDN -Recursive -Confirm:$False -ErrorAction SilentlyContinue write-host -ForegroundColor Green "Deleted AD Computer Object:" $ComputerFQDN} catch{write-host -ForegroundColor Red `n"Could not Delete AD Computer Object:" $ComputerFQDN `n$_} } # Get End Time $d2=get-Date $dur=$d2-$d1 Write-Host "Finished deleting Virtual Machine " $VMName write-Host "Duration:"`t`t $dur.Hours "hours" $dur.Minutes "mins" $dur.Seconds "secs"