Category Archives: vmware

VMware Scripts

PowerCLI get list of vMotion IP’s in use



$Array = @()
$Clusters = Get-Cluster | Sort Name
ForEach ($Cluster in $Clusters){
$VmHosts = $Cluster | Get-VmHost | Where {$_.ConnectionState -eq “Connected”} | Sort Name
ForEach ($VmHost in $VmHosts){
$Array += Get-VMHostNetworkAdapter -VMHost $VmHost.Name -VMKernel | Where {$_.VMotionEnabled -eq “True”} | select VmHost,IP
}
}
$Array | Out-GridView

 

Source: http://blog.jgriffiths.org/powercli-get-list-of-vmotion-ips-in-use/


Restart Management Agent on ESXi 6.5

since 6.5 you get a error Message when you try to restart the Management Agent with the command: services.sh restart

-> Errors: Invalid operation requested: This ruleset is required and connot be disabled

 

the new command is:
services.sh restart & tail -f /var/log/jumpstart-stdout.log

Source: https://fabianbrash.com/?p=66


Create / check user on all esxi hosts

If you need to create a aditional user on every vSphere host in your enviroment, you can use the following script:

 


#Vcenter name, can be FQDN
$vsphere = "vcenter"
#User to create
$new_user = "seconduser"
#Password for new user
$new_user_passwd = "password"
#Group to add user too
$new_user_grp = "root"

#local host username with rights to create local users
$root_user = "root"
#Password for local host user with rights to create local users
$root_passwd = "password"

 #Get all of the ESX servers (connect using Windows credentials)
connect-viserver $vsphere
$hosts = Get-View -ViewType HostSystem
disconnect-viserver -confirm:$false

# For each ESX server, connect and see if the new account exists.
# If it does, reset the password and ensure the account is granted shell access.
# If it doesn't, create it and add to the root group (this seems to be necessary to allow ssh login in ESX4.0)
$hosts | %{ $_.name } | %{
 echo $_
 connect-viserver -server $_ -user $root_user -password $root_passwd
 if ($?) {
 if (! (get-vmhostaccount | ?{ $_.id -eq $new_user })) {
 new-vmhostaccount -useraccount -id $new_user -password $new_user_passwd -grantshellaccess
 set-vmhostaccount -groupaccount $new_user_grp -assignusers $new_user
 }
 else {
 set-vmhostaccount -useraccount $new_user -password $new_user_passwd -grantshellaccess $true
 }

disconnect-viserver -confirm:$false "*"
 }
}

 

Source: https://vtote.wordpress.com/2013/03/20/create-user-on-hosts/

Script based vmware “DRS” (Distributed Resource Scheduler)

Recently I found a very interesting script to do something like vmware DRS.

https://ryanmangansitblog.com/2013/03/08/vmware-host-cluster-vm-memory-balancing-script-implementation/

There are some points to improve and the you can use it very well.


Use it only on a certain cluster

change the line 5 from

before
$objHosts = get-vmhost; 

after
$objHosts = get-vmhost | Where-Object {$_.Parent -Like “*Clustername*”} ;

Use random VM to move
(without that, you will face the problem, that you have a lot of small VMs on one host and on the others, you’ll have the larger ones!)

Remark the line 31

# $objVMs = $objVMs | sort “Memory*”

change the percentage of the average memory load
(either set this value to 0 or use an other number that 10%)

change the value in the line 94

if($objHost.PercentMemory -gt ($intAverage + 10)){

 

 

thanks to Scott Warren and Ryan Mangan