Showing posts with label vCenter. Show all posts
Showing posts with label vCenter. Show all posts
, , ,

Move multiple disks from one vm to another vm (KB#00102)

Hello Folks,

Adding another solution for one unique challenge I got from my customer. 

Challenge - Customer used to move multiple vmdk files from one vm to another vm in vcenter server. Actual challenge is the time in hand for this operation and number of vmdks to remove are always around 15, 18 or 20 per vm. This activity used to have multiple VMs. This become more challenging at the time of roll back once we have moved all disks from source to target vm. That much of vmdks to move and in short period of time can lead to confusion and human error too.

Solution - I created below powercli script which drastically decrease the time require in such activity and roll back too become very easy with it. Chance of human error is 99% lesser now.

Below is the code -

Function move-vmdk{
clear
$time
= Get-Date
#It will record the timestamp before starting this activity

Write-Host
"Current time is $time"
$sourcevm
= Read-Host "Enter the source VM Name "
Write-Host
"Total number of disk on source vm is" (Get-HardDisk -VM $sourcevm).count -ForegroundColor yellow
$fd
= Get-HardDisk -VM $sourcevm
$TargetVM
= Read-Host "Enter the target VM Name "
Write-Host
"Total number of disk on target vm is" (Get-HardDisk -VM $TargetVM).count -ForegroundColor Green
Write-Host
"This script works with file name of disk to move so please mention the filename of each disk to move in notepad and save it in C:\temp with disklock.txt name"
#save the disk’s location for all the targeted disks to path C:\Temp in notepad file named diskloc.txt
$diskfile
= Get-Content -Path C:\Temp\diskloc.txt
$confirm
= Read-Host -Prompt "Are you sure you want to process for this disk migration (Y/N) "
If
($confirm -eq "y") {
Foreach
($loc in $diskfile){
$trgVM
= Get-VM -Name $TargetVM
$disk
=get-vm -name $SourceVM | Get-HardDisk | Where-Object {($_.Filename -eq $loc)}
Remove-HardDisk
$disk -Confirm:$false
New-HardDisk
-VM $trgVM -DiskPath $loc
Write-host
(" ")
}
}
$time
= Get-Date
#It will record the timestamp after completion of this activity
Write-host
"Operation has been completed succesfully"
Write-Host
"Current time is $time"
Write-Host
"Total number of disk on target vm is" (Get-HardDisk -VM $TargetVM).count -ForegroundColor Green
Write-Host
"Total number of disk on source vm is" (Get-HardDisk -VM $sourcevm).count -ForegroundColor yellow
}

Below is the sample output

Note that time taken is just 32 seconds to move four VMDK files. Just to add, size of vmdk doesn't change the time for this migration.

Hope you will find it useful if you too have such requirement! Any doubt or thought, plesae feel free to comment.

Cheers!

 

, ,

PS | How to get HA restarted VM's Org and OrgvDC info with VM Name

Overview

You will see many blogs giving solution for fetching the VM names which are restarted by HA in event of esxi host failures using Get-VIEvent powercli command. But the extracted VM Name too is not in well format to use as it is. You have to use excel and text to column and then extract the VM Name etc. For me, I have vCD also so at the time of ESXi host failures and HA events, I not only need to fetch the VM Name but also Org and OrgvDC info to share it with my customer. It becomes more lengthy for me and I need to make it quick. So it is extended solution for such kind of scenario. Hope you will find it useful.

Let's see how I could do it using powershell.

Script

#Start here

Write-Host "This script will help you out to have VM name restarted by HA due to esxi host failuers" -ForegroundColor Yellow

Function Get-HAVM{
$Date=Get-Date
$HAVMrestartold=1
$raw = Get-VIEvent  -maxsamples 10000000 -Start ($Date).AddDays(-$HAVMrestartold) -type warning | Where {$_.FullFormattedMessage -match "restarted"} |select CreatedTime,FullFormattedMessage |sort CreatedTime -Descending
$raw.vm.name
Remove-Item -Path C:\Temp\vmlist.csv
$raw.vm.name | Out-File C:\Temp\vmlist.csv
}
Get-HAVM
$allvms = Get-Content -Path C:\Temp\vmlist.csv
$vms = Get-VM -Name $allvms
$myView = @()
foreach ($vm in $vms){
$Report = [PSCustomObject] @{
 VM_Name = $vm.Name
 Org_Name = $vm.Folder.Parent.Parent.Name
 OrgvDC_Name = $vm.Folder.Parent.Name
}
$MyView += $Report
}
$myView | Out-GridView

#End here

Any doubt? Comment box is yours :)

Let's give it more power

If you have smtp configured in your environment then simply you can mail it from the same script using Send-MailMessage command but for that you might have to do some tweak in above script. 

Hint is, You have to save final report. Change in the last line of above script like

$myView | Out-File C:\Temp\vmsrestartedbyHA.csv

