Write-Output to file ForEach loop

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 9 months ago.

  • Author
    Posts
  • #5543

    by netman06 at 2013-02-13 10:11:13

    Hello,

    I have never needed to have a ForEach loop, output it's data other then a one liner to a file.

    So how can I perform it using this code. Since I would like to use either the Export-Output or maybe Convert-To-HTML, but need to figure out how to perform the output first.


    ForEach ($Site in $ADSites)
    { ## OPEN ForEach Site in ADSites
    $SiteName = $Site.Name
    $SiteSubnets = $Site.Subnets
    $SiteServers = $Site.Servers
    $SiteAdjacentSites = $Site.AdjacentSites
    $SiteSiteLinks = $Site.SiteLinks
    $SiteInterSiteTopologyGenerator = $Site.InterSiteTopologyGenerator
    $SiteOptions = $Site.Options

    Write-Output “Site Name: $SiteName `r”
    Write-Output “Site Subnets: $SiteSubnets `r”
    Write-Output “Site Servers: $SiteServers `r”
    Write-Output “Adjacent Sites: $SiteAdjacentSites `r”
    Write-Output “Site Links: $SiteSiteLinks `r”
    Write-Output “Site InterSiteTopologyGenerator: $SiteInterSiteTopologyGenerator `r”
    Write-Output “Site Options: $SiteOptions `r”
    Write-Output " `r" | Out-File "C:\My Scripts\Results.txt"

    This is the line that is casing me troubles.

    Write-Output " `r" | Out-File "C:\My Scripts\Results.txt"

    I do not know how to make it output to a file.

    Any Ideas would be great.

    Thanks, Mike

    by DonJ at 2013-02-13 10:32:58

    You need to create a custom object and output that. Right now you're outputting formatted text – bad.


    $data = @{'Site Name'=$sitename;'Site Subnets'=$SiteSubnets;'Site Servers'=$SiteServers}
    $obj = New-Object -Type PSObject -Prop $data
    Write-Output $obj

    Something like that. You can add as many pieces of data to that hash table as you need – I added three as an example. Let's assume this is all in a script called MyScript.ps1.


    .\MyScript.ps1 | ConvertTo-HTML | Out-File whatever.html

    Is how you convert the output. Don't put Out-File into the script itself, or you'll eliminate the possibility of converting it properly.

    by netman06 at 2013-02-13 13:17:59

    Hi Don,

    So this is how I set it up:


    ForEach ($Site in $ADSites)
    { ## OPEN ForEach Site in ADSites
    $SiteName = $Site.Name
    $SiteSubnets = $Site.Subnets
    $SiteServers = $Site.Servers
    $SiteAdjacentSites = $Site.AdjacentSites
    $SiteSiteLinks = $Site.SiteLinks
    $SiteInterSiteTopologyGenerator = $Site.InterSiteTopologyGenerator
    $SiteOptions = $Site.Options

    $data = @{'Site Name'=$sitename;'Site Subnets'=$SiteSubnets;'Site Servers'=$SiteServers;'$SiteAdjacentSites'=$Site.AdjacentSites;'$SiteSiteLinks'=$Site.SiteLinks;'$SiteInterSiteTopologyGenerator'=$Site.InterSiteTopologyGenerator;'$SiteOptions'=$Site.Options}
    $obj = New-Object -Type PSObject -Prop $data
    Write-Output $obj

    But it seems to not output to file in the loop, can you see any problem in my ForEach statement?

    Thanks,

    by DonJ at 2013-02-13 13:27:17

    It won't output to the file. You need to do what I told you above: PIPE the results of the script to another command, like ConvertTo-HTML or Out-File. The script is just producing the data (objects) – you can then PIPE those to another command to export them, convert them, etc.

    If you want this all in one script:


    function DoSites {
    ForEach ($Site in $ADSites)
    { ## OPEN ForEach Site in ADSites
    $SiteName = $Site.Name
    $SiteSubnets = $Site.Subnets
    $SiteServers = $Site.Servers
    $SiteAdjacentSites = $Site.AdjacentSites
    $SiteSiteLinks = $Site.SiteLinks
    $SiteInterSiteTopologyGenerator = $Site.InterSiteTopologyGenerator
    $SiteOptions = $Site.Options

    $data = @{'Site Name'=$sitename;'Site Subnets'=$SiteSubnets;'Site Servers'=$SiteServers;'$SiteAdjacentSites'=$Site.AdjacentSites;'$SiteSiteLinks'=$Site.SiteLinks;'$SiteInterSiteTopologyGenerator'=$Site.InterSiteTopologyGenerator;'$SiteOptions'=$Site.Options}
    $obj = New-Object -Type PSObject -Prop $data
    Write-Output $obj
    }
    DoSites | Out-File whatever.txt

    Write-Output puts objects in the PowerShell pipeline... from there, other commands can do whatever they want with them. Consider looking into my "Toolmaking" book (powershellbooks.com). It covers this whole approach and the technique... it's the 'right' way to be building these things in PowerShell.

    by netman06 at 2013-02-13 14:54:28

    Hi Don,

    Yes, that works now. I did purchase your book, I have not yet read it, so this will make me.

    One last question, is where is the out-file sorting happening, since your this new method, it seems that my data is not in the correct order, like when I was using the Write-Output method.

    I would think that this is performed at the $data – @{'SiteName, etc level. I tried a Sort-Object before the Out-File whatever.txt, but that did not work either.

    Thanks,

You must be logged in to reply to this topic.