Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Export to CSV Powershell

Posted on 2011-09-08
3
Medium Priority
?
743 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Accepted Solution

by:
gazm81 earned 500 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 71

Assisted Solution

by:Qlemo
Qlemo earned 500 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

688 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