Creating 3 column CSV output

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of iTodd iTodd 3 years, 7 months ago.

  • Author
    Posts
  • #10195
    Profile photo of iTodd
    iTodd
    Participant

    I'm a bit lost 🙁

    Im trying to build a CSV file with 3 columns reporting on content found within a text file across multiple servers. I've got to the point where I can get the results I want for one server but when I try to add results from additional servers into another "all results" type object I hit problems.

    I'd like to get to a point where I can export to csv and end up with 3 columns with a header titling each property and rows with values of queried server as server,primary,failover.

    I've tried a custom object approach and now a string object approach but in both cases I seem to write the result of one server to one object but can't figure out how to get multiple results into one object (to then pipe to exportto-csv).

    Hope that makes sense. How do I do this.. what am I missing?

    Script so far looks like this

    $results = @()
    $outputfile = "c:\temp\export.csv"

    function Audit-DBconfig ($server){
    Try {$dsn = Get-Content "\\$server\c$\test.dsn" -ErrorAction Stop}

    Catch {Write-Warning "DSN File not found"}

    $Object = "" | Select-Object Server, Primary, Failover
    $object.Server = $server
    $object.Primary = (($dsn | Select-String -SimpleMatch "database").Line -split "=")[1]
    $object.Failover = (($dsn | Select-String -SimpleMatch "failover").Line -split "=")[1]

    $results += $Object
    }

    "server1","server2" | %{Audit-DBconfig $_}

    $results # I'd hope to see results for server1 and server2 here but instead just get server2
    #end of script

    I also tried the approach below but hit a wall there when trying to add the second server I'm trying to add properties and values that already exist... arrgghhh I'm sure this is simple to do but I can't get my head around it

    function Audit-DBconfig ($server){
    Try {$dsn = Get-Content "\\$server\c$\test.dsn" -ErrorAction Stop}

    Catch {Write-Warning "DSN File not found"}

    $object | Add-Member NoteProperty "Server" $server
    $object | Add-Member NoteProperty "Primary" (($dsn | Select-String -SimpleMatch "database").Line -split "=")[1]
    $object | Add-Member NoteProperty "Failover" (($dsn | Select-String -SimpleMatch "failover").Line -split "=")[1]

    #$Object | Select-Object server,primary,failover
    Write-Output $object
    }

  • #10197
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Hi Todd,

    Give this a try. Rather than having the function try to append to an array in the parent scope (which may work, but is not great design), I've modified it to accept pipeline input and to output one object at a time. If you assign the output of the function to a variable, PowerShell will turn it into an array for you.

    function Audit-DBConfig {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
            [System.String]
            $Server
        )
    
        process {
            try {
                $dsn = Get-Content "\\$Server\c$\test.dsn" -ErrorAction Stop
            } catch {
                Write-Warning "DSN File not found"
            }
    
            $object = "" | Select-Object Server, Primary, Failover
            $object.Server = $server
            $object.Primary = (($dsn | Select-String -SimpleMatch "database").Line -split "=")[1]
            $object.Failover = (($dsn | Select-String -SimpleMatch "failover").Line -split "=")[1]
    
            Write-Output $object
        }
    }
    
    $results = "server1","server2" | Audit-DBConfig
    
    $results 
    
  • #10198
    Profile photo of iTodd
    iTodd
    Participant

    Thanks Dave, that works a treat... and (more surprisingly) your explanation makes perfect sense to me.

You must be logged in to reply to this topic.