VMware Cloud Community
Sureshadmin
Contributor
Contributor
Jump to solution

Need vCenter management scripts

Hi,

I need powershell scripts to perform below given tasks,

1. Script to power on all the powered off vm's in a cluster with a criteria of 2 vm's power on at any given point of time and the remaining vm's follow as the running power on tasks complete.

2. Script to get the count of virtual machines in vcenter classified by guest OS. Please note vmware tools are installed in all the machines.

Example:

Guest OS VM count

Windows XP Professional 50

Windows 2003 server 40

3. Script to give a count of esx servers in a vcenter classified by their version and build number.

Example:

Version(build) Count

3.5.0(176894) 12

3.5.0(199239) 10

4. Script to get count of virtual machine in each folder with datacenter name.

Datacenter_name | Foldername | vmcount

5. Script to get Overall Count Statistics from vcenter

Total Datacenters:

Total Clusters:

Total ESX Hosts:

Total VM's:

Total Templates:

Total LUN Datastores:

Total Size of LUN Datastores in GB:

Thanks in advance!

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I have checked and changed the script. I even managed to test it. It now starts the powered off VM's with a maximum of two at the same time:

Get-Cluster <MyCluster> | `
  Get-VM | `
  Where-Object {$_.PowerState -eq "PoweredOff"} | `
  ForEach-Object {
    while ((Get-Task | Where-Object {($_.Name -eq "PowerOnVM_Task" -or $_.Name -eq "PowerOnMultiVM_Task") -and $_.PercentComplete -ne 100}).Count -ge 2) {
      Start-Sleep -Seconds 5
    }
    Start-VM -VM $_ -RunAsync
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
23 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

A script to give a count of esx servers in a vcenter classified by their version and build number:

Get-View -ViewType HostSystem | `
  ForEach-Object { "$($_.Config.Product.Version)`($($_.Config.Product.Build)`)" } | `
  Group-Object | `
  Select-Object -property @{N="Version(Build)";E={$_.Name}},Count

Regards, Robert

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
RvdNieuwendijk
Leadership
Leadership
Jump to solution

A script to get the count of virtual machines in vcenter classified by guest OS:

Get-View -ViewType Virtualmachine | `
  ForEach-Object { $_.Config.GuestFullname} | `
  Group-Object | `
  Select-Object -property @{N="Guest OS";E={$_.Name}},Count

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

A script to power on all the powered off vm's in a cluster with a criteria of 2 vm's power on at any given point of time and the remaining vm's follow as the running power on tasks complete:

Get-Cluster MyCluster | `
  Get-VM | `
  Where-Object {$_.PowerState -eq "PoweredOff"} | `
  ForEach-Object {
    while ((Get-Task | Where-Object {$_.Name -eq "PowerOnVM_Task"}).Count -ge 2) {
      Start-Sleep -Seconds 5
    }
    Start-VM -VM $_ -RunAsync
}

I was not able to test this script, as I don't want to start all my poweredoff VM's. Smiley Wink

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

A script to get count of virtual machine in each folder with datacenter name:

Get-DataCenter | `
  ForEach-Object {
    $DataCenter = $_.Name
    $_ | Get-Folder | `
      ForEach-Object {
        $Report = "" | Select-Object Datacenter,Folder,VMCount
        $Report.Datacenter = $Datacenter
        $Report.Folder = $_.Name
        $Report.VMCount = @($_ | Get-VM).Count
        $Report
      }
  }

Message was edited by: RvdNieuwendijk

The script had a problem if the number of VM's in a folder is 0 or 1. That problem is corrected.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Robert,

This script does not list out ESX servers in a vcenter classified by their version and build number. I think it gives out the Vmware tools build number and the VM count

Can you please check it?

Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Robert,

I noticed couple of problems with this script,

1. For the folder "Discovered Virtual machine" in every datacenter the VMcount is blank, but i have some Vm's in it.

2. I notice two folders "host" and "vm" are present for each datacenter. I dont have such folder in datacenters. Is it something present in default?

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Sorry, misunderstood your question. Will change the script.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

  1. I changed the script to give a count of esx servers in a vcenter classified by their version and build number to give the version(build) count.

  2. I changed the script to get count of virtual machine in each folder with datacenter name to display zero instead of blank if there are no VM's in a folder.

  3. The "host" and "vm" folders are allways there. Allthough you don't see them in the vSpere client. There is even a folder "Datacenters". But my script doesn't show the "Datacenters" folder, because it show only the folders below the Datacenters.

  4. I don't understand why the script doesn't display the count of the VM's in the "Discovered virtual machine" folder. The scripts counts the VM's in my "Discovered virtual machine" folder correct.

I will work on the script to get Overall Count Statistics from vcenter now.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

A script to get Overall Count Statistics from vcenter:

"Total Datacenters: $(@(Get-Datacenter).Count)"
"Total Clusters: $(@(Get-Cluster).Count)"
"Total ESX Hosts: $(@(Get-VMHost).Count)"
"Total VM's: $(@(Get-VM).Count)"
"Total Templates: $(@(Get-Template).Count)"
"Total LUN Datastores: $(@(Get-Datastore).Count)"
$TotalDatastoresCapacityMB = 0
Get-Datastore | ForEach-Object { $TotalDatastoresCapacityMB += $_.CapacityMB }
$TotalDatastoresCapacityGb = $TotalDatastoresCapacityMB/1024
"Total Size of LUN Datastores in GB: $TotalDatastoresCapacityGb"

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

