Powershell 使用列将数据导出到 CSV
Powershell export data to CSV with columns
我想导出 SharePoint 场的所有用户并找到下面的代码。我是 PowerShell 的新手,所以解决方案可能很简单,但我很难解决。
该代码运行良好,只是它在单列中输出数据,每一行条目的数据都在彼此之后列出(“类型、用户、组、weburl、webtitle”)。当它像这样将它输出到 5 列时我会喜欢它
type
user
group
weburl
webtitle
type 1
user 1
group 1
weburl 1
webtitle 1
type 1
user 2
group 2
weburl 1
webtitle 1
type 1
user 3
group 1
weburl 2
webtitle 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()
}
}
}
我想导出 SharePoint 场的所有用户并找到下面的代码。我是 PowerShell 的新手,所以解决方案可能很简单,但我很难解决。
该代码运行良好,只是它在单列中输出数据,每一行条目的数据都在彼此之后列出(“类型、用户、组、weburl、webtitle”)。当它像这样将它输出到 5 列时我会喜欢它
type | user | group | weburl | webtitle |
---|---|---|---|---|
type 1 | user 1 | group 1 | weburl 1 | webtitle 1 |
type 1 | user 2 | group 2 | weburl 1 | webtitle 1 |
type 1 | user 3 | group 1 | weburl 2 | webtitle 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()
}
}
}