【问题标题】:PowerShell and SharePoint - comparing data from an arrayPowerShell 和 SharePoint - 比较数组中的数据
【发布时间】:2014-02-07 07:58:17
【问题描述】:

我尝试将 SharePoint 列表中的数据(日期)与数组(多个日期)中的数据进行比较。如果数组中有一个项目(那么不是真正的数组),它可以工作,但不止一个它会失败。 SharePoint 列表包含一个日期(格式为 M/dd/yyyy),如果我的脚本在该日期后三天内运行,它将执行更多操作,否则会失败。最终,我需要在 60 天后使用它,所以为了保持脚本的可管理性,我想让阵列正常工作。轻松将 60 天保存回数组将是我的下一个挑战。

我希望能够在脚本中使用相对于当天的日期数组。一个固定的日期是行不通的。我正在尝试查看来自 SharePoint 的日期是否在数组中。

$item is an item from the SharePoint list, and the stuff in brackets ["..."] is the column header.

这就是我获取日期的方式:

$today = (get-date).ToString("M/dd/yyyy")
$yesterday = (get-date).AddDays(-1).ToString("M/dd/yyyy")
$twodaysago = (get-date).AddDays(-2).ToString("M/dd/yyyy")

这是我的数组: enter code here$scheduledate = $today,$yesterday,$twodaysago

这是逻辑:

 if(($item["Computer"] -eq "$computer") -and ($item["Status"] -eq "No") -and ($item["Schedule Date"] -contains $scheduledate))

其他尝试:

If $scheduledate is set to this it works:  

$scheduledate = $今天

如果 $scheduledate 设置为此它不起作用:$scheduledate = $today,$yesterday,$twodaysago

我什至明确创建了数组,但它不起作用:$scheduledate = @($today,$yesterday,$twodaysago)

引号,空格,都试过了。

我试过这些:

-and ($item["Schedule Date"] -eq $scheduledate)) - works if dates match

-and ($item["Schedule Date"] -contains $scheduledate)) - works with single item in array

-and ($item["Schedule Date"] -match $scheduledate)) - ?

-and ($item["Schedule Date"] -like $scheduledate)) - ?

这可行,但在 60 天内并不理想:if(($item["Computer"] -eq "$computer") -and

($item["Status"] -eq "No") -and `
                ($item["Schedule Date"] -contains $today) -or ($item["Schedule Date"] -contains $yesterday) -or ($item["Schedule Date"] -contains $twodaysago))

我不确定如何确定 SharePoint 中的数据格式,或者它是否重要。

请帮忙。

【问题讨论】:

    标签: arrays date sharepoint powershell


    【解决方案1】:

    使用-contains 运算符时,您需要翻转变量。您的参考值或数组需要在左侧,您的测试值需要在右侧。请查看TechNet documentation on about_Comparison_Operators

    此外,正如@websch01ar 指出的那样,格式需要匹配以及类型(字符串到字符串或日期到日期)。您可以采用任何一种方式,但只要格式和类型相同,这样的事情就应该可以工作:

    [String]$today = (get-date).ToString("M/dd/yyyy")
    ...
    [String[]]$scheduleDate = $today, $yesterday, $twodaysago
    (... ($scheduleDate -contains $item["Schedule Date"].ToString("M/dd/yyyy")))
    

    这假设$item.Fields["Schedule Date"].GetType().FullName 等于Microsoft.SharePoint.SPFieldDateTime

    【讨论】:

    • 我改变了这个,它没有工作。相反,我使用 -notcontains 来验证它正在读取数组,它有效。我找到了一种输出一些原始 SharePoint 数据的方法,格式为: ows_Schedule_x0020_Date='2014-01-20 00:00:00' 我正在尝试更新脚本,但到目前为止没有任何效果。 $today = (get-date).ToString("yyyy-MM-dd") + " 00:00:00"
    • 我仍然缺少一些东西。我无法让它与添加的 [String] 内容一起使用。我将另一个 SharePoint 值设置为我正在寻找的格式(字符串不是日期格式)“2014-01-20 00:00:0”,并且 -contains 有效。我不明白为什么只是 "$item["Schedule Date"].ToString()" 不起作用。
    • @user3208164 我已经对此进行了测试,只要满足字段类型假设,上述编辑应该可以工作。
    • 做到了!谢谢!
    • 你能解释一下[String]和[String[]]的区别吗?
    【解决方案2】:

    SharePoint 以特定格式存储数据。以下是我处理格式的几个函数:

    function ToSPDateTime
    {
        param($date)
        if ($date -ne $null)
        {
            return [System.Convert]::ToDateTime($date).ToString("yyyy-MM-ddTHH:mm:ssZ")
        }
        else 
        {
            [string]::Empty
        }
    }
    
    function ToSPDate
    {
        param([datetime]$date)
        if ($date -ne $null)
        {
            return [System.Convert]::ToDateTime($date).ToString("yyyy-MM-dd")
        }
        else 
        {
            [string]::Empty
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2023-01-29
      • 1970-01-01
      • 2010-11-19
      相关资源
      最近更新 更多