PowerShell Tip -- OGV is your friend!

Discussion in 'Webmastering & Programming' started by notarat, May 1, 2019.

  1. notarat

    notarat [H]ard|Gawd

    Messages:
    1,703
    Joined:
    Mar 28, 2010
    If you deal with PowerShell a lot like I do you encounter situations where you make adjustments to your code or your output formatting so the data retrieved is presentable in something like an MS Excel spreadsheet.

    Outputting data to a spreadsheet is simple:

    Code:
    (Ex: $Monitor_Array | export-csv C:\Outputfiles\sasasasas.csv -Append -NoTypeInformation)
    
    One thing I always hated about outputting data to a spreadsheet is that you have to then open it up after creation or appending to it, review the data to ensure it's properly presented, then close the spreadsheet.

    This is especially aggravating when the output from the script must be appended to the previous out put file...(at least IMHO, anyway)

    Recently I encountered an issue where I needed to pull the computer name and all attached monitors with their Mfgr, Model, Serial#, Mfgr Year, Mfgr Week, Size, and monitor's Resolution. Additionally, these snippets of information were to be stored into an array piped through a PSCustomObject to create the column headers and store the information needed.

    This script also required the use of more than one type of call/query to the system for monitor information...

    Code:
    $variableA=Get-WmiObject -Namespace "root\WMI" -Class "WMIMonitorID" -ComputerName $Computer -ErrorAction SilentlyContinue
    
    and
    Code:
    $variableb=Get-WmiObject -Namespace 'root\wmi' -ComputerName $Computer -Query "SELECT MaxHorizontalImageSize,MaxVerticalImageSize FROM WmiMonitorBasicDisplayParams"
    
    Properly formatting the output was a bit of a pain because as I was coding the script I added more data elements to it so I had to open and close the spreadsheet several times to verify the new data elements were retrieved and properly formatted in the proper cells.

    The standard output from the script creates an array and pipes it out to a spreadsheet located in the folder I chose. It appends each new instantiation to the end of the previous run so that this script can be invoked against many remote computers in order to provide an inventory of sorts of which computers have which monitors attached, their size and resolution, and all the other bits of info I mentioned above.


    Code:
    $Monitor_Array | export-csv C:\Outputfiles\sasasasas.csv -Append -NoTypeInformation
    
    That's where Output Grid View is handy.

    Code:
    $Monitor_Array | Out-GridView -PassThru
    
    Using OGV opens a pop-up window to present the data...much the same as if you saw it in a spreadsheet, but it does so far faster than it would take to write the data a spreadsheet and then double-click the resulting output file and open it.

    ogv.jpg


    It's not instantaneous (on my slow-assed) workstation but it is several seconds faster than dumping the data to a spreasheet file and then opening the file.

    Additionally, if you need to make a change to formatting or spelling or something like that you simply close the window, make your changes, then press F5 again to re-run the script. You don't have to worry about deleting the old file or appending un-necessary data to the spreadsheet.
     
    FNtastic likes this.
  2. FNtastic

    FNtastic [H]ard|Gawd

    Messages:
    1,419
    Joined:
    Jul 6, 2013
    Cool. Thanks for sharing. Unfortunately, I end up writing a lot of powershell as well (even though I don't want to)