【问题标题】:PowerShell multiple IF statements and ForEach loopsPowerShell 多个 IF 语句和 ForEach 循环
【发布时间】:2021-12-13 20:34:54
【问题描述】:

我很难理解为什么下面的脚本在到达第二个 ForEach/IF 语句时会失败。

Multiple 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


【解决方案1】:

来自marszecomment

您应该改用常规双引号:["InquestTwoDate"]

【讨论】:

    猜你喜欢
    • 2014-02-11
    • 2015-09-15
    • 2021-11-09
    • 2021-08-27
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多