then use below command

Send-MailMessage -From 'gautam.johar@vcnotes.in' -To 'my.reader@home.com', 'myreader2@home.com' -Subject 'HA Event is triggered and VM list is attached' -Body "Please find the attachment" -Attachments C:\Temp\vmsrestartedbyHA.csv -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtp.vcnotes.in'

Change wherever applicable.

If you are good enough in PowerShell then you can have many ways to enhance the ideas. For me this is basic script which is working fine for me.

Side Note

I created this script to run perfectly in PowerShell ISE so run in that please or if you have any error in running it in simple powershell cli terminal then you might need to fix the visible errors.

Good Luck!









,

PS | To extract DRS rules with VM names

Hi Guys,

This is not a big thing but still I wanted to document it for my own reference. I got a request like which VMs are in which DRS rules so I got below script.

#Start here

$VC = Read-host "Enter the FQDN\IP of vCenter Server"

Connect-VIServer $VC
$DRSRules = Get-Cluster | Get-DrsRule
$Results = ForEach ($DRSRule in $DRSRules)
     {
    "" | Select-Object -Property @{N="Cluster";E={(Get-View- Id $DRSRule.Cluster.Id).Name}},
    @{N="Name";E={$DRSRule.Name}},
    @{N="Enabled";E={$DRSRule.Enabled}},
    @{N="DRS Type";E={$DRSRule.KeepTogether}},
    @{N="VMs";E={$VMIds=$DRSRule.VMIds -split ","
     $VMs = ForEach ($VMId in $VMIds)
        {
        (Get-View -Id $VMId).Name
        }
      $VMs -join ","}}
     }
$Results | out-gridview

#End here

Another window will open and copy entire output into excel if you want.

Cheers!


, ,

Automation | Add vCPU in VM if usage is more than 90%

Hi Guys,

I got this question from one of the VMware group so I thought to add it in my blog and then share. Automation of CPU addition is not tough but adding it with condition can be little challenging. You can do it with vROPS very easily but if you don't have vROPS then I have solution for you.

Pre-requisite is to enable hot add for CPU.

Below is the base code- 

Connect-VIServer vcenter_ip #to connect vcenter
$VM = Get-VM VM_Name #to get the vm name
#to convert the value in GHz. Default value comes in MHz
$maxcpu = ($vm.Extensiondata.Summary.Runtime.MaxCpuUsage)/1024
#to convert number of cpu into GHz speed. Here you need to replace 2.80 as per physical host's core speed at your end
$cpus = ($vm.Extensiondata.summary.config.NumCPU)*2.80
# to have percentage value
$cpulimit = $cpus*90/100
#below is the hot add cpu command
if ($maxcpu -gt $cpulimit){
get-vm $vm | Set-VM -NumCpu 2 -Confirm:$false #Current value is 1 and it will change it to 2
}

Now the question is how to automate it,

you can create a loop so that it can run infinite. I have created below function

Connect-VIServer vcenter_ip
Function AutoAddCPU{
$VM = Get-VM VM_Name
#to convert the value in GHz
$maxcpu = ($vm.Extensiondata.Summary.Runtime.MaxCpuUsage)/1024
#to convert number of cpu into GHz speed
$cpus = ($vm.Extensiondata.summary.config.NumCPU)*2.80
$cpulimit = $cpus*90/100

if ($maxcpu -gt $cpulimit){
get-vm $vm | Set-VM -NumCpu 2 -Confirm:$false
}
AutoAddCPU
}
AutoAddCPU #this is not a mistake. Use it as it is.

Now the problem is, how will it monitor that particular VM continuously.

Simple solution, use task scheduler from where you can access vcenter server. Add above script in task scheduler and then run it once, it will run forever.

Another problem is, what if that server or jump server restart then it will break the script. Solution is to create task scheduler to run this script at every system startup.

There must be many questions here. For example, you might need to monitor many VMs at the same time, you want to monitor the cpu usage if goes above than 95% and more....

This is the base solution I have provided and can be customized or enhance as per individual need.

If you have such requirement too then feel free to write me back. Will be glad to assist. 

If  anyone wants the same thing from vrops then let me know, I will create one post on that as well. Using vrops is more authentic and efficient way to do it.



, ,

Automate Power-On Operations using vROPS

Hi Guys,

Today someone asked "What we we can automate in vsphere environment?"

This question directly hit the automation which I recently done in production environment. My use case might be or might not be similar in your case but I trust that it certainly can give you more ideas on "What you can automate in your environment".

My use case was "These VMs must not powered off due to any reason". There was direct impact on the production if even single VM goes down and in few cases we took much time to dig down the actual cause of the issue where we found that VM was powered off!!! why? how?.....that's different topic.

Just to give you more insight that it is a VRA (Virtual Replication Appliance) of Zerto Disaster Recovery. It is responsible for replication between primary and recovery site. More detail about VRA, you can find here.

