none
New-Object order RRS feed

  • Question

  • Hello,

    I am using the Out-GridView command from a data collected with the New-Object command.

    However, the order of the output heading is not in the same order as the one defined in the New-Object command.

    Could you please advice.

    Thank you in advance.

    Here is the code used:

       $ObjData = @()

       $Required = $Services = $Pathname = $FileInfo = ""

       $Required = Get-service -Computer $CompName | Where {$_.Name -eq "WinCollect"} | Select RequiredServices

       $Services = Get-WMIObject Win32_Service -Computer $CompName | Where {$_.Name -eq "WinCollect"} |
                         Select Name, Status, PathName, Started, StartName, State

       #$Process = Get-Process -Computer $CompName | Where {$_.Processname -eq "WinCollect" -or $_.ProcessName -eq "WinCollectSvc"}

       $Pathname = ("\\" + $CompName + "\" + ($Services.PathName).replace(":", "$")).Replace("""", "")
       $FileInfo = Get-Item $Pathname

       $ObjData += New-Object PsObject -Property  @{
           "Server Name"   = $CompName
           "Service_Name"  = $Services.Name
           "Status"        = $Services.Status
           "Started"       = $Services.STarted
           "Start Name"    = $Services.StartName
           "State"         = $Services.State
           "Path Name"     = $Services.PathName
           "File Size"     = $FileInfo.length
           "File Version"  = $FileInfo.VersionInfo.FileVersion
           "File Creation" = $FileINfo.CreationTime
           "Last Access"   = $FileINfo.LastAccessTime
           "Required Srvc" = $Required.RequiredServices.Name
       }

    $ObjData | Out-GridView -Title "QRadar WinCollect"


    Learning PowerShell scripting

    Thursday, February 13, 2014 11:48 AM

Answers

All replies

  • Hi Mario,

    you could try this:

    $ObjData | Select "Server Name", "Service_Name", ... | Out-GridView -Title "QRadar WinCollect"

    or

    You could compile a result class in C# and add it as a type. And plenty other ways are available. But this one I think is the easiest, as one helpful soul once showed me right here on Technet :)Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, February 13, 2014 12:46 PM
  • If you don't mind changing how the data is displayed slightly you could use an ordered hash table:

      $Required = $Services = $Pathname = $FileInfo = ""

       $Required = Get-service -Computer $CompName | Where {$_.Name -eq "WinCollect"} | Select RequiredServices

       $Services = Get-WMIObject Win32_Service -Computer $CompName | Where {$_.Name -eq "WinCollect"} |
                          Select Name, Status, PathName, Started, StartName, State

       #$Process = Get-Process -Computer $CompName | Where {$_.Processname -eq "WinCollect" -or $_.ProcessName -eq "WinCollectSvc"}

       $Pathname = ("\\" + $CompName + "\" + ($Services.PathName).replace(":", "$")).Replace("""", "")
        $FileInfo = Get-Item $Pathname

       $ObjData = [ordered] @{
            "Server Name"   = $CompName;
            "Service_Name"  = $Services.Name;
            "Status"        = $Services.Status;
            "Started"       = $Services.STarted;
            "Start Name"    = $Services.StartName;
            "State"         = $Services.State;
            "Path Name"     = $Services.PathName;
            "File Size"     = $FileInfo.length;
            "File Version"  = $FileInfo.VersionInfo.FileVersion;
            "File Creation" = $FileINfo.CreationTime;
            "Last Access"   = $FileINfo.LastAccessTime;
            "Required Srvc" = $Required.RequiredServices.Name;
        }


    $ObjData | Out-GridView -Title "QRadar WinCollect"


    • Edited by Braham20 Thursday, February 13, 2014 1:01 PM
    Thursday, February 13, 2014 1:00 PM
  • Or you could even do this:

       $Required = $Services = $Pathname = $FileInfo = ""

       $Required = Get-service -Computer $CompName | Where {$_.Name -eq "WinCollect"} | Select RequiredServices

       $Services = Get-WMIObject Win32_Service -Computer $CompName | Where {$_.Name -eq "Wincollect"} |
                          Select Name, Status, PathName, Started, StartName, State

     #$Process = Get-Process -Computer $CompName | Where {$_.Processname -eq "WinCollect" -or $_.ProcessName -eq "WinCollectSvc"}

       $Pathname = ("\\" + $CompName + "\" + ($Services.PathName).replace(":", "$")).Replace("""", "")
        $FileInfo = Get-Item $Pathname

       $ObjData = new-object System.Object

    $objdata | Add-member -type NoteProperty ServerName  $compname
    $objdata | Add-member -type NoteProperty Service_Name $Services.name
    $objdata | Add-member -type NoteProperty Status $Services.status
    $objdata | Add-member -type NoteProperty Started $services.started
    $objdata | Add-member -type NoteProperty State $services.state
    $objdata | Add-member -type NoteProperty PathName $Services.Pathname
    $objdata | Add-member -type NoteProperty FileSize $FileInfo.Length
    $objdata | Add-member -type NoteProperty FileVersion $FileInfo.VersionInfo.FileVersion
    $objdata | Add-member -type NoteProperty FileCreation $FileInfo.Creationtime
    $objdata | Add-member -type NoteProperty LastAccess $FileInfo.LastAccessTime
    $objdata | Add-member -type NoteProperty RequiredSrvc $Required.RequiredServices.Name


    $ObjData | Out-GridView -Title "QRadar WinCollect"

    Although I'm sure there are probably nicer looking, less repetitive, ways of accomplishing the same thing.

    Thursday, February 13, 2014 3:14 PM
  • If you're using at least PowerShell 3.0, this syntax is the simplest way of accomplishing what you need (and also takes no performance hit):

       $ObjData += [pscustomobject] @{
            "Server Name"   = $CompName
            "Service_Name"  = $Services.Name
            "Status"        = $Services.Status
            "Started"       = $Services.STarted
            "Start Name"    = $Services.StartName
            "State"         = $Services.State
            "Path Name"     = $Services.PathName
            "File Size"     = $FileInfo.length
            "File Version"  = $FileInfo.VersionInfo.FileVersion
            "File Creation" = $FileINfo.CreationTime
            "Last Access"   = $FileINfo.LastAccessTime
            "Required Srvc" = $Required.RequiredServices.Name
        }

    If you are using PoweShell 2.0, the most common solution seems to be using Select-Object to force the properties into a certain order.
    Thursday, February 13, 2014 3:18 PM
  • The [ordered] works fine in vertical format, but not for headings.

    Thank you for your advice.


    Learning PowerShell scripting


    Thursday, February 13, 2014 3:45 PM
  • Your suggestion works, although lots of typing.

    Learning PowerShell scripting

    Thursday, February 13, 2014 3:46 PM
  • This is closer to what you want.  I have removed most of the unnecessary code.  Try to keep the visual noise level to a minimum as it makes your code easier to manage, understand and debug.  All of the extra code shows a weak understanding of how PowerShell actually works.

    $Required = Get-service WinCollect -Computer $CompName
    $Services = Get-WMIObject Win32_Service -Computer $CompName -Filter 'Name="WinCollect"'
    $Pathname = ("\\$CompName\$($Services.PathName)").replace(":", "$")
    $FileInfo = Get-Item $Pathname
    New-Object PsObject -Property ([ordered]@{
        'Server Name'   = $CompName
        'Service_Name'  = $Services.Name
        'Status'        = $Services.Status
        'Started'       = $Services.STarted
        'Start Name'    = $Services.StartName
        'State'         = $Services.State
        'Path Name'     = $Services.PathName
        'File Size'     = $FileInfo.length
        'File Version'  = $FileInfo.VersionInfo.FileVersion
        'File Creation' = $FileINfo.CreationTime
        'Last Access'   = $FileINfo.LastAccessTime
        'Required Srvc' = $Required.RequiredServices.Name
    }) | 
    Out-GridView -Title 'QRadar WinCollect'
    
    



    ¯\_(ツ)_/¯

    Thursday, February 13, 2014 4:07 PM
  • [pscustomobject] @{}

    is a shorter version of:

    New-Object psobject -Property ([ordered]@{} )

    The end result is the same; PowerShell treats the hashtable literals as ordered in both cases.

    Thursday, February 13, 2014 4:11 PM
  • Yes - [pscustomobject] can take a constructor that is a hashtable.  It is shorter and it does provide a handy syntax for generating ordered property objects.  I don't use it too often as most have no install V3 but I see that is changing now.


    ¯\_(ツ)_/¯

    Thursday, February 13, 2014 4:16 PM
  • The [ordered] syntax didn't exist until PowerShell 3.0 either, so you may as well use [pscustomobject] in that case.  (Though you can accomplish the same thing in PowerShell 2.0 by building an OrderedDictionary yourself, rather than using the literal / accelerator syntax:)

    $properties = New-Object System.Collections.Specialized.OrderedDictionary
    
    $properties['FirstProperty'] = 'Something'
    $properties['SecondPrpoerty'] = 'Something Else'
    $properties['ThirdProperty'] = 'Etc...'
    
    $object = New-Object psobject -Property $properties

    Takes a couple of extra lines of code, but it gets the job done if you really want to use [ordered] in v2.0.
    Thursday, February 13, 2014 4:20 PM
  • Thank you! That did the trick.

    Learning PowerShell scripting

    Thursday, February 13, 2014 7:00 PM
  • Thank you! That did the trick.

    Learning PowerShell scripting

    Just a note - you're supposed to mark the posts that answered your question as the answers, not your reply to them. Feel free to disregard this, but now at least you'll know for next time.

    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Thursday, February 13, 2014 7:19 PM