1

These forums are great, and I'm hoping to get some help with a headache I have on doing a process manually that should be easier to automate than I've found it to be.

I have a command line tool from a vendors app that will upload logs for the app. The tool only takes Unix UTC time though, and I need to run the scheduled task 3 to 4 times a day to upload the app logs. I could probably just upload all the logs for the day, but that's still a problem as I can't figure out how to do that in a script. So I could just upload all the logs for the entire day and not worry about only uploading the latest logs.

The command is in the powershell script I started which is below, but if it's easier to do this with a batch file i could, I was just trying to avoid adding something like blat to send the email and also I couldn't figure out how to get the task result in a batch script which is what I need to send in the email.

The "IEX" below is the full command, and that's where I need to populate the UTC time for the day if that's easier than trying to get the time from the last time the scheduled task ran, it's the safest option anyway. Then the "1 1" at the end are parameters the vendor requires which add options for the upload.

How can I populate the current day and time for the log upload? If it's the time such as 12:01am (eastern) to 11:59pm (eastern) that is populated each day that should be fine?

# "iex" is an alias for the invoke-expression cmd
iex c:\path_to_exe\myprog.exe -a 1379300570 1379300570 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) 
{
    $smtpServer = "smtp.mydomain.com"
    $fromAddress = "[email protected]"
    $toAddress = "[email protected]"
    $subject = "FAILURE"
    $msgBody = "Trouble with Task"

    # Use these variables if auth for the SMTP server is required!
    $senderCreds = new-object System.Net.networkCredential
    $senderCreds.UserName = "senderusername"
    $senderCreds.Password = "senderpwd"

    $smtpClient = new-object Net.Mail.SmtpClient($smtpServer)
    $smtpClient.Credentials = $senderCreds
    $smtpClient.Send($fromAddress,$toAddress,$subject,$msgBody)
}

1 Answer 1

4

To get the current date/time as a Unix time stamp, calculate the number of seconds passed since the start of the unix epoch:

$Now   = [DateTime]::UtcNow
$Epoch = Get-Date -Year 1970 -Date 01/01

$unixTime = [int](New-TimeSpan $epoch $now).TotalSeconds

To find it for two specific instances in time (00:01 - 23:59 on the previous day in the example):

$StartTime  = (Get-Date "00:01:00").AddDays(-1)
$EndTime  = (Get-Date "23:59:00").AddDays(-1)
$Epoch = Get-Date -Year 1970 -Date 01/01

$unixStart = [int](New-TimeSpan $epoch $StartTime).TotalSeconds
$unixEnd = [int](New-TimeSpan $epoch $EndTime).TotalSeconds

iex("c:\path_to_exe\myprog.exe -a $unixStart $unixEnd 1 1")
1
  • 1
    See also: -UFormat options -- e.g. [int] [double]::Parse((Get-Date -UFormat %s))
    – jscott
    Oct 5, 2013 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .