Hello,
I would like to have an script wich would do the following:
Connect to Virtual Center
List all running vm's with: VM Name, Os version, total hard disk sizes in GB, total memory size in GB
export to csv or html
I tried stuff lik get-vm, get-harddisk etc and i can get the information but i would like to get it scripted.
can anyone help?
Hi Styler,
You should either run it interactively (start Powershell; type the full path to the script): the output file is stored in your current directory.
Or run powershell.exe -noExit -command &""
Might be handy to change the script to use the exact path for the output file.
Message was edited by: hugopeeters
Here is what I meant to reply:
Try something like this:
$VIServer = "server.domain.com" $OutputCSV = c:\path\to\file.csv $OutputHTML = c:\path\to\file.html connect-viserver $VIServer # specify credentials, or leave off to be prompted $vm = get-vm $vm | foreach-object { $DiskCapacityGB = $_.Harddisks[0].CapacityKB * 1kb / 1gb Add-Member -inputObject $_ -memberType noteproperty -name DiskCapacityGB -value $DiskCapacityGB } $vm | Export-Csv $OutputCSV $vm | ConvertTo-Html | Set-Content $OutputHTML -Encoding ASCII
First, conect to theVirtual Center.
Connect-VIServer -server serverName
Then, because it's better to play with variables that contain objects then repeatadly hammer the server for information which you then filter (and is slow). With Get-VM you alread have access to the Name, PowerState, Memory and Hard Drives of the guest system.
$VMs = Get-VM # This gets all the VMs in a nice quick format to play with
$PoweredOn = $VM | Where-Object { $_.PowerState -eq "PoweredOn" } # This filters based on power state Use Get-Member to see properties
Now the other trick I swiped from earlier in the forums ( http://communities.vmware.com/message/892201#892201 )
$PoweredOn | select Name, Memory*, @{ Name="TotalDisk"; Expression={ (($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum)/1024 }}
Double check me on the /1024 part, default output is KB for VM, and I am in a hurry so not really checking my math. Just pipe the output to export-cvs or similar. As to getting the guest OS, not something I've needed so off the top of my head don't have it. But show how you are currently doing it I am sure some folks can point to more efficient ways then my answer or we can figure out how to splice it together later.
Steven
Here's a script for you (rename to .ps1 and fire it up).
Hugo
PS: Replace the last line with the following one to export to html:
$myCol | ConvertTo-HTML | Out-File VMs.html
Or remove the last line and dot-source the script to manipulate (sort / filter / format) the output ($myCol) manually.
Hi Hugo,
Tnx for all reactions people!
The script looks good but how do i fire up the ps1? if i run powershell.exe d:\temp\test.ps1 i get a black screen and than it goes away. what am i doing wrong?
Hi Styler,
You should either run it interactively (start Powershell; type the full path to the script): the output file is stored in your current directory.
Or run powershell.exe -noExit -command &""
Might be handy to change the script to use the exact path for the output file.
Message was edited by: hugopeeters
Hal,
I get an error on the Add-Member statement in your code. Any ideas?
Unexpected token 'Add-Member' in expression or statement.
At C:\powershell\scripts\vmguest_info.ps1:6 char:104
+ $vm = get-vm $vm | foreach-object { $DiskCapacityGB = $_.Harddisks[0].CapacityKB * 1kb / 1gb Add-Member <<<< -inputO
bject $_ -memberType noteproperty -name DiskCapacityGB -value $DiskCapacityGB } $vm | Export-Csv $OutputCSV $vm | Conve
rtTo-Html | Set-Content $OutputHTML -Encoding ASCII
I get an error on the Add-Member statement in your code. Any ideas?
I accidentally put in an extra line break. It's fixed above now.
Author of the upcoming book: Managing VMware Infrastructure with PowerShell
Co-Host, PowerScripting Podcast (http://powerscripting.net)
Tnx Hugo this works good!
still going to look at the script of alan! that looks even more amazing
Alan,
Fantastic script! This is alot of what I was looking for and much more. This one is a winner for sure.
Good work mate.
Thanks, lets hope the judges of the vi toolkit powershell comp think so too !
Let me know if you think there is anything missing or anything more that would be usefull, always looking to improve and learn more !
Alan
Hi alan,
Tnx for your tip.
When i execute your script it shows only the following:
PS D:\report> .\Report.ps1
An expression was expected after '('.
At D:\report\Report.ps1:211 char:11
+ .Activate() <<<<
any ideas on whats going wrong?
im using windows vista an office 2007
I tried Windows XP with office 2007 and that seemed ok, Vista was not available to me though, sadly.
It looks like its at the point where it makes the document active, can you double check you dont have word open but hidden (task manager, look for winword), if that doesnt work I will try it at home tonight on my vista/office 2007 install. Will need to build myself a couple of VM's first though
ok,
no winword process running even closed outlook and excel.
I had powershell 1.0 installed, upgraded to powershell 2.0 and still same error.
this is the section it talks about:
$wordDoc
.Activate()
Hi, cant get to my test machine until later, should be ok with Powershell 1 though. I have PM'd you a couple of things to try.
Phew, issue was with my crappy blog and not the code, i have moved it to poshcode.org now !
indeed, now its working fine!
Thanks for the quick responses and good help. the tool is running now... 22 servers so it would take a while