Solved

Export to CSV Powershell

Posted on 2011-09-08
3
726 Views
Last Modified: 2012-06-27
Hi All,

I have written the following code to query the local and remote iis server. The script works fine and produces the desired output however its not able to export the results to csv file. Seems like its outputting the result from the function block itself but not returning results to  FinalResult variable. Can someone debug this and help me with exporting the results to csv file.
Function Get_W3C([String] $Machine)
{

$result = @()


$Webservers = Get-WmiObject -Authentication PacketPrivacy -Impersonation Impersonate `
-Class IISWebserverSetting -Namespace "root\microsoftiisv2" -computerName $Machine

foreach($ws in $webservers) 
    {       
            $temp = "" | Select computerName, Version, SiteName, LogLocation, W3CLoggingFields
            $temp.ComputerName = $Machine
            $temp.Version = Invoke-Command {(Get-ItemProperty HKLM:\Software\Microsoft\InetStp).SetupString} -ComputerName $Machine
            $site = $ws.ServerComment.ToString()
            $temp.SiteName = $site
            $LLoc = $ws.LogFileDirectory.ToString()
            $LnameBfr = $ws.Name
            $LnameAft = $LnameBfr -replace "/", ""
            $temp.LogLocation = $LLoc + "\" + $LnameAft
                                                   
            $siteLoc = "IIS:\Sites\$site"

            $SB = { param($siteLoc)
                  import-module webadministration;
                  (Get-ItemProperty "$siteLoc").logFile.logExtFileFlags
                  } 
        
           $res = Invoke-Command -computerName $Machine -ScriptBlock $SC -ArgumentList $siteLoc
                   
           $temp.W3CLoggingFields = $res
            
           $result += $temp
           
            
    }
    return $result
}

import-module WebAdministration

$FinalResult = @()

$Col = import-csv C:\Scripts\list1.csv

foreach($comp in $col) 
{
      $srv = $comp.ServerName
      
      Get_W3C "$srv"
      
      $FinalResult += $FinalResult
      
  
 }


$FinalResult | Select-Object computerName, Version, SiteName, LogLocation, W3CLoggingFields | `
Export-Csv -Path C:\Scripts\Output-1.csv -NoTypeInformation

#$FinalResult | Select-Object @{Label="computerName";Expression={$_.ComputerName};}, @{Label="Version";Expression={$_.version};}, `
#@{Label="SiteName";Expression={$_.SiteName};}, @{Label="LogLocation";Expression={$_.LogLocation};}, `
#@{Label="W3CLoggingFields";Expression={$_.W3CLoggingFields};} | Export-Csv -Path C:\Scripts\Output.csv -NoTypeInformation

Open in new window

0
Comment
Question by:Navdeep
3 Comments
 
LVL 2

Accepted Solution

by:
gazm81 earned 125 total points
ID: 36508117
I think you missed one line

$temp = Get_W3C "$srv"
 $FinalResult += $temp

or below
Function Get_W3C([String] $Machine)
{

$result = @()


$Webservers = Get-WmiObject -Authentication PacketPrivacy -Impersonation Impersonate `
-Class IISWebserverSetting -Namespace "root\microsoftiisv2" -computerName $Machine

foreach($ws in $webservers) 
    {       
            $temp = "" | Select computerName, Version, SiteName, LogLocation, W3CLoggingFields
            $temp.ComputerName = $Machine
            $temp.Version = Invoke-Command {(Get-ItemProperty HKLM:\Software\Microsoft\InetStp).SetupString} -ComputerName $Machine
            $site = $ws.ServerComment.ToString()
            $temp.SiteName = $site
            $LLoc = $ws.LogFileDirectory.ToString()
            $LnameBfr = $ws.Name
            $LnameAft = $LnameBfr -replace "/", ""
            $temp.LogLocation = $LLoc + "\" + $LnameAft
                                                   
            $siteLoc = "IIS:\Sites\$site"

            $SB = { param($siteLoc)
                  import-module webadministration;
                  (Get-ItemProperty "$siteLoc").logFile.logExtFileFlags
                  } 
        
           $res = Invoke-Command -computerName $Machine -ScriptBlock $SC -ArgumentList $siteLoc
                   
           $temp.W3CLoggingFields = $res
            
           $result += $temp
           
            
    }
    return $result
}

import-module WebAdministration

$FinalResult = @()

$Col = import-csv C:\Scripts\list1.csv

foreach($comp in $col) 
{
      $srv = $comp.ServerName
      
      $temp = Get_W3C "$srv"
      
      $FinalResult += $temp
      
  
 }


$FinalResult | Select-Object computerName, Version, SiteName, LogLocation, W3CLoggingFields | `
Export-Csv -Path C:\Scripts\Output-1.csv -NoTypeInformation

#$FinalResult | Select-Object @{Label="computerName";Expression={$_.ComputerName};}, @{Label="Version";Expression={$_.version};}, `
#@{Label="SiteName";Expression={$_.SiteName};}, @{Label="LogLocation";Expression={$_.LogLocation};}, `
#@{Label="W3CLoggingFields";Expression={$_.W3CLoggingFields};} | Export-Csv -Path C:\Scripts\Output.csv -NoTypeInformation

Open in new window

0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 125 total points
ID: 36508510
Just to show it can be much more compact, and removes some pitfalls like the above, lines 42 to 55 could also look like:
$FinalResult = @()
import-csv C:\Scripts\list1.csv | % {$FinalResult += Get_W3C $_.Servername}

Open in new window

or even more
$FinalResult = @(import-csv C:\Scripts\list1.csv  | % {Get_W3C $_.Servername})

Open in new window

And you are using the wrong (non-existent) script block $SC in line 29 - should be $SB.
0
 
LVL 12

Author Comment

by:Navdeep
ID: 36511856
Thanks You Gentlemen, I will make the modification and get back to you.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question