Skip navigation
VMware

This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
390 Views 2 Replies Last post: Jul 23, 2008 1:56 AM by hugopeeters RSS
rbmadison Enthusiast 154 posts since
Apr 3, 2006
Currently Being Moderated

Jul 21, 2008 12:58 PM

Powershell Wizard Help

I wrote the code below to create a table for a snapshot report. It's not the best in powershell coding techniques so I was hoping someone could offer suggestions on improving speed and shorten the number of lines needed. I added the $Snapshot.created filter because it seems to run faster with the that. Right now I can also only get the report to run to by piping it to a file using the command line below. I'd like to be able to use some cmdlet to get the output. I also thought about using XML to display the data in HTML but I'm not familiar with XML.

 

Any help on improving this would be greatly appreciated.

 

 

Run it using : ./listsnapshotreport.ps1 | report.htm

 

======== ListSnapshotreport.ps1 ========

 

$null = Get-VIServer "

halr9000 Master vExpert 1,124 posts since
Jun 7, 2007
Currently Being Moderated
1. Jul 21, 2008 4:19 PM in response to: rbmadison
Re: Powershell Wizard Help

Have you had a chance to check out the ConverTo-Html cmdlet?

 

NAME
    ConvertTo-Html

SYNOPSIS
    Creates an HTML page that represents an object or a set of objects.


SYNTAX
    ConvertTo-Html [-inputObject <psobject>] [[-property] <Object[]>] [-body <string[]>] [-h
    ead <string[]>] [-title <string>] [<CommonParameters>]


DETAILED DESCRIPTION
    The ConvertTo-HTML cmdlet creates an HTML page that represents one or more objects. The
    cmdlet returns a complete HTML page with the input objects in an HTML table on the page.
     The object's properties are column headings and the property values are listed in the t
    able rows.

 

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
hugopeeters Hot Shot 156 posts since
Jan 10, 2008
Currently Being Moderated
2. Jul 23, 2008 1:56 AM in response to: rbmadison
Re: Powershell Wizard Help

 

Hi rbmadison,

 

 

You'd do better to create an object for your output. That way, it's much more flexible. Read about it here: http://www.peetersonline.nl/index.php/powershell/pimp-your-output-use-objects

 

 

Below is my script that checks for snapshots. You can easily adapt it to create HTML output by piping the output $myCol to ConvertTo-HTML. See this article for more help using HTML output: http://www.microsoft.com/technet/scriptcenter/resources/pstips/jan08/pstip0104.mspx

 

 

Hope this helps.

 

 

================================

 

 

$VC = "server.domain.ext"

 

*

Write-Host "Connecting to VC server $VC ..."

 

*

 

$connectingVC = Get-VIServer $VC

 

*

Write-Host "Locating all virtual servers"

 

*

 

$vms = Get-VM

 

 

$myCol = @()

 

 

$i=1

 

 

ForEach ($vm in $vms)

 

 

{

 

*

Write-Progress "Searching for snapshots" "Percent complete" +-PercentComplete +(100*$i/$vms.Length)

 

*

 

$snapshots = Get-SnapShot -VM $vm

 

 

ForEach ($snapshot in $snapshots)

 

 

{

 

 

$myObj = "" | Select-Object VM, Snapshot, Created

 

 

$myObj.VM = $vm.name

 

 

$myObj.Snapshot = $snapshot.name

 

 

$myObj.Created = $snapshot.created

 

 

$myCol += $myObj

 

 

}

 

 

$i++

 

 

}

 

 

  1. Creating output

 

 

$myCol | Sort-Object VM | Format-Table -AutoSize

 

 

Bookmarked By (0)

Share This Page

Communities