Looping a function and outputting results to csv

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 3 years, 9 months ago.

  • Author
    Posts
  • #10042
    Profile photo of ShiftNick
    ShiftNick
    Participant

    I'm new to powershell and I'm now pushing the limits of ability with the following.

    I found the following method for creating a function to gather machine info. Now I'm looking to loop the function for a large group of machines on my network and output the results to a single file or a csv.

    Any help, or links pointing to the answer for this would be appreciated.

    # This section creates a function that calls the info from a WMIObject  #
    function Get-MachineInfo($ServerName="localhost")             
    {            
    get-wmiobject win32_computersystem -ComputerName $ServerName |            
    select DNSHostName, Manufacturer, Model, SystemType ,             
            @{Name="TotalPhysicalMemoryInMB";Expression={"{0:n2}" -f($_.TotalPhysicalMemory/1mb)}},             
            NumberOfLogicalProcessors, NumberOfProcessors, CurrentTimeZone, DaylightInEffect            
    }# End 
    
    # This section calls the function #
    Get-MachineInfo
    
  • #10043
    Profile photo of Don Jones
    Don Jones
    Keymaster
    function Foo {
      $computers = 'one','two','three'
      foreach ($computer in $computers) {
        Get-MachineInfo $computer
      }
    }
    
    Foo | Export-CSV whatever.csv
    

    This'll take a while for a long list, because the computers are going to be contacted sequentially via WMI. Not much you can do about that, really, except drastically modify your approach perhaps.

    Alternately, just don't do this in a function. Create your list of computer names in a text file named computers.txt, listing one computer name per line. Then:

    get-wmiobject win32_computersystem -ComputerName (Get-Content computers.txt) |            
    select DNSHostName, Manufacturer, Model, SystemType ,             
            @{Name="TotalPhysicalMemoryInMB";Expression={"{0:n2}" -f($_.TotalPhysicalMemory/1mb)}},             
            NumberOfLogicalProcessors, NumberOfProcessors, CurrentTimeZone, DaylightInEffect |
    export-csv filename.csv
    

    There isn't any specific need for a function, if this is all it's doing.

  • #10044
    Profile photo of ShiftNick
    ShiftNick
    Participant

    Thanks so much, that worked brilliantly.

  • #10045
    Profile photo of ShiftNick
    ShiftNick
    Participant

    I have a 2nd part now that I've just noticed. Can I find the OS as well. I've noticed that it's part of a different WMI class but I'm not sure how I would include it. Can I just pipe the results of the first get-wmiobject in to the second?

  • #10046
    Profile photo of Don Jones
    Don Jones
    Keymaster

    No, you can't. Combining information from two places into a single output is more complex. If you want a usable CSV file, then you have to query the pieces you want, and then combine them into a unified custom output object.

    Short example:

    function foo ($computers) {
      foreach ($computer in $computers)
        $os = get-wmiobject win32_operatingsystem -computer $computer
        $cs = get-wmiobject win32_computersystem -computer $computer
        $props = @{'ComputerName'=$computer;
                   'OSBuild'=$os.buildnumber;
                   'Mfgr'=$cs.manufacturer;
                   'SPVersion'=$os.servicepackmajorversion}
        New-Object -Type PSObject -Prop $props
      }
    }
    
    foo -computers 'one','two','three' | export-csv output.csv
    

    Check out "Learn PowerShell Toolmaking in a Month of Lunches" (book; MoreLunches.com). It really tackles this exact topic in a lot more depth, with more complete examples. You can even just grab the sample code if you want from the Web site. I'd actually start with "Learn Windows PowerShell 3 in a Month of Lunches" to nail down some of the important core stuff, like how the pipeline works, and how you can tell when you can pipe something to a command and have it work or not.

You must be logged in to reply to this topic.