Solved

Export to CSV Powershell

Posted on 2011-09-08
3
725 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 68

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now