Let's see, how could I ensure that none of the VM will shutdown.

See, due to any issue or manual intervention if VM is shutdown then once it will be shutdown, my focus was to instantly turn it on if it is detected as shutdown at any point of time.

Being a DR appliance VM, it was not that business critical and could be handled calmly. isn't it?

There could be many ways to do it but I'll share what I did.

Method 1: Automate Power-On operation using vROPS. This method for those who has vROPS in their environment.

Follow below series of action to do it-
Step 1 - Create a Policy
Step 2 - Create Custom Group
Step 3 - Create Symptom Definition
Step 4 - Create recommendation
Step 5 - Create Alert Definition
Step 6 - Enable automation in created policy in Step 1

I have created a video for demonstration purpose and tried to show the above steps one by one. Your any comment\thoughts are welcome

Play below video to understand the complete procedure to do this.


In case, any doubt, feel free to let me know.



Thank you,
vCloudNotes
,

PowerCLI | Who wants this engine?

Hi Guys,

In my last post, I shared a few PowerCLI commands to take a snapshot but today I changed the look and feel. I tried to create an engine\application kind of thing where you just need to press 1, 2 and 3 and all your work related to your snapshot will be done.

No need to login GUI and tasks will be done in a faster way.

It helped me a lot and I hope it will be helpful for you as well. Tell me if anyone wants this engine now.





Thank you,
vCloudNotes






,

vCenter | Snapshot Operations

Hi Folks,

Today someone asked about the snapshot. Like if I need to take snapshot from PowerShell then how to? if I quiesce the snapshot in PowerShell then how-to etc. etc. so, I thought to summarize as many as I can snapshot operations in a single page and share with all. Here you go..

Connect-VIserver VC-IP

#To take a snapshot for a single VM without quiescing and memory
$VM = read-host "Enter the VM Name "
New-Snapshot -VM $VM -Name vcnotes_snap -Description "This is test snap"
#with quiesced on
New-Snapshot -VM $VM -Name vcnotes_snap -Description "This is test snap" -Quiesce
#With memory state
New-Snapshot -VM $VM -Name vcnotes_snap -Description "This is test snap" -Memory
#with Memory and quiesce both
New-Snapshot -VM $VM -Name vcnotes_snap -Description "This is test snap" -Memory -Quiesce

#take snapshot for mulitple VMs and with quiesced off and no memory state
foreach ($AVM in (Get-content -path C:\Temp\vmlist.txt)){New-Snapshot -VM $AVM -Name vsnap -Description "This is test"}
#with quiesced on
foreach ($AVM in (Get-content -path C:\Temp\vmlist.txt)){New-Snapshot -VM $AVM -Name vsnap -Description "This is test" -Quiesce}
#with memory state
foreach ($AVM in (Get-content -path C:\Temp\vmlist.txt)){New-Snapshot -VM $AVM -Name vsnap -Description "This is test" -memory}
#with memory and quiesce both
foreach ($AVM in (Get-content -path C:\Temp\vmlist.txt)){New-Snapshot -VM $AVM -Name vsnap -Description "This is test" -Quiesce -Memory}

#Snapshot Consolidation
#For all Vms which needs consolidation
Get-VM | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded} | foreach {$_.ExtensionData.ConsolidateVMDisks_Task()}
#for any single VM
$vmname = Read-Host "Enter VM Name"
$vm = Get-VM -Name $vmname
$vm.ExtensionData.consolidateVMDisks_Task()

#snapshot deletion
#to delete all snapshot older than specific days for single VM. In order to delete all snapshot taken today just replace 10 with 0 in below command
$vmname = Read-Host "Enter VM Name"
Get-VM -Name $vmname | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-10)} | Remove-Snapshot

#delete snapshot on multiple VMs older than specific days
$VM = Get-Content -Path C:\temp\vmlist.txt
Get-VM -Name $VM | Get-Snapshot | Where {$_.created -lt (Get-Date).AddDays(-10)} | Remove-Snapshot

#delete all snapshot on a VM
$VM = Read-Host "Enter VM Name"
Get-VM -Name $VM  | Get-Snapshot | Remove-Snapshot
#delete specific snapshot on a VM
$VM = Read-Host "Enter VM Name"
Get-VM -name $VM | Get-Snapshot | Select VM,Name,Created,SizeGB | FT
Write-Host "Tell me the snapshot name from above list"
$snapname = Read-Host "enter name here"
Get-VM -Name $VM | Get-Snapshot -name $snapname | Remove-Snapshot



#Revert to the last snapshot
$VM = read-host "Enter VM Name" 
Get-snapshot -VM $VM | select name 
Write-host "Tell me the name of Snapshot from the above list which you want to revert to"
Get-Snapshot -VM $VM -Name Test1 | Set-VM $VM


Let me know if anyone wants me to add anything to the list.



Thank you,
vCloudNotes