string 在 PowerShell 中将 System.Object[] 导出为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41672713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exporting System.Object[] to string in PowerShell
提问by Brian
I created a script that basically iterates through all lists in a site and its subsites and lists the permissions for each document or item.
我创建了一个脚本,它基本上遍历站点及其子站点中的所有列表,并列出每个文档或项目的权限。
The script works, however, when being written to the CSV file the permissions (Member + Role) string gets written as System.Object[]
instead of a proper string in the CSV output (e.g. Member: user123 Role: Full Control
).
然而,该脚本在写入 CSV 文件时有效,权限(成员 + 角色)字符串被写入System.Object[]
而不是 CSV 输出中的正确字符串(例如Member: user123 Role: Full Control
)。
Sample output:
示例输出:
"Common Hover Panel Actions", "https://#######.####.###.##", "https://######.######.#####.####", "System.Object[]", " Ministry for Finance", "Master Page Gallery", "12/22/2015 8:34:39 AM", "_catalogs/masterpage/Display Templates/Search/Item_CommonHoverPanel_Actions.js", "12/22/2015 8:34:39 AM", "6.2763671875"
The below is the script:
下面是脚本:
Add-PSSnapin Microsoft.SharePoint.PowerShell
function Get-DocInventory([string]$siteUrl) {
$site = New-Object Microsoft.SharePoint.SPSite $siteUrl
$i = 0;
foreach ($web in $site.AllWebs) {
$count = $site.AllWebs.Count
foreach ($list in $web.Lists) {
if ($list.BaseType -ne “DocumentLibrary”)
{
continue
}
foreach ($item in $list.Items) {
$data = @{
"Web Application" = $web.ToString()
"Site" = $site.Url
"Web" = $web.Url
"list" = $list.Title
"Item URL" = $item.Url
"Item Title" = $item["Title"]
"Item Created" = $item["Created"]
"Item Modified" = $item["Modified"]
"File Size" = $item.File.Length/1KB
"Permissions" = foreach($roleAssignment in $item.RoleAssignments)
{
$Permission = ""
foreach($roleDefinition in $roleAssignment.RoleDefinitionBindings)
{
$Permission = $(foreach($roleAssignment in $item.RoleAssignments){$roleAssignment}
{
"Member: " + $roleAssignment.Member.LoginName + " Role: " + $roleDefinition.Name + " "
} -join ',')
$Permission
Write-Progress -activity "Working" -status "Checked Sub-Site $i of $count" -percentComplete (($i / $count)/100)
}
}
}
New-Object PSObject -Property $data
}
}
$web.Dispose();
}
$i++
$site.Dispose()
}
<# Get-DocInventory "https://#####.####.###.##/" | Out-GridView #>
Get-DocInventory "https://#####.####.###.##/" | Export-Csv -NoTypeInformation -Path C:\Users\livob002-lap\Desktop\Permissions-FinanceIntranet.csv
Is it possible to export the System.Object[] for the username as string in the CSV? The names appear as they should in the GridView.
是否可以将用户名的 System.Object[] 导出为 CSV 中的字符串?名称在 GridView 中显示为它们应有的样子。
回答by Andreas
The foreach statement returns an array, so the line
foreach 语句返回一个数组,因此该行
"Permissions" = foreach($roleAssignment in $item.RoleAssignments)
assigns a System.Array to the key "Permissions". The ToString() method for a System.Array returns System.Object[].
将 System.Array 分配给键“权限”。System.Array 的 ToString() 方法返回 System.Object[]。
You can "convert" a string array to a string by using the -join operator. The following could work.
您可以使用 -join 运算符将字符串数组“转换”为字符串。以下可以工作。
"Permissions" = foreach($roleAssignment in $item.RoleAssignments)
{
...
} -join ', '
回答by Ranadip Dutta
There are a few ways to avoid it.
有几种方法可以避免它。
Using
–Join
[pscustomobject]@{ Value = (@(1,3,5,6) -join ',') } | Export-Csv -notype output.csv
Out-String and Trim()
[pscustomobject]@{ Value = (@(1,3,5,6) | Out-String).Trim() } | Export-Csv -notype output.csv
使用
–Join
[pscustomobject]@{ Value = (@(1,3,5,6) -join ',') } | Export-Csv -notype output.csv
Out-String 和 Trim()
[pscustomobject]@{ Value = (@(1,3,5,6) | Out-String).Trim() } | Export-Csv -notype output.csv
Reference: Avoiding System.Object[] (or Similar Output) when using Export-Csv