powershell - 从特定视图而不是所有项目中检索 list.items
问题描述
我正在尝试导出 sharepoint 2013 powershell 脚本数组中的项目,以便仅从列表中的某个自定义视图中提取。我创建了一个显示“当前月份”的视图 - 仅在当前日历月中创建的项目。所有以前的项目都保留在列表中,但我们只希望查看和使用本月输入的项目。
我已尝试遵循以下内容及其许多变体: https ://sharepoint.stackexchange.com/questions/134830/powershell-script-to-get-items-only-in-a-particular-view-of-sharepoint-列表库
$list = $web.Lists["testlist"]
$view = $list.Views["Current Month"]
$items = $list.GetItems($view)
#Array to Hold Result - PSObjects
$ListItemCollection = @()
$items.Items | foreach {
下面的代码可以很好地获取列表中的所有项目并且可以正常发送电子邮件。
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$web = Get-SPWeb -Identity "https://sharepointsite.com"
$list = $web.Lists["testlist"]
#Array to Hold Result - PSObjects
$ListItemCollection = @()
$list.Items | foreach {
$ExportItem = New-Object PSObject
$ExportItem | Add-Member -MemberType NoteProperty -Name "Name" -Value ($_["Name"] -replace '^\d+;#')
$ExportItem | Add-Member -MemberType NoteProperty -Name "Reason" -Value $_["Reason"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "Example" -Value $_["Example"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "Created" -Value $_["Created"]
$ListItemCollection += $ExportItem
}
$web.Dispose()
$emailbody = $(cat C:\temp\emailbody.txt) + $ListItemCollection
#Email formatting
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"
$SMTPServer = "mail.com"
$EmailFrom = "test@mail.com"
$EmailTo = "joel@mail.com"
$EmailSubject = "Test Email"
$Message = New-Object System.Net.Mail.MailMessage $EmailFrom, $EmailTo
$Message.Subject = $EmailSubject
$Message.IsBodyHTML = $true
$message.Body = $emailbody + ($ListItemCollection | ConvertTo-Html -Head $style | Out-String)
$SMTP = New-Object Net.Mail.SmtpClient($SMTPServer)
$SMTP.Send($Message)
当我使用上面采样的代码时,它会为 3 个添加成员行中的每一行给出错误:无法索引到空数组。
解决方案
你有没有机会给我们一个 $list.Items 的例子?
使用 foreach ( $list.items 中的 $item )怎么样?
foreach ( $item in $list.Items ) {
$ExportItem = New-Object PSObject
$ExportItem | Add-Member -MemberType NoteProperty -Name "Name" -Value $( $item.name -replace '^\d+;#' )
$ExportItem | Add-Member -MemberType NoteProperty -Name "Reason" -Value $item.reason
$ExportItem | Add-Member -MemberType NoteProperty -Name "Example" -Value $item.example
$ExportItem | Add-Member -MemberType NoteProperty -Name "Created" -Value $item.created
$ListItemCollection += $ExportItem
}
foreach () {} 和 ... | foreach {} 行为不同,我知道这是有逻辑的原因,无论如何我总是从 foreach () {} 中得到我想要的
推荐阅读
- function - 访问方法时出现以太坊奇怪错误
- python - tensorflow GradientDescentOptimizer:操作输入和计算的输入梯度之间的形状不兼容
- java - Java 7 接口变量?
- oracle - 具有自定义类型的 Oracle 参考游标
- c++ - 循环中的 C++ 窗口图像导致延迟
- python - Tkinter Python 3 create_rectangle 参数 - relheight,relwidth,relx,rely
- sql - .Net webservice API,同时多次调用,在SQL中获取重复插入而不是更新
- c# - 防止屏幕捕获软件捕获应用程序屏幕
- mysql - 订购和分组mysql
- java - 无法自动选择文件通过使用 Selenium Webdriver