简体   繁体   English

Powershell 导出数据到 CSV 带列

[英]Powershell export data to CSV with columns

I want to export all users of a SharePoint Farm and found the code below.我想导出 SharePoint 农场的所有用户,并找到下面的代码。 I'm new to PowerShell, so the solution is probably easy, but I struggle with it.我是 PowerShell 的新手,所以解决方案可能很简单,但我很难解决。

The code works well except that it outputs the data in a single column with each row entry with the data all listed after each other ("type,user,group,weburl,webtitle").该代码运行良好,只是它在单个列中输出数据,每个行条目的数据都依次列出(“类型、用户、组、weburl、webtitle”)。 I'd love it when it would output it into 5 columns kinda like this我会喜欢它 output 它分成 5 列有点像这样

type类型 user用户 group团体 weburl网址 webtitle网页标题
type 1 1型 user 1用户 1 group 1第 1 组 weburl 1网址 1 webtitle 1网页标题 1
type 1 1型 user 2用户 2 group 2第 2 组 weburl 1网址 1 webtitle 1网页标题 1
type 1 1型 user 3用户 3 group 1第 1 组 weburl 2网址 2 webtitle 2网页标题 2
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue 
    
$Currentime = get-date -format "yyyyMMdd_hhmmtt" 
$filename = "FarmUsers" 
$datafile = ("{0}{1}.csv" -f $filename, $Currentime) 
    
$headerfile = "type,user,group,weburl,webtitle" 
$headerfile | out-file -FilePath $datafile 
    
$iissitedata = get-spwebapplication  
foreach($farmsite in $iissitedata) 
{ 
    foreach ($SiteCollection in $farmsite.sites) 
    { 
        foreach ($web in $SiteCollection.Allwebs) 
        {  
             foreach ($usersite in $web.users) 
             {       
                    $data = ("RootUser,{0},-,{1},{2}" -f $usersite, $web.url,$web.name)  
                    $data | out-file -FilePath $datafile  -append 
                } 
     
             foreach ($group in $web.Groups) 
            { 
                 foreach ($user in $group.users) 
                 {                           
                          $data = ("GroupUser,{0},{1},{2},{3}" -f $user, $group, $web.url, $web.name) 
                          $data | out-file -FilePath $datafile  -append 
                 } 
            }    
            $web.Dispose() 
        } 
    
    } 
}

What changes do I have to make to the script, so that it outputs into columns?我必须对脚本进行哪些更改才能输出到列中?

Thanks in advance!提前致谢!

Instead of manually formatting each row in the CSV, you'll want to create a series of objects with properties corresponding to the column names you want, and then let Export-Csv take care of constructing the CSV file for you:与其手动格式化 CSV 中的每一行,不如创建一系列具有与所需列名对应的属性的对象,然后让Export-Csv为您构建 CSV 文件:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 
    
$Currentime = Get-Date -format "yyyyMMdd_hhmmtt" 
$filename = "FarmUsers" 
$datafile = ("{0}{1}.csv" -f $filename, $Currentime) 
    
$iissitedata = Get-SPWebApplication  
foreach ($farmsite in $iissitedata)
{ 
    foreach ($SiteCollection in $farmsite.sites)
    { 
        foreach ($web in $SiteCollection.Allwebs)
        {  
            foreach ($usersite in $web.users)
            {
                $data = [pscustomobject]@{
                    type     = "RootUser"
                    user     = $usersite
                    group    = '-'
                    weburl   = $web.url
                    webtitle = $web.name
                }
                $data | Export-Csv -LiteralPath $datafile -NoTypeInformation -Append
            } 
     
            foreach ($group in $web.Groups)
            { 
                foreach ($user in $group.users)
                {                           
                    $data = [pscustomobject]@{
                        type     = "GroupUser"
                        user     = $user
                        group    = $group
                        weburl   = $web.url
                        webtitle = $web.name
                    }
                    $data | Export-Csv -LiteralPath $datafile -NoTypeInformation -Append
                } 
            }    
            $web.Dispose() 
        } 
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM