[英]Powershell export data to CSV with columns
我想導出 SharePoint 農場的所有用戶,並找到下面的代碼。 我是 PowerShell 的新手,所以解決方案可能很簡單,但我很難解決。
該代碼運行良好,只是它在單個列中輸出數據,每個行條目的數據都依次列出(“類型、用戶、組、weburl、webtitle”)。 我會喜歡它 output 它分成 5 列有點像這樣
類型 | 用戶 | 團體 | 網址 | 網頁標題 |
---|---|---|---|---|
1型 | 用戶 1 | 第 1 組 | 網址 1 | 網頁標題 1 |
1型 | 用戶 2 | 第 2 組 | 網址 1 | 網頁標題 1 |
1型 | 用戶 3 | 第 1 組 | 網址 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()
}
}
}
我必須對腳本進行哪些更改才能輸出到列中?
提前致謝!
與其手動格式化 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.