VMware Cloud Community
lorried82
Enthusiast
Enthusiast
Jump to solution

vmware powershell

I have this query that I want to send out an email for anything that is used at a certain percentage. I want to add Free Space as well as % for Used space and then alert on the % of used space that is > 90%. I was hoping to also add a section on the script were certain datastores could be added so they are ignored (ones we know about and don't care ). I also want to have an email sent if there query returns nothing with the subject like - no datastores over 90% usage. Any help is greatly appreciated - I have the gist of what I want but I am not sure how to build in the logic

#poweshell script to query the datastores and send an e-mail for any over 90%

connect-viserver <server>" -User <user> -Password <password>

$report=Get-Datastore | Select Name,@{N="TotalSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity)/1GB,0)}},@{N="UsedSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace)/1GB,0)}},@{N="ProvisionedSpaceGB";E={[Math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1GB,0)}},@{N=”AmountOverprovisionedGB”;E={[Math]::Round(($_.ExtensionData.Summary.Uncommitted – $_.ExtensionData.Summary.FreeSpace)/1GB,0)}},@{N="NumVMOn”;E={@($_ | Get-VM | where{$_.PowerState -eq "PoweredOn"}).Count}},@{N="NumVMOff”;E={@($_ | Get-VM | where{$_.PowerState -nq "PoweredOn"}).Count}} | Sort Name | Export-Csv -Path z:\datastoresize.csv -NoTypeInformation -UseCulture

$reportHtml = $report | ConvertTo-Html | Out-String

Send-MailMessage -To<email> -Subject DataStore_Usage_Over_90% `

  -SmtpServer <relay>-From <from> `

  -BodyAsHtml -Body $reportHtml -Attachments z:\datastoresize.csv

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this.

Note: with the -contains operator you can only check against full name, no masking characters.

If you want to check for part of a name (like local*), you coan use a RegEx expression.

Note that in a RegEx expression the vertical bar is the OR. So in the example the datastorename shall not contain 'local' or 'otherundesired'.

$undesiredDatastores = "local|otherundersired"

$report=Get-Datastore |

