0

Extreme powershell newbie here. I appreciate any and all help. I'm trying to put together a simple anti-flooding script to work with Sharepoint/Powershell. Need it to look at a datetime in a field and compare it to the current datetime then stop execution if within 5 seconds of the last submittal. The method im using now always seems to evaluate to true.

#get system datetime (output format - 06/12/2014 07:57:25)
$a = (Get-Date)


# Get current List Item
$ListItem = $List.GetItemById($ItemID)
$DateToCompare = $ListItem["baseline"].AddMilliseconds(5000)

if ($DateToCompare -gt $a)
     {Break}


#set variable to field
$ListItem["baseline"] = $a


#write new item
$ListItem.Update()
Break
user3723688
  • 47
  • 1
  • 10

2 Answers2

0

I don't have Sharepoint access so I cannot fully test.

Can you verify the datatype of "baseline" attribute?

($ListItem["baseline"]).getType().Name

Are you sure that 5000 millseconds is really being added?

Write-Output "NOW: $($curDate) BASELINE: $($DateToCompare) DIFF: $( ($curDate - $DateToCompare).TotalMilliseconds )"

Why use break rather than let the evaluation naturally end? Below is an alternative way you might restructure you code.

#The difference in Milliseconds acceptable
$threshold = 5000

#Get current date, the formatting depends on what you have defined for output. 
$curDate = Get-Date

#Get current list item from SP
$listItem = $List.GetItemById($ItemID)

# Get current List Item's baseline
$DateToCompare = $listItem["baseline"]

Write-Output "NOW: $($curDate) BASELINE: $($DateToCompare) DIFF: $( ($curDate - $DateToCompare).TotalMilliseconds )"

if ( ($curDate - $DateToCompare).TotalMilliseconds -le $threshold ){

    #set variable to field
    $ListItem["baseline"] = $curDate

    #write new item
    $ListItem.Update()
} else {
    #Outside of threshold
}
Bin
  • 211
  • 1
  • 7
  • So it turns out the script as I gave it above was functional. The issue was the time I was pulling under the (Get-Date) function was the server time (central), rather than the local time (eastern). I'll post final code after the 8 hour, new user, limit. – user3723688 Jun 12 '14 at 14:44
0

So it turns out the script as I gave it above was functional. The issue was the time I was pulling under the (Get-Date) function was the server time (central), rather than the local time (eastern).

#bring server time up to eastern time
$a = (Get-Date).AddMilliseconds(7200000)


# Get current List Item
$ListItem = $List.GetItemById($ItemID)

#take baseline time and add 5 seconds
$DateToCompare = $ListItem["baseline"].AddMilliseconds(5000)

#stop if script has run in the last 5 sec (loop prevention)
if ($DateToCompare -gt $a)
    {Break}

#stop if the status hasnt changed
if ($ListItem["baselinestatus"] -eq $ListItem["Status"])
    {Break}

#get current activity status
$currentstatus = $ListItem["Status"]

#get current contents of log
$log = $ListItem["Log"]

#append new entry to existing and write it to the log
$newentry = $log + "<br>" + $a + " - " + $currentstatus

#set variable to field
$ListItem["Log"] = $newentry
$ListItem["baseline"] = $a
$ListItem["baselinestatus"] = $currentstatus


#write new item
$ListItem.Update()
user3723688
  • 47
  • 1
  • 10