VMware Cloud Community
electricd7
Contributor
Contributor

PowerCLI script to count powered on machines in given datacenter excluding single datastore

Hello,

I am trying to come up with a powercli script that I can specify a datacenter and a excluded datstore, then loop through the vms in the given datacenter and check powered-on status exluding machines which reside in the given datastore.  So script should to following:

- only count machines in 1 datacenter

- find instance of machine, check datastore

  - if datastore = passed datastore exclude it from the count

- If machine is powered on, increase count by 1

Can anyone help me come up with this??

Reply
0 Kudos
7 Replies
admin
Immortal
Immortal

There are many scripts available on this site:

http://www.virtu-al.net/script-list/

You can customise it as per your need.

Anand

Reply
0 Kudos
electricd7
Contributor
Contributor

Thanks for the link.  Unfortunately I am too new to PowerCLI to edit these to get what I want.  I have a script which will give me the powered state of all my VMs, but not to only give me those in which I care about.  I am currently using:

$vmsindatacenter = Get-DataCenter $DataCenterName | Get-VM
    ForEach ($machine in $vmsindatacenter)
        {
            if ($machine.PowerState -eq "PoweredOn"){
                Write-Host $machine.name " - On"
            }else{
                Write-Host $machine.name " - Off"
            }
        }

And that works and tells me the powered state, but I really want to be able to only report the powered state for those machines not residing in excluded_datestore.  I found the following script

Get-Datastore | Select Name, @{N="NumVM";E={@($_ | Get-VM).Count}} | Sort Name

Which gives me a count of machines in datastores, but no way to see if they are powered on.

Reply
0 Kudos
electricd7
Contributor
Contributor

This works:

$vmsindatacenter = Get-DataCenter $DataCenterName | Get-VM
    ForEach ($machine in $vmsindatacenter)
        {
            if ($machine.PowerState -eq "PoweredOn"){
                $Datastores = Get-Datastore -VM $machine
                ForEach ($ds in $Datastores){
                    if ($volpath -contains $ds.name){
                        $strPoweredCount += 1
                    }
                }
            }
        }
        $strPoweredCount

Reply
0 Kudos
mattboren
Expert
Expert

Hello, electricd7-

Seems like you came up with a round-about way to get the info you want.  Another, slightly more direct way, would be:

$strMyDatacenter = "myDatacenter"
$strDatastoreToExclude = "someDatastore"
(
Get-Datacenter $strMyDatacenter | Get-Datastore | ?{$_.Name -ne $strDatastoreToExclude} | `
   
Get-VM | ?{$_.PowerState -eq "PoweredOn"} | Measure-Object).Count

Just use your datacenter name and the name of the datastore to exclude.  This gets the datastores in the given datacenter, except for the the datastore specified, gets the PoweredOn VMs on those datastores, and then gets the count of them via Measure-Object.

Reply
0 Kudos
electricd7
Contributor
Contributor

Awesome! Could this be changed easily to exclude a second datastore? I forgot about the iso store which a lot of machines are connected to? I was thinking of $datastorearray -ncontains $_

Reply
0 Kudos
electricd7
Contributor
Contributor

Sorry. Sent on a accident. Should have said $_.name. Would that work?

Reply
0 Kudos
mattboren
Expert
Expert

Hello, electricd7-

Yes, you could make an array of datastore names and use the -notcontains operator.  So, like:

$strMyDatacenter = "myDatacenter"
$arrDatastoreNamesToExclude = @("someDatastore","ISOsDStore")
(
Get-Datacenter $strMyDatacenter | Get-Datastore | ?{$arrDatastoreNamesToExclude -notcontains $_.Name} | `
   
Get-VM | ?{$_.PowerState -eq "PoweredOn"} | Measure-Object).Count

Enjoy.

Reply
0 Kudos