rbmadison
Contributor
Contributor

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 "

0 Kudos
2 Replies
halr9000
Commander
Commander

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
0 Kudos
hugopeeters
Hot Shot
Hot Shot

Hi rbmadison,

You'd do better to create an object for your output. That way, it's much more flexible. Read about it here:

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:

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

0 Kudos