0

I am working on a script that will check a folder and return the last file date modified time stamp. It will then compare the Current System Time and find the difference between the two and if time is greater than 20 minutes if will send out an email notification.

When debugging/running it I get the following error:

New-TimeSpan : A positional parameter cannot be found that accepts argument '$null'. At C:\Users\jalden\Desktop\CalderaMonitoring-Part1.ps1:7 char:14 + $dtdiff = New-TimeSpan ($_.LastWriteTime) $(Get-Date) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewTimeSpanCommand

Here is my Script:

$src="c:\test\"
$sendmail=$false

Get-Item -path $src | Foreach {$_.LastWriteTime}
Foreach-Object 
{ 
    #write-host $_.fullname
    $dtdiff = New-TimeSpan ($_.LastWriteTime) $(Get-Date)

    if ($dtdiff.minutes -gt 20)
    {
        $strbody=$strbody +$_.fullname+ " - Created Time: "  +$_.LastWriteTime +"`r`n"
        $sendmail=$true
    }       
}

#$strbody

if($sendmail -eq $true)
{
    # Email components
    $strFromAddress = "[email protected]"
    $strToAddress = "[email protected]"
    $strMessageSubject = "Files not uploaded in the last 20 minutes"
    $strMessageBody = $strbody
    $strSendingServer = "smtp.gmail.com"
    $SMTPPort = "587"
    $emailSmtpUser = "[email protected]"
    $emailSmtpPass = "testasfasdfa"

    # Email objects
    $objSMTPMessage = New-Object System.Net.Mail.MailMessage         $strFromAddress, $strToAddress, $strMessageSubject, $strMessageBody
    $objSMTPClient = New-Object System.Net.Mail.SMTPClient( $strSendingServer, $SMTPPort )
    $objSMTPClient.EnableSsl = $true
    $objSMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
    $objSMTPClient.Send($objSMTPMessage)
}

Any suggestions?

2
  • 2
    While this is not your error issue, don't forget that .Minutes. could be 0, where .TotalMinutes would show you something that was days old. Commented Dec 21, 2016 at 21:09
  • I also don't think you need the Foreach {$_.LastWriteTime} at all. In my basic testing on my box - that wasn't necessary in any way to continue in the stream... not sure what you were trying to do there. Commented Dec 21, 2016 at 21:16

1 Answer 1

5

One the second line below, you don't provide any input to ForEach-Object, so $_ is $null

Get-Item -path $src | Foreach {$_.LastWriteTime}
  Foreach-Object { 
  #write-host $_.fullname
  $dtdiff = New-TimeSpan ($_.LastWriteTime) $(Get-Date)

  if ($dtdiff.minutes -gt 20){
    $strbody=$strbody +$_.fullname+ " - Created Time: "  +$_.LastWriteTime +"`r`n"
    $sendmail=$true
    }       
}

Change it to:

Get-Item -path $src | ForEach-Object { 
  #write-host $_.fullname
  $dtdiff = New-TimeSpan ($_.LastWriteTime) $(Get-Date)

  if ($dtdiff.TotalMinutes -gt 20){
    $strbody=$strbody +$_.fullname+ " - Created Time: "  +$_.LastWriteTime +"`r`n"
    $sendmail=$true
    }       
}

As @gravity notes, you should use $dtdiff.TotalMinutes rather than $dtdiff.Minutes if the if statement

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

1 Comment

One last question. If I wanted to return the results of only the latest file I know I would need to use Get-ChildItem but what syntax would I use to return the latest file?

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.