понедельник, 14 марта 2022 г.

Removing Shadow VMs on vCloud

Removing Shadow VMs


##==  ==##

# Removing Shadow VMs on a specific Datastore

# ToDo - Choose Datastore with OGV !


$ErrorActionPreference = "SilentlyContinue" 

$WarningPreference = "SilentlyContinue" 


cls

$User = $env:UserName

$SecurePass = Read-Host -AsSecureString "Please enter password for '$User' and wait..."

$Pass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePass))

If(-not($Pass)){Write-Host "You must enter a password!" -ForegroundColor Yellow -BackgroundColor DarkRed; Write-Host; Break}

$vCloud = "vCloud"

$vCenter = "VCRD"


##== Connecting to vCloud ==##

Disconnect-CIServer * -Confirm:$false 

$CIServerConnected = ""

$CIServerConnected = Connect-CIServer -Server $vCloud -User $User -Password $Pass # -Org $Org | Out-Null

If(-not($CIServerConnected)){Write-Host "$vCloud is NOT Connected! `nPlease check your password, locked user account or permissions!" -BackgroundColor DarkRed  -ForegroundColor Yellow; Write-Host; Break}

Else{Write-Host "$vCloud is Connected!" -ForegroundColor Yellow -BackgroundColor DarkRed}

Write-Host 


##== Connecting to vCenter ==##

Disconnect-VIServer * -Confirm:$false 

$VIServerConnected = ""

$VIServerConnected = Connect-VIServer -Server $vCenter -User $User -Password $Pass

If(-not($VIServerConnected)){Write-Host "$vCenter is NOT Connected!" -BackgroundColor DarkRed -ForegroundColor Yellow}

Else{Write-Host "vCenter is Connected!" -ForegroundColor Yellow -BackgroundColor DarkRed}

Write-Host 


##== Getting info for four NF400N Datastores from vCenter ==##

$AllDatastoreS = Get-Datastore

$ChosenDatastoreS = $AllDatastoreS | Where{$_.Name -like "Datastore_NF400N_*"} | sort -Property FreeSpaceGB | select -First 5

Write-Host "Five Datastores with the least free space" -BackgroundColor DarkGreen

$ChosenDatastoreS

Write-Host 


##== Searching for Shadow VMs on NF400N Datastores ==##

$ShadowVMs400 = Search-Cloud AdminShadowVM -Filter DatastoreName==Datastore_NF400N_*

$ShadowVMs400Count = $ShadowVMs400.Count

Write-Host "There are $ShadowVMs400Count ShadowVMs in NF400N Cluster" -BackgroundColor DarkGreen


##== Counting Shadow VMs by Datastore ==##

foreach ($Datastore in $ChosenDatastoreS)

    {

    $ShadowVMsDatastore = $ShadowVMs400 | where{$_.DatastoreName -like $Datastore}

    $VMsCount = $ShadowVMsDatastore.Count

    If($VMsCount){Write-Host "$Datastore = $VMsCount VMs" -BackgroundColor Green}

    }

Write-Host 


##== Choosing specific Datastore and creating filter sentence for searching Shadow VMs on this Datastore ==##

$DatastoreNumber = ""

[Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') > $NULL

$DatastoreNumber = [Microsoft.VisualBasic.Interaction]::InputBox("Choose Datastore number for deleting ShadowVMs (or press Enter for exit): ", "Datastore Number")


###=CHOOSE DATASTORE OR EXIT FROM THE SCRIPT IF A NUMBER IS NOT CHOOSEN=###

Write-Host 

$ChosenDatastore = -join("Datastore_NF400N_"+$DatastoreNumber)

$FilterDatastore = "DatastoreName==$ChosenDatastore"

Write-Host 


##== Exit from script by Datastore ==##

If(!$DatastoreNumber){Write-Host "NO Datastore is choosen.`n`nSCRIPT IS FINISHED!" -ForegroundColor Yellow -BackgroundColor DarkCyan; break}

Else{Write-Host "You selected '$ChosenDatastore'" -BackgroundColor Green}


##== Searching Shadow VMs on the specific Datastore ==##

$ShadowVMsOnDatastore = Search-Cloud AdminShadowVM -Filter $FilterDatastore

### $ShadowVMsOnDatastoreCount = $ShadowVMsOnDatastore.name.Count

$ShadowVMsOnDatastoreCount = $ShadowVMsOnDatastore.Count

##== Exit from script if NO ShadowVMs is found/defined on the selected Datastore ==##

If(!$ShadowVMsOnDatastoreCount){Write-Host "Wrong choice, there are NO Shadow VMs on '$ChosenDatastore'. I am exiting!" -BackgroundColor DarkGreen; Break}

Write-Host "There are $ShadowVMsOnDatastoreCount Shadow VMs on '$ChosenDatastore'. Select some or all to kill..." -BackgroundColor DarkGreen

Write-Host 


$VMsNamesBefore = $ShadowVMsOnDatastore.Name

$ShadowVMsDelete = $ShadowVMsOnDatastore | OGV -Title "Shadow VMs On '$ChosenDatastore'" -PassThru

$ShadowVMsNames = ($ShadowVMsDelete).Name

If(!$ShadowVMsNames){Write-Host "Seems you are afraid to kill the Shadow VMs :) `nYou should know it is your choice. I am exiting!" -BackgroundColor DarkGreen; Break}

Write-Host "Shadow VMs selected for deleting:" -BackgroundColor Green

$ShadowVMsNames

Write-Host 


##== Deleting Shadow VMs on the selected Datastore ==##

foreach ($shadowVM in $ShadowVMsDelete)

    {

    $shadowVMName = $shadowVM.Name

    Write-Host "Deleting '$shadowVMName'" -BackgroundColor DarkBlue -ForegroundColor Magenta

    $ShadowDelete = $shadowVM | Get-CIView

    $ShadowDelete.delete()

    }


Write-Host 

$ShadowVMsOnDatastore = Search-Cloud -QueryType AdminShadowVM

$ShadowVMsOnDatastore = $ShadowVMsOnDatastore | where {$_.DatastoreName -like $ChosenDatastore}

$VMsCountAfter = $ShadowVMsOnDatastore.Count

$VMsDifference = $ShadowVMsOnDatastoreCount - $VMsCountAfter


Write-Host $ChosenDatastore -BackgroundColor Black

Write-Host "From $ShadowVMsOnDatastoreCount Shadow VMs you choosed $VMsDifference to delete." -BackgroundColor DarkRed

Write-Host "After deleting there are $VMsCountAfter Shadow VMs left." -BackgroundColor DarkRed

Write-Host 

Write-Host "GREAT JOB '$env:UserName' !" -ForegroundColor Yellow -BackgroundColor DarkRed

Write-Host 


# del $env:TEMP\ShadowDeletedLog.csv

# $VMsDifference | Export-Csv $env:TEMP\ShadowDeletedLog.csv -NoTypeInformation -UseCulture

# ii $env:TEMP\ShadowDeletedLog.csv

##==  ==##




Комментариев нет:

Отправить комментарий