Select Name,

    @{N='UsedPerc';E={($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB}} |

where{$_.UsedPerc -ge 0.9 -and $_.Name -notmatch $undesiredDatastores} |

Select Name |

Sort-Object -Property Name


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this.

#poweshell script to query the datastores and send an e-mail for any over 90%

connect-viserver '<server>' -User '<user>' -Password '<password>'

$report=Get-Datastore |

Select Name,

    @{N='TotalSpaceGB';E={[math]::Round($_.CapacityGB,0)}},

    @{N='UsedSpaceGB';E={[math]::Round($_.CapacityGB - $_.FreeSpaceGB,0)}},

    @{N='UsedSpace%';E={[math]::Round(($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*100,0)}},

    @{N='ProvisionedSpaceGB';E={[Math]::Round(($_.CapacityGB - $_.FreeSpaceGB + $_.ExtensionData.Summary.Uncommitted/1GB),0)}},

    @{N='AmountOverprovisionedGB';E={[Math]::Round($_.ExtensionData.Summary.Uncommitted/1GB $_.FreeSpaceGB,0)}},

    @{N='NumVMOn';E={@(Get-View -Id $_.VM -Property RunTime -Filter @{'Runtime.PowerState'='poweredon'}).Count}},

    @{N='NumVMOff';E={@(Get-View -Id $_.VM -Property RunTime -Filter @{'Runtime.PowerState'='poweredoff'}).Count}} |

Where{$_.'UsedSpace%' -ge 90} |

Sort-Object -Property Name |

Export-Csv -Path z:\datastoresize.csv -NoTypeInformation -UseCulture

$reportHtml = $report | ConvertTo-Html | Out-String

Send-MailMessage -To<email> -Subject DataStore_Usage_Over_90% `

  -SmtpServer <relay>-From <from> `

  -BodyAsHtml -Body $reportHtml -Attachments z:\datastoresize.csv


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

Reply
0 Kudos
lorried82
Enthusiast
Enthusiast
Jump to solution

This looks like exactly what I am looking for thanks so much. The num of VM's is not showing anything. Also - how would I add exceptions for any disks I don't want to show u in the report? When pulling the disks like this - is there a way to also indicate what datacenter/cluster?

Thanks,

Lorri

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can add a Where-clause and use an array of datastore you don't want to see in the report.

Datacenter is possible, for cluster you need to check if the datastore is visible on all ESXi nodes in the cluster to start with.

The calculation for the number of of powered on and powered off VMs was incorrect.

The new version

#poweshell script to query the datastores and send an e-mail for any over 90%

connect-viserver '<server>' -User '<user>' -Password '<password>'

$undesiredDatastores = @('DS1','DS2')

$report=Get-Datastore |

where{$undesiredDatastores -notcontains $_.Name} |

Select Name,

    @{N='Datacenter';E={

        $parent = Get-View $_.ExtensionData.Parent

        while($parent -isnot [VMware.Vim.Datacenter]){

            $parent = Get-View -Id $parent.Parent

        }

        $parent.Name

    }},

    @{N='TotalSpaceGB';E={[math]::Round($_.CapacityGB,0)}},

    @{N='UsedSpaceGB';E={[math]::Round($_.CapacityGB - $_.FreeSpaceGB,0)}},

    @{N='UsedSpace%';E={[math]::Round(($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*100,0)}},

    @{N='ProvisionedSpaceGB';E={[Math]::Round(($_.CapacityGB - $_.FreeSpaceGB + $_.ExtensionData.Summary.Uncommitted/1GB),0)}},

    @{N='AmountOverprovisionedGB';E={[Math]::Round($_.ExtensionData.Summary.Uncommitted/1GB $_.FreeSpaceGB,0)}},

    @{N='NumVMOn';E={@(Get-View -Id $_.ExtensionData.VM -Property RunTime | where{$_.Runtime.PowerState -eq 'poweredon'}).Count}},

    @{N='NumVMOff';E={@(Get-View -Id $_.ExtensionData.VM -Property RunTime | where{$_.Runtime.PowerState -eq 'poweredoff'}).Count}} |

Where{$_.'UsedSpace%' -ge 90} |

Sort-Object -Property Name |

Export-Csv -Path z:\datastoresize.csv -NoTypeInformation -UseCulture

$reportHtml = $report | ConvertTo-Html | Out-String

Send-MailMessage -To<email> -Subject DataStore_Usage_Over_90% `

  -SmtpServer <relay>-From <from> `

  -BodyAsHtml -Body $reportHtml -Attachments z:\datastoresize.csv


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

Reply
0 Kudos
lorried82
Enthusiast
Enthusiast
Jump to solution

ok thanks LucD this looks great - my only question is if I can pull the datacenter can I also pull the cluster? In the get-view for get-datasore I am not see what that would be. Or do I need a report that is designed to pull the datacenter and cluster and then pull my datastores?

Appreciate the help

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can do

Get-Datastore -Name MyDS | Get-VMHost | Get-Cluster

But datastores are kept on the level of the Datacenter.

A datastore can be visible on one or more ESXi nodes in that Datacenter (that depends on the LUN mapping in your storage solution)..

From the ESXi nodes you can go to the Cluster, but you would need to make sure that the datastore is visible on all ESXi nodes in the cluster as well.


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

lorried82
Enthusiast
Enthusiast
Jump to solution

Hi LucD - so if I wanted to take this script and have the output just provide me with the name - how can I do that? When I strip down the report I am getting an Attempted to divide by zero error....I want the report to only provide output for Used% > 90 AND if provisionedspaceGB > totalspaceGB. I want to still have the option to add datastores I don't want checked as well.

 

#powershell script to query the datastores for Used% > 90% AND provisionedpsace > totalspace

connect-viserver '<server>' -User '<user>' -Password '<password>'

$undesiredDatastores = @('local*')

$TotalSpaceGB=[math]::Round($_.CapacityGB,0)

$UsedSpaceGB=[math]::Round($_.CapacityGB - $_.FreeSpaceGB,0)

$UsedSpace%=[math]::Round(($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*100,0)

$ProvisionedSpaceGB=[Math]::Round(($_.CapacityGB - $_.FreeSpaceGB + $_.ExtensionData.Summary.Uncommitted/1GB),0)

$AmountOverprovisionedGB=[Math]::Round($_.ExtensionData.Summary.Uncommitted/1GB – $_.FreeSpaceGB,0)

$report=Get-Datastore |

where{$undesiredDatastores -notcontains $_.Name} | Select Name | Where{$_.'UsedSpace%' -ge 90 and $_.'ProvisionedSpaceGB' -gt $_.'TotalSpaceGB'} |

Sort-Object -Property Name

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this.

Note: with the -contains operator you can only check against full name, no masking characters.

If you want to check for part of a name (like local*), you coan use a RegEx expression.

Note that in a RegEx expression the vertical bar is the OR. So in the example the datastorename shall not contain 'local' or 'otherundesired'.

$undesiredDatastores = "local|otherundersired"

$report=Get-Datastore |

Select Name,

    @{N='UsedPerc';E={($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB}} |

where{$_.UsedPerc -ge 0.9 -and $_.Name -notmatch $undesiredDatastores} |

Select Name |

Sort-Object -Property Name


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

Reply
0 Kudos
lorried82
Enthusiast
Enthusiast
Jump to solution

perfect thanks for all the help

Reply
0 Kudos