sTyLeR_1
Contributor
Contributor

Dump vm information

Jump to solution

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?

0 Kudos
1 Solution

Accepted Solutions
hugopeeters
Hot Shot
Hot Shot

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

View solution in original post

0 Kudos
17 Replies
halr9000
Commander
Commander

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

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
sepeck
Contributor
Contributor

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

hugopeeters
Hot Shot
Hot Shot

Here's a script for you (rename to .ps1 and fire it up).

Hugo

www.peetersonline.nl

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.

0 Kudos
sTyLeR_1
Contributor
Contributor

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?

0 Kudos
hugopeeters
Hot Shot
Hot Shot

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

View solution in original post

0 Kudos
alanrenouf
VMware Employee
VMware Employee

You may also find my reporting script usefull for other information....

Alan Renouf

VMware, Citrix, Microsoft Consultant

UK

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
kcvman
Contributor
Contributor

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

0 Kudos
halr9000
Commander
Commander

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)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
sTyLeR_1
Contributor
Contributor

Tnx Hugo this works good!

still going to look at the script of alan! that looks even more amazing Smiley Happy

0 Kudos
kcvman
Contributor
Contributor

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.

0 Kudos
alanrenouf
VMware Employee
VMware Employee

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

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
sTyLeR_1
Contributor
Contributor

Hi alan,

Tnx for your tip.

When i execute your script it shows only the following:

PS D:\report&gt; .\Report.ps1

An expression was expected after '('.

At D:\report\Report.ps1:211 char:11

+ .Activate() &lt;&lt;&lt;&lt;

any ideas on whats going wrong?

im using windows vista an office 2007

0 Kudos
alanrenouf
VMware Employee
VMware Employee

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 Smiley Happy

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
sTyLeR_1
Contributor
Contributor

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()

0 Kudos
alanrenouf
VMware Employee
VMware Employee

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.

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
alanrenouf
VMware Employee
VMware Employee

Phew, issue was with my crappy blog and not the code, i have moved it to poshcode.org now !

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
sTyLeR_1
Contributor
Contributor

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 Smiley Happy

0 Kudos