Omid
Omid

Reputation: 11

Powershell IIS Get-Binding list export-csv issue System.Object

I'm trying to get a script that lists the IIS site name, FilePath and Bindings, which works fine expect when I export-csv. When doing so I get System.Object[] in my results for the path and not sure how to correct this.

Get-WebBinding | % {
$names = $_.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', '$1'
$path = get-website $names | select-object physicalpath
New-Object psobject -Property @{
    Binding = $_.bindinginformation.Split(":")[-1]
    path = $path
    Name = $names
}    } | export-csv c:\test1.csv -notypeinformation

I actually created a more elegant solution for this

Solution:

Import-Module WebAdministration
$hostname = hostname
$Websites = Get-ChildItem IIS:\Sites
$date = (Get-Date).ToString('MMddyyyy')
foreach ($Site in $Websites) {
    $Binding = $Site.bindings
    [string]$BindingInfo = $Binding.Collection
    [string[]]$Bindings = $BindingInfo.Split(" ")#[0]
    $i = 0
    $status = $site.state
    $path = $site.PhysicalPath
    $fullName = $site.name
    $state = ($site.name -split "-")[0]
    $Collection = ($site.name -split "-")[1]
    $status = $site.State
    $anon = get-WebConfigurationProperty -Filter /system.webServer/security/authentication/AnonymousAuthentication -Name Enabled -PSPath IIS:\sites -Location $site.name | select-object Value
    $basic = get-WebConfigurationProperty -Filter /system.webServer/security/authentication/BasicAuthentication -Name Enabled -PSPath IIS:\ -location $site.name | select-object Value
    Do{
        if( $Bindings[($i)] -notlike "sslFlags=*"){
            [string[]]$Bindings2 = $Bindings[($i+1)].Split(":")
            $obj = New-Object PSObject
            $obj | Add-Member Date $Date
            $obj | Add-Member Host $hostname
            $obj | Add-Member State $state
            $obj | Add-Member Collection $Collection
            $obj | Add-Member SiteName $Site.name
            $obj | Add-Member SiteID $site.id
            $obj | Add-member Path $site.physicalPath
            $obj | Add-Member Protocol $Bindings[($i)]
            $obj | Add-Member Port $Bindings2[1]
            $obj | Add-Member Header $Bindings2[2]
            $obj | Add-member AuthAnon $Anon.value
            $obj | Add-member AuthBasic $basic.value
            $obj | Add-member Status $status
            $obj #| export-csv "c:\temp\$date-$hostname.csv" -Append -notypeinformation
            $i=$i+2
        }
        else{$i=$i+1}
    } while ($i -lt ($bindings.count))
}

Upvotes: 0

Views: 2960

Answers (1)

Trondh
Trondh

Reputation: 3351

This happens because CSV output is really crappy at nested objects. If you take a look at your code, your "path" attribute is basically a "slice" of the websiteobject. What you want to do is probably expand out that variable so that you get back the actual value (see example below). This is one of those times where 2 minutes of debugging in the ISE can be really helpful!

Get-WebBinding | ForEach-Object {
    $names = $_.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', '$1'
    $path = get-website $names | select-object -expandproperty physicalpath
    New-Object psobject -Property @{
        Binding = $_.bindinginformation.Split(":")[-1]
        path = $path
        Name = $names
    }    
    }| export-csv c:\test1.csv -notypeinformation

Upvotes: 1

Related Questions