【问题标题】:Powershell - How to include array data with flat filePowershell - 如何将数组数据包含在平面文件中
【发布时间】:2015-02-27 18:27:07
【问题描述】:

我正在创建一个包含一些数据的 csv 平面。其中一列是内容类型,它可能在列中具有一种或多种内容类型(字符串值)。如何将数组对象中的所有值包含到平面文件中,但要包含受尊重的行?请参阅下面的代码...我将 $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
                    }
                }
            }
        }
       }
}

【问题讨论】:

    标签: arrays powershell csv output


    【解决方案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
    }
    

    您可以使用 Objects 和 ConvertTo-CSV 更进一步,但首先要确保我们走在正确的轨道上。

    【讨论】:

    • 完美。像魅力一样工作。非常感谢您的详细回答。我真的很感激。你是男人……
    猜你喜欢
    • 1970-01-01
    • 2011-04-25
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2018-02-21
    • 2013-01-07
    • 1970-01-01
    相关资源
    最近更新 更多