【发布时间】:2021-12-13 20:34:54
【问题描述】:
我很难理解为什么下面的脚本在到达第二个 ForEach/IF 语句时会失败。
谁能解释一下为什么这不起作用?以及如何在同一个脚本中使用多个独立的 If 语句和一个操作?
#Get the SharePoint list contents
$listItems= (Get-PnPListItem -List $ListName -Fields "Title","InquestTwoDate", "InquestThreeDate" -PageSize 1000)
#Iterate through each item in the List and set date to null
ForEach ($listItem in $listItems)
{
If($listItem[“InquestTwoDate”] -eq "$date")
{
Set-PnPListItem -List $ListName -Identity $listItem -Values @{“InquestTwoDate”=$null}
#Write-Host $listItem[“InquestTwoDate”]
}
}
#Iterate through each item in the List and set date to null
ForEach ($listItem in $listItems)
{
If($listItem[“InquestThreeDate”] -eq "$date")
{
Set-PnPListItem -List $ListName -Identity $listItem -Values @{“InquestThreeDate”=$null}
#Write-Host $listItem[“InquestThreeDate”]
}
}
Error:
At C:\Users\%user%\Documents\PowerShell\Upload SharePoint items from CSV\deletedatefields2.ps1:23 char:14
If($listItem[“InquestTwoDateâ€
~
Array index expression is missing or not valid.
At C:\Users\%user%\Documents\PowerShell\Upload SharePoint items from CSV\deletedatefields2.ps1:23 char:14
If($listItem[“InquestTwoDateâ€
~~~~~~~~~~~~~~~~~~~~~Unexpected token '“InquestTwoDateâ€At C:\Users\%user%\Documents\PowerShell\Upload SharePoint items from CSV\deletedatefields2.ps1:23 char:14
If($listItem[“InquestTwoDateâ€
~~~~~~~~~~~~~~~~~~~~~
Missing closing ')' after expression in 'If' statement.
At C:\Users\%user%\Documents\PowerShell\Upload SharePoint items from CSV\deletedatefields2.ps1:22 char:1
{
~
Missing closing '}' in statement block or type definition.
At C:\Users\%user%\Documents\PowerShell\Upload SharePoint items from CSV\deletedatefields2.ps1:23 char:47
If($listItem[“InquestTwoDateâ€
~Unexpected token ')' in expression or statement.
At C:\Users\%user%\Documents\PowerShell\Upload SharePoint items from CSV\deletedatefields2.ps1:25 char:76
... t-PnPListItem -List $ListName -Identity $listItem -Values @{“Inques ...
~
Missing '=' operator after key in hash literal.At C:\Users\%user%\Documents\PowerShell\Upload SharePoint items from CSV\deletedatefields2.ps1:32 char:14
If($listItem[“InquestThreeDateâ€
~
Array index expression is missing or not valid.At C:\Users\%user%\Documents\PowerShell\Upload SharePoint items from CSV\deletedatefields2.ps1:32 char:14If($listItem[“InquestThreeDateâ€
~~~~~~~~~~~~~~~~~~~~~~~Unexpected token '“InquestThreeDateâ€At C:\Users\%user%\Documents\PowerShell\Upload SharePoint items from CSV\deletedatefields2.ps1:32 char:14If($listItem[“InquestThreeDateâ€
~~~~~~~~~~~~~~~~~~~~~~~
Missing closing ')' after expression in 'If' statement.
At C:\Users\%user%\Documents\PowerShell\Upload SharePoint items from CSV\deletedatefields2.ps1:31 char:1
{
~
Missing closing '}' in statement block or type definition.
Not all parse errors were reported. Correct the reported errors and try again.
CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : MissingArrayIndexExpression
【问题讨论】:
-
请以纯文本格式发布代码。还包括
$listitems。 -
@AbrahamZinala - 已添加,谢谢。
-
如果您说您的脚本“失败”,那么您应该在问题中包含错误消息。
-
@marsze 已添加,谢谢。
-
@alexthannah 您应该使用常规双引号:
["InquestTwoDate"]
标签: powershell sharepoint-online