Plexus81
Plexus81

Reputation: 1251

Date / Time condition in powershell - SharePoint - LastItemModifiedDate - LastRunTime

I'm making a script that collect two dates from the system. One from when the scheduling task has last run and the date a list has last been modified. Problem is that the datetime format isn't correct .

  1. LastItemModifiedDate from a SPList - output format 04/09/2015 12:48:48
  2. LastRunTime from a scheduling task - output format 09.04.2015 10:50:03

What I want to do is to check if the list has been changed since the last time the scheduling task has run.

$scheduledTask = Get-ScheduledTask -TaskName "SharePoint scheduling" | Get-ScheduledTaskInfo
$scheduledTaskLastRunTime = $scheduledTask.LastRunTime

$listExist = $spSourceWeb.Lists | where{$_.Title -eq $listName}
    if($listExist)
    {
        $spSourceList = $spSourceWeb.Lists[$listName]

        if ($scheduledTaskLastRunTime -le $spSourceList.LastItemModifiedDate)
        {
            Write-Host " Changes found" -ForegroundColor Green
            SetListColumnToCopy($listName)
        }

Do I do the IF correct ? What magic do I have to do to check the datetime? I've read that "don't need a special format to compare dates in Powershell" but is it true in this situation?

Upvotes: 2

Views: 624

Answers (1)

JPBlanc
JPBlanc

Reputation: 72640

You should use ParseExact static method from DateTime class.

$dateToCompare = [datetime]::ParseExact("09.04.2015 10:50:03","MM.dd.yyyy hh:mm:ss", $null)

Upvotes: 2

Related Questions