Solved

Export to CSV Powershell

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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…
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

733 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