As a bonus I give the script to count the VMware Tools versions installed:

Get-View -ViewType Virtualmachine | `
  ForEach-Object { $_.Config.Tools.Toolsversion} | `
  Group-Object | `
  Select-Object -property @{N="Version(Build)";E={$_.Name}},Count

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

A script to give a count of esx servers in a vcenter classified by their version and build number --> Works perfect

A script to get the count of virtual machines in vcenter classified by guest OS--> Works perfect

A script to get Overall Count Statistics from vcenter--> Works perfect

A script to power on all the powered off vm's in a cluster with a criteria of 2 vm's power on at any given point of time and the remaining vm's follow as the running power on tasks complete--> Still didn't execute it, will do it in couple of days.

A script to get count of virtual machine in each folder with datacenter name--> Now its showing the Vm's in "Discovered virtual machine" folder. But can you prevent showing the default "vm" and "host" folder from the output because i get the count of the folders and vm wrong, and then i have to manually remove it for each datacenter

Finally thanks for the bonus script Smiley Happy

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I'm glad that everything works perfect so far. Here the changed script to get the count of virtual machines in each folder with datacenter name without the vm and host folders:

Get-DataCenter | `
  ForEach-Object {
    $DataCenter = $_.Name
    $_ | Get-Folder | `
      Where-Object {$_.Name -ne "host" -and $_.Name -ne "vm"} | `
      ForEach-Object {
        $Report = "" | Select-Object Datacenter,Folder,VMCount
        $Report.Datacenter = $Datacenter
        $Report.Folder = $_.Name
        $Report.VMCount = @($_ | Get-VM).Count
        $Report
      }
  }

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Robert,

Thanks for the script. Its working fine and the vm and host folders are now gone.

I ran your script and got the ouput and compared to the virtual center summary page, but the count of VM shown in the summary page and the count of vm in the script didn't match. So i was tracking where was the missing vm's and found that some vm's are not kept in folder and placed directly under the datacenter object.

So now a new addition is required to add a line to the output with the count of vm's which are not in folder. Can you please help me with this.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to include the "vm" folder to get the guests at the root of the datacenter.

In this script you will see it as a "blank" foldername

Get-DataCenter | `
  ForEach-Object {
    $DataCenter = $_.Name
    $_ | Get-Folder | `
      Where-Object {$_.Name -ne "host"} | `
      ForEach-Object {
        $Report = "" | Select-Object Datacenter,Folder,VMCount
        $Report.Datacenter = $Datacenter
				if($_.Name -eq "vm"){
					$Report.Folder = ""
				}
				else{
					$Report.Folder = $_.Name
				}
        $Report.VMCount = @($_ | Get-VM).Count
        $Report
      }
  }

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

In the vm folder are all the VM's. Not only the VM's that are not in another folder. So to get the correct count of VM's that are not in another folder, you have to substract the number of VM's in all the folders that are not the host or the vm folder from the total number of VM's:

Get-DataCenter | `
  ForEach-Object {
    $DataCenter = $_.Name
    $FolderVMcount = 0
    $_ | Get-Folder | `
      Where-Object {$_.Name -ne "host" -and $_.Name -ne "vm"} | `
      ForEach-Object {
        $Report = "" | Select-Object Datacenter,Folder,VMCount
        $Report.Datacenter = $Datacenter
        $Report.Folder = $_.Name
        $Report.VMCount = @($_ | Get-VM).Count
        $FolderVMCount += $Report.VMCount
        $Report
      }
    $Report = "" | Select-Object Datacenter,Folder,VMCount
    $Report.Datacenter = $Datacenter
    $Report.Folder = ""
    $Report.VMCount = @($_ | Get-VM).Count - $FolderVMcount
    $Report
  }

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are right, but your last script gives incorrect numbers when you have nested blue folders.

This should do the trick (and be a lot faster).

Get-Datacenter | %{
	$dc = $_
	Get-View -ViewType Folder -SearchRoot ($_ | Get-View).MoRef | `
	where {"host","network","Storage","datastore" -notcontains $_.Name} | %{
		New-Object PSObject -Property @{
			Datacenter = $dc.Name
			FolderName = &{if($_.Name -eq "vm"){""}else{$_.Name}}
			VMcount = &{if($_.ChildEntity){($_.ChildEntity | where{$_.Type -eq "VirtualMachine"} | Measure-Object).Count}else{0}}
		}
	}
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RvdNieuwendijk
Leadership
Leadership
Jump to solution

You are right. I didn't have nested folders in my test environment, so I didn''t run against the nested folders problem. I have made some nested folders now and I see where my script fails. Your script is very fast. I like it!

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Luc,

This script produces perfect output and its very fast. Thank you so much.

Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Robert,

I ran your script to power on VM's in a cluster 2 at a time. But the script powered on totally 2 vm's in the cluster and other powered off vm's were not powered on. My requirement is to power on all the powered off vm's, 2 vm's at a time. Can you please check the script once?

Reply
0 Kudos