Hey everyone,
Im a total beginner with scripting. I've been lurking the forum for a while now. Taking bits of code from here and there. Reading the powershell book etc.
I can manage when i comes to codes of 1 line but somehow i just can't figure out how to put all these lines into a more functional piece of code and merge it
into one script.
So i here i am asking for some help and pointer on where to start.
This is what i want to do. I want to be able to run a single script that can collect a number of things;
Clustername
Total CPU Resources ($test = get-cluster <clustername> | get-view)($test.summary.totalcpu)
Total Memory ($test = get-cluster <clustername> | get-view)($test.summary.totalmemory)
Number of Hosts ($test = get-cluster <clustername> | get-view)($test.summary.NumHosts)
Total Processors ($test = get-cluster <clustername> | get-view)($test.summary.numcpucores)
Number of Virtual Machines (?)
Hostname (get-cluster | get-vmhost)
System Uptime (get-stat -entity <vmhost> -stat sys.uptime.latest -maxsamples 1)
Total CPU
CPU Usage Average (%) (get-stat -entity <vmhost> -stat cpu.usage.average -maxsamples 1)
CPU UsageMhz Average (Mhz) (get-stat -entity <vmhost> -stat cpu.usagemhz.average -maxsamples 1)
Total Memory
Memory Usage Average (get-stat -entity <vmhost> -stat mem.usage.average -maxsamples 1)
Memory Granted (KB) (get-stat -entity <vmhost> -stat mem.granted.average -maxsamples 1)
Average Network Usage (get-stat -entity <vmhost> -stat net.usage.average -maxsamples 1)
Average Network Received (get-stat -entity <vmhost> -stat net.received.average -maxsamples 1)
Average Network Transmitted (get-stat -entity <vmhost> -stat net.transmitted.average -maxsamples 1)
CPU Usage Average (Cluster) get-stat -entity <clustername> -stat cpu.usagemhz.average -maxsamples 1)
Memory Usage Average (Cluster) get-stat -entity <clustername> -stat mem.usage.average -maxsamples 1)
So it would look somehing like this
Cluster 1
39 Ghz
144 GB
3
12
59
Host1
...
...
Host 2
...
...
Cluster CPU usage
Cluster Memory usage
As you can see, most commands i can get by typing individual commands, but off course i
would like this in 1 script where it can automaticly get the clusters, hosts etc and use those further down in the script.
Export would be nice to be CSV (mayve to html if that can be done..)
So we can make some nice charts where we can see a weekly growth.
Any pointers/help would be greatly appriciated.
Kind regards,
Michiel
For the complete VC:
$Datastores = get-datastore | Sort Name $TotalStore = 0 Foreach ($Datastore in $Datastores){ $TotalStore += $datastore.capacityMB } $Total = "{0:n2}" -f ($TotalStore/1kb) $Total
For a cluster just add the Get-Cluster
$Datastores = Get-Cluster "My Cluster" | get-datastore | Sort Name $TotalStore = 0 Foreach ($Datastore in $Datastores){ $TotalStore += $datastore.capacityMB } $Total = "{0:n2}" -f ($TotalStore/1kb) $Total
If you found this information useful, please consider awarding points for Correct or Helpful.
Alan Renouf
This is also where i strugled when i first started, the key is to create your own object and add each item you want to the object so in the case o the first part of your script below:
Clustername
Total CPU Resources ($test = get-cluster <clustername> | get-view)($test.summary.totalcpu)
Total Memory ($test = get-cluster <clustername> | get-view)($test.summary.totalmemory)
Number of Hosts ($test = get-cluster <clustername> | get-view)($test.summary.NumHosts)
Total Processors ($test = get-cluster <clustername> | get-view)($test.summary.numcpucores)
Number of Virtual Machines (?)
We can do this by first retrieving the cluster object and storing it in a variable, this makes all the repeat calls come from this variable rather than calling the information from the virtual infrastructure each time:
$clusters = Get-Cluster | Get-View
Then we can use this to get our other objects and add them to our uberobject, in the case below i have called this 'uberobject'
$Clusters = Get-Cluster | Get-View $UberObject = @() Foreach ($Cluster in $Clusters){ $Details = "" | Select Name, TotalCPUResource, TotalMemory, NumberofHosts, TotalProcessors, NumberofVMs $Details.Name = $Cluster.Name $Details.TotalCPUResource = $Cluster.summary.totalcpu $Details.TotalMemory = $Cluster.summary.totalmemory $Details.NumberofHosts = $Cluster.summary.NumHosts $Details.TotalProcessors = $Cluster.summary.numcpucores $Details.NumberofVMs = (Get-Cluster $Cluster.Name | Get-VM).Count $UberObject += $Details } $UberObject
Now we can build up our uberobject with each section and then finally export it to csv or html or whatever.
I will leave the rest for you to add
Hope this helps !
If you found this information useful, please consider awarding points for Correct or Helpful.
Alan Renouf
I basically did the same as you are doing with the aim of having a decent report of mine and our customers sites, this is what I came up with, hopefully you'll get some ideas or modify it yourself. A lot of people helped me make it what it is now, they are listed on the page also.
Hope this helps,
Dan
This is great guys
I'll try to add the things i need/want.
Probably be back though!
Thanks for the good pointers/reference!
Kind Regards,
Michiel
Here i am again
Is there a way to sum up al the disk space of the datastores?
I know how to do it for one..
$Datastores = get-datastore | Sort Name
$Store = "" -f ($datastores.capcityMB/1kb) (Took it from the healthcheckscript)
$Store
This works.
What i would like to do is have this sum up all the datastore coupled to a VC or Cluster
Also to sum up the free space and used space but i would assume that would be the same syntax.
Any pointers? Is there a way you can sum it up using something similar to the .count parameter?
Tried searching for it but couldn't find it..
Thanks in advance!
Kind Regards,
Michiel
For the complete VC:
$Datastores = get-datastore | Sort Name $TotalStore = 0 Foreach ($Datastore in $Datastores){ $TotalStore += $datastore.capacityMB } $Total = "{0:n2}" -f ($TotalStore/1kb) $Total
For a cluster just add the Get-Cluster
$Datastores = Get-Cluster "My Cluster" | get-datastore | Sort Name $TotalStore = 0 Foreach ($Datastore in $Datastores){ $TotalStore += $datastore.capacityMB } $Total = "{0:n2}" -f ($TotalStore/1kb) $Total
If you found this information useful, please consider awarding points for Correct or Helpful.
Alan Renouf