1
votes

I have data (a date) in a SharePoint list that I am trying to compare to data in an array (multiple dates). If the array has one item in it (not really an array then), it works, but more than one it fails. The SharePoint list contains a date (format of M/dd/yyyy), if my script is run within three days from that date it will do some more things, otherwise fail. Eventually I need to have it at sixty days back, so to keep the script manageable I'd like to get the array working. Easily saving sixty days back to an array will be my next challenge.

My hope was that I would be able to have an array of dates, relative to the current day, to use in the script. A set hard date will not work. I am trying to see if the date from SharePoint is in the array.

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

This is how I am getting the dates:

$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")

This is my array: enter code here$scheduledate = $today,$yesterday,$twodaysago

This is the logic:

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

Other attempts:

If $scheduledate is set to this it works:  

$scheduledate = $today

If $scheduledate is set to this it does not work: $scheduledate = $today,$yesterday,$twodaysago

I have even created the array explicitly, it does not work: $scheduledate = @($today,$yesterday,$twodaysago)

Quotes, spaces, have been tried.

I have tried these:

-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)) - ?

This works, but is not ideal for sixty days: 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))

I'm not sure how to figure out what the format the data in SharePoint is, or if it even matters.

Please help.

2

2 Answers

1
votes

You need to flip your variables around when using the -contains operator. Your reference values or array needs to be on the left side and your test value needs to be on the right. Please see the TechNet documentation on about_Comparison_Operators.

Additionally, as @websch01ar points out, the formats need to match as well as types (string to string or date to date). You can go either way, but something like this should work as long as the formats and types are the same:

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

This assumes that $item.Fields["Schedule Date"].GetType().FullName equals Microsoft.SharePoint.SPFieldDateTime.

0
votes

SharePoint stores data in a specific format. Here are a couple functions I have for working with the format:

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
    }
}