1

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 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I changed this around and it didn't work. Instead I used -notcontains to verify it is reading the array, which worked. I found a way to output some of the raw SharePoint data, the format is in: ows_Schedule_x0020_Date='2014-01-20 00:00:00' I am trying to update the script, but nothing is working so far. $today = (get-date).ToString("yyyy-MM-dd") + " 00:00:00"
I am still missing something. I cannot get this to work with the [String] stuff added. I set another SharePoint value to the format I am looking for (string not date format) "2014-01-20 00:00:0", and the -contains works. I don't understand why just "$item["Schedule Date"].ToString()" doesn't work.
@user3208164 I've tested this now and the above edits should work as long as the field type assumption is met.
Can you explain the difference between [String] and [String[]] ?
Quick answer, [String] uses type conversion to ensure that the variable is a string. [String[]] does the same for an array. blogs.msdn.com/b/powershell/archive/2013/06/11/… for more reading.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.