0

I am creating a csv flat with some data. One of the column is content type which may have one or more content types in the column (string values). How do I include all the values from the array object into the flat file but with the respected row? See the code below... I passed $ct to the add-content but flat file does not have the content type data in it. when I do write-host for $ct I see that it has values. Please suggest.

#For Output file generation
$OutputFN = "Libraries.csv"
#delete the file, If already exist!
if (Test-Path $OutputFN)
 {
    Remove-Item $OutputFN
 }
#Write the CSV Headers
Add-Content $OutputFN "Web URL, Site Name, Library Name, Content Types"


$systemlibs =@("Converted Forms", "Customized Reports", "Documents", "Form Templates",  
                              "Images", "List Template Gallery", "Master Page Gallery", "Pages",  
                               "Reporting Templates", "Site Assets", "Site Collection Documents", 
                               "Site Collection Images", "Site Pages", "Solution Gallery",  
                               "Style Library", "Theme Gallery", "Web Part Gallery", "wfpub")

#Get the Site collection 
$Site= Get-SPSite "http://inside.contoso.com"
$spWebApp = $Site.WebApplication
foreach($allSites in $spWebApp.Sites)
{
       #Loop through all Sub Sites
       foreach($Web in $allSites.AllWebs)
       {
        #Write-Host "-----------------------------------------------------"
        #Write-Host "Site Name: '$($web.Title)' at $($web.URL)"
        #Write-Host "-----------------------------------------------------"
        foreach($list in $Web.Lists)
        {
            if($list.BaseTemplate -eq "DocumentLibrary" -and $list.AllowContentTypes -eq $true)             
            {
                if(-not ($systemlibs -Contains $list.Title))
                {
                    if ($list.AllowContentTypes -eq $true)
                    {
                        $ct = @()
                        foreach ($contenttype in $list.ContentTypes)
                        {
                            $ctProperties = @{ContentType = $contenttype.Name}                          
                            $ctObject = New-Object PSObject -Property $ctProperties
                            #write-host $ct
                            $ct += $ctObject
                            #write-host $ct
                            #Write-Host "$($web.URL), $($web.Title), $($list.Title), $ct"                           
                        }
                        #$ct
                        write-host $ct

                    #Write-Host "$($web.URL), $($web.Title), $($list.Title), $($ct)" 
                    $content = $web.URL + "," + $web.Title +"," + $list.Title +"," + $ct
                    add-content $OutputFN $content
                    }
                }
            }
        }
       }
}

1 Answer 1

1

If you wanted to include data in a csv file that could be later converted to an array you could just store it as a delimited string. The delimiter obviously cannot be the same as the one you are using in your flat file. In its basic form you could be using -split and -join

$array = @("a","b","c")
Add-Content $path ($array -join ";")

In context we could make a couple of changes for the better. In place of this if statement

if ($list.AllowContentTypes -eq $true){
    # Stuff happens here               
}

You could instead do the following.

if ($list.AllowContentTypes -eq $true){
    $ct = $list.ContentTypes | Select-Object -ExpandProperty Name
    $content = "$($web.URL), $($web.Title), $($list.Title), $($ct -join ";")"
    add-content $OutputFN $content
}

You could take this farther using Objects and ConvertTo-CSV but lets be sure we are on the right track first.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. worked like charm. Thank you so much for detailed answer. I truly appreciated it. You r the man......

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.