2 Replies Latest reply on Jul 23, 2008 1:56 AM by hugopeeters

    Powershell Wizard Help

    rbmadison Enthusiast

      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 "

        • 1. Re: Powershell Wizard Help
          halr9000 Master

          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)

          • 2. Re: Powershell Wizard Help
            hugopeeters 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: 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