mercredi 31 décembre 2014

Powershell - get a list of all the document libraries for a web application including content types - output to csv



I have following code and it works great. However, business unit now wants to see content types for each document library. Most document libraries come with Document and Folder content types. Now, there are some libraries which has 5 or 6 different content types. I need to provide a csv with 1 row for document library (name, url, content types). The content type column should have all the content types in it. I can't get the content type exported to csv. When I do write-host on the content type (write-host $ct) I can see it has all the content types belong to a document library. I understand $ct is an array object and it may need to be handled specially in order for add-content to accept it's data. Any idea how to do this?



#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://ift.tt/13TieJF"
$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
}
}
}
}
}
}







0 commentaires:

Enregistrer un commentaire