Solved

Export to CSV Powershell

Posted on 2011-09-08
3
724 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks You Gentlemen, I will make the modification and get back to you.
0

Featured Post

Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Set OWA language and time zone in Exchange for individuals, all users or per database.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

772 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

10 Experts available now in Live!

Get 1:1 Help Now