我有一个脚本,可以从列表中获取项,为每个项编写值,我想做的只是将其管道到一个表中。我试过Format-Table、format-table-force、Select-Object和Format-Table,但都不起作用,结果只是显示在列表中。
#CAML Query to Filter List Items
$Query = "<View><Query><Where><Eq><FieldRef Name='MyChoice' /><Value Type='Choice'>Text Two</Value>
</Eq></Where></Query></View>"
#Get All List Items matching given query
$ListItems = Get-PnPListItem -List $ListName -Query $Query
#Loop through each Item
Write-host -f Green "Number of List Items Found:"$ListItems.Count
$output =
ForEach($ListItem in $ListItems)
{
Write-Output "Id :" $ListItem["ID"]
Write-Output "FileLeafRef :" $ListItem["FileLeafRef"]
Write-Output "MySecurity :" $ListItem["MySecurity"]
}
$Output | Format-Table -Property ID, FileLeafRef, MySecurity | Out-File
"C:\wherever\File4.csv"
当我运行该脚本(2个列表项的示例)时,它现在是如何显示的:
ID:1 FileLeafref:MyFile.docx MySecurity:内部ID:2 FileLeafref:MySecondFile.png MySecurity:Private
我真的不能在这里添加一个表,即使是用HTML...但本质上我只希望每个列表项都是一行,并且我希望列:ID、FileLeafRef、MySecurity横跨顶部。
首先,对于4列及以下的结果,PowerShell默认为表格/表视图。你只要求3英镑。因此,根本不需要格式表。此外,Out-File不会生成Csv格式。这是纯文本。
这个...
#Loop through each Item
Write-host -f Green "Number of List Items Found:"$ListItems.Count
$output =
ForEach($ListItem in $ListItems)
{
Write-Output "Id :" $ListItem["ID"]
Write-Output "FileLeafRef :" $ListItem["FileLeafRef"]
Write-Output "MySecurity :" $ListItem["MySecurity"]
}
$Output | Format-Table -Property ID, FileLeafRef, MySecurity | Out-File
"C:\wherever\File4.csv"
...是获得最终结果的错误方法。您永远不会使用Format-Table以这种方式创建文件。格式-表格用于屏幕输出。
所以,试试这个...
#Loop through each Item
Write-host -f Green "Number of List Items Found:"$ListItems.Count
ForEach($ListItem in $ListItems)
{
[PSCustomObject]@{
Id = "$ListItem['ID']"
FileLeafRef = "$ListItem[FileLeafRef]"
MySecurity = "$ListItem['MySecurity']"
} |
Export-Csv -Path 'C:\wherever\File4.csv' -Append -NoTypeInformation
}
结果示例:
#Loop through each Item
Write-host -f Green "Number of List Items Found:"$(Get-Process).Count
ForEach($ListItem in (Get-Process))
{
[PSCustomObject]@{
Id = $ListItem.Id
FileLeafRef = $ListItem.Handles
MySecurity = $ListItem.ProcessName
} |
Export-Csv -Path 'D:\temp\File4.csv' -Append -NoTypeInformation
}
Import-Csv -Path 'D:\temp\File4.csv'
# Results
<#
Id FileLeafRef MySecurity
-- ----------- ----------
7236 204 aesm_service
2144 476 ApplicationFrameHost
21000 150 AppVShNotify
...
#>
但是,如果您想要一个格式化的/表格式的文本文件作为报表,那么这个...
#Loop through each Item
Write-host -f Green "Number of List Items Found:"$(Get-Process).Count
$ListReport = ForEach($ListItem in (Get-Process))
{
[PSCustomObject]@{
Id = $ListItem.Id
FileLeafRef = $ListItem.Handles
MySecurity = $ListItem.ProcessName
}
}
Out-File -FilePath 'D:\temp\File4.txt' -InputObject $ListReport
Get-Content -Path 'D:\temp\File4.txt'
# Results
<#
Id FileLeafRef MySecurity
-- ----------- ----------
7236 204 aesm_service
2144 476 ApplicationFrameHost
21000 150 AppVShNotify
...
#>