Powershell - 如何使用平面文件包含数组数据

时间:2014-12-31 18:29:45

标签: arrays powershell csv output

我正在使用一些数据创建一个csv flat。其中一列是内容类型,其中列中可能包含一个或多个内容类型(字符串值)。如何将数组对象中的所有值包含到平面文件中但是包含受尊重的行?请参阅下面的代码...我将$ ct传递给add-content但平面文件中没有内容类型数据。当我为$ ct写主持人时,我发现它有价值。请建议。

#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 个答案:

答案 0 :(得分:1)

如果要将数据包含在csv文件中,以后可以将其转换为数组,则只需将其存储为分隔字符串即可。分隔符显然不能与您在平面文件中使用的分隔符相同。在其基本形式中,您可以使用-split-join

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

在上下文中,我们可以做出更好的改变。取代这个if语句

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

您可以改为执行以下操作。

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
}

您可以使用对象和ConvertTo-CSV进一步使用,但请确保我们首先使用正确的轨道。