Script to run as sch task, runs cmd line, email result?

This topic contains 4 replies, has 3 voices, and was last updated by  Richard Siddaway 4 years, 1 month ago.

  • Author
    Posts
  • #10162

    Jake Sully
    Participant

    Howdy,
    I'm trying to work out a script that will get the current year/month/date and it has to be in unix utc format, then I want to specify it to always be 12:00am for the start time and 11:59pm for the end time in the command I have to run which is below.

    I have to schedule this powershell script as a scheduled task. Once the task completes I need to email the result on whether it ran or failed to someone. I've got it close I think but I can't get the date correct and then I also tried using & 'command as shown below' as well as iex 'command as shown below', what's the right way?

    #I think the command below puts the date correctly and the time at 12:00am, but how do I statically set 11:59pm so I only do the logs for the 1 day?
    $Time = get-date -format %y%m%d00000

    # "iex" is an alias for the invoke-expression cmd
    iex "c:\Program Files (x86)\app\util>up.exe -b $Time $Time 1 1""

    # $? lets us know if the previous command was successful or not
    # $LASTEXITCODE gives us the exit code of the last Win32 exe execution
    if (!$? -OR $LASTEXITCODE -gt 0)

    send-mailmessage -from "dev01@devlab.local" -to "test.user@devlab.local>" -subject "Log Upload Failure" -body "Scheduled task to upload logs on server Dev01 failed." -priority High -dno onSuccess, onFailure -smtpServer smtp.devlab.local

  • #10167

    Don Jones
    Keymaster

    In terms of statically doing the time, PowerShell can accept a string in a date format, and parse it as a date. So...

    [datetime]$now = "$((Get-Date).ToShortDateString()) 11:59PM"

    Would create a datetime with today's date at 11:59pm today.

    BTW, $? doesn't necessarily let us know if the previous command was successful. It's kind of up to the previous command to set a nonzero exit code if it failed. If your command does so, great. $lastexitcode is probably more reliable for external commands, if you know what exit codes they return. Just be aware of that.

    You don't technically need either & or Invoke-Expression. You can just run the command – e.g., I can run "ping" without wrapping it in anything. I don't have Up.exe to test with, but unless it's doing something illegal in terms of the characters it uses for its arguments, it should accept the command line without being in a string or script block.

    As a note, when you have two discrete issues, consider posting them in separate posts. That way we can focus on each one. A post about "getting right date format" and "running external command" would help make sure the right folks are looking at your post – we tend to look at the subjects to see if it's a post we think we can help with; when you bundle multiple stuff into one post you might be missing out on some great advice!

    Be aware that, currently, your If construct is not correctly formed.

  • #10172

    Richard Siddaway
    Moderator

    if you are after a unix formatted date wouldn't you be better off with -UFormat ?

    I ran you date creation line and got an odd result
    £> get-date -format %y%m%d00000
    13311600000

    I think you want
    £> get-date -format %y%M%d00000
    1391600000

    m is for minutes & M is for month

    as for your second time – does this work for you
    £> get-date -Hour 23 -Minute 59 -Second 59 -Format %y%M%d%h%m%s
    13916115959

  • #10196

    Jake Sully
    Participant

    Thanks, here's what I came up with to get the time in epoch format for a current time which the .exe requires that I'm trying to run and then the earlier time which is 5 hours earlier. The time was a lot of searching, but it's the only way I could get it to come out right, if there's easier methods I'd definitely like to know.

    $Now = [int][double]::Parse($(Get-Date -date (Get-Date).ToUniversalTime()-uformat %s))
    $Before = [int][double]::Parse($(Get-Date -date (Get-Date).AddHours(-9).ToUniversalTime()-uformat %s))

    Then I ran the command as shown below and I'm still confused as to the "correct" way to specify a ".exe" that requires parameters as I've read a bunch of info such as the links below.

    & "c:\Program Files (x86)\ATDIR\util\upload.exe" -b $Now $Before 1 1

    After this I'm stuck on the "IF" statement on reporting if the scheduled task runs correctly, there's no log file I can refer to so that I can ensure the script works, the .exe doesn't allow a parameter.

    And once that is done it's a matter of getting the email sent out with the results.

  • #10199

    Richard Siddaway
    Moderator

    Sending the email is probably the easy part – use the Send-MailMessage cmdlet as you had in your original post

    As for knowing if the exe worked or not

    $lastexitcode may work for you

    £> ping 127.0.0.1

    Pinging 127.0.0.1 with 32 bytes of data:
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

    Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
    £> $LASTEXITCODE
    0
    £> ping 10.10.98.99

    Pinging 10.10.98.99 with 32 bytes of data:
    Request timed out.
    Request timed out.
    Request timed out.
    Request timed out.

    Ping statistics for 10.10.98.99:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
    £> $LASTEXITCODE
    1

    so it should pick up the state of your exe
    £> ping 127.0.0.1 ; if ($LASTEXITCODE -eq 0){"Good"}else{"Bad"}

    Pinging 127.0.0.1 with 32 bytes of data:
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

    Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
    Good
    £> ping 10.10.98.99 ; if ($LASTEXITCODE -eq 0){"Good"}else{"Bad"}

    Pinging 10.10.98.99 with 32 bytes of data:
    Request timed out.
    Request timed out.
    Request timed out.
    Request timed out.

    Ping statistics for 10.10.98.99:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
    Bad

    The only other way to check the correct completion would depend on the exe's actions eg a file is created, deleted, moved etc

You must be logged in to reply to this topic.