.DESCRIPTION
The Tool supports:
1. Power off VMs
2. Power on VMs
3. Shutdown VMs
4. Restart VMs
5. Restart VM guest
6. Delete VMs from Disk
.NOTES
#>
#--------------------- Functions --------------------
function GetInput
{
Param($prompt, $IsPassword = $false)
$prompt = $prompt + ": "
Write-Host $prompt -NoNewLine
[Console]::ForegroundColor = "Blue"
if ($IsPassword)
{
$input = Read-Host -AsSecureString
$input = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($input))
}
else
{
$input = Read-Host
}
[Console]::ResetColor()
return $input
}
function IsVMExists ($VMExists)
{
Write-Host "Checking if the VM $VMExists Exists"
[bool]$Exists = $false
#Get all VMS and check if the VMs is already present in VC
$listvm = Get-vm
foreach ($lvm in $listvm)
{
if($VMExists -eq $lvm.Name )
{
$Exists = $true
Write-Host "$VMExists is Exist"
}
}
return $Exists
}
function Delete_VM($VMToDelete)
{
Write-Host "Deleting VM $VMToDelete"
Get-VM $VMToDelete | where { $_.PowerState –eq "PoweredOn" } | Stop-VM –confirm:$false
Get-VM $VMToDelete | Remove-VM –DeleteFromDisk –confirm:$false
}
#------------------ Handle input ---------------------
"-----------------------------------------------------"
$vcAddress = GetInput -prompt "Your vCenter address" -IsPassword $false
$vcAdmin = GetInput -prompt "Your vCenter admin user name" -IsPassword $false
$vcPassword = GetInput -prompt "Your vCenter admin user password" -IsPassword $true
"-----------------------------------------------------"
$action = GetInput -prompt 'Select action: 1). Power On 2). Power Off 3) Shutdown VM Guest 4). Restart VM 5). Restart VM Guest 6). Delete VM' -IsPassword $false
$sleepTime = GetInput -prompt 'Wait time (seconds) between each VM' -IsPassword $false
"-----------------------------------------------------"
[Console]::ForegroundColor = "Yellow"
switch ($action)
{
1
{
"Your selection is 1). Power On"
}
2
{
"Your selection is 2). Power Off"
}
3
{
"Your selection is 3) Shutdown"
}
4
{
"Your selection is 4). Restart VM"
}
5
{
"Your selection is 5). Restart VM Guest"
}
6
{
"Your selection is 6). Delete VM"
}
default
{
"Invalid selection for action: $action"
exit
}
}
[Console]::ResetColor()
$csvFile = '.\CloneVMs.csv'
#check if file exists
if (!(Test-Path $csvFile))
{
write-host -ForeGroundColor Red "CSV File not found"
exit
}
"-----------------------------------------------------"
#--------------------- Main --------------------------
#Read input CSV file
Disconnect-VIServer $vcAddress -Confirm:$false
#Connect-VIServer $vcAddress -ErrorAction Stop -user $vcAdmin -password $vcPassword
Connect-VIServer $vcAddress -user $vcAdmin -password $vcPassword
$csvData = Import-CSV $csvFile
foreach ($line in $csvData)
{
$VMName = $line.VMName
switch ($action)
{
1
{
Get-VM $VMName | Start-VM -Confirm:$false
}
2
{
Get-VM $VMName | Stop-VM -Confirm:$false
}
3
{
Get-VM $VMName | Shutdown-VMGuest -Confirm:$false
}
4
{
Get-VM $VMName | Restart-VM -Confirm:$false
}
5
{
Get-VM $VMName | Restart-VMGuest -Confirm:$false
}
6
{
if (IsVMExists ($VMName))
{
Delete_VM ($VMName)
}
}
default{}
}
Start-Sleep -s $sleepTime
}
Disconnect-VIServer $vcAddress -Confirm:$false
exit