使用PowerShell导出为CSV

时间:2019-01-15 14:42:11

标签: powershell csv sharepoint powershell-v3.0

我正在使用PowerShell脚本抓取根站点URL和SharePoint网站中的所有子站点,并输出URL,标题和总用户,如下所示。

Url: http://sourcevideo.f.com | Title: The Source Video | Users: 3345
Url: http://sourcevideo.f.com/AGap | Title: A Gap | Users: 3345
Url: http://sourcevideo.f.com/AVideos | Title: Videos | Users: 417
Url: http://sourcevideo.f.com/BCt | Title: BC Japan | Users: 39

如何将其导出为具有三列的CSV?

代码:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$SiteCollections = Get-SPWebApplication "http://sourcevideo.f.com" |
                   Get-SPSite -Limit All
foreach ($Site in $SiteCollections) {
    foreach ($Web in $Site.AllWebs) {
        Write-Host "Url:"$web.URL "| Title:"$web.Title "| Users:" $web.AllUsers.Count
    }
    Write-Host ""
}

3 个答案:

答案 0 :(得分:1)

要扩展Lee的答案,代码应如下所示:

$WebInfo = @(
“$web.url”
“$web.title”
"$web.allusers.count"
)
$properties = @{'website url'=$WebInfo[0];'website title'=$WebInfo[1];'user count'=$WebInfo[2]}
$WebsiteObject = New-Object -TypeName PSObject -Property $properties
$WebsiteObject | Export-Csv -path "put path here" -NoTypeInformation 

我还没有测试过。

答案 1 :(得分:1)

我认为您需要创建一个新对象

Cursor

这是我写的完整脚本,但是可以测试,因为我没有Sharepoint插件

ReadOnly

答案 2 :(得分:1)

将外部foreach分配给变量并
在内部建立一个[PSCustomObject]

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$SiteCollections = Get-SPWebApplication "http://sourcevideo.f.com" |
                   Get-SPSite -Limit All
$Data = foreach ($Site in $SiteCollections) {
    foreach ($Web in $Site.AllWebs) {
        [PSCustomObject]@{
            Url   = $web.URL
            Title = $web.Title
            Users = $web.AllUsers.Count
        }
    }
}
$Data | Export-Csv .\yourName.csv -NoTypeInformation