rhyeal/aws-rotate-iam-keys

(Windows) Bug with scheduled task generation time

Closed this issue · 4 comments

The time randomization portion of the scheduled task creation has the potential to generate invalid times. This is because the minute random number can be generated as a single-digit value. If this is passed directly to the call to create the scheduled task, as it is currently, it generates an "Invalid starttime value" error.

schtasks : ERROR: Invalid starttime value.
At line:1 char:1
+ schtasks /create /f /tn "AWS Rotate IAM Keys" /tr "Powershell.exe -Ex ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (ERROR: Invalid starttime value.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Example:

Get-Random assigners result in the following values:

$hour = 2
$minute = 9

"0$($hour):$minute" outputs 02:9 (resulting in error) whereas the intended value is 02:09

Proposed fix:

Update code section to either

  • Exclude single-digit values from the $minute random (Get-Random -Maximum 60 -Minimum 10)
    OR
  • Add a check for single digit values and update the value for $minute to add a leading zero.
$minute = Get-Random -Maximum 60
if ($minute -lt 10) { $minute = "0$($minute)" }

I'd be happy to fork and submit a pull request though it'd probably just be easier for you to update it since it should be a one-liner. Thanks!

Use the -Format operator.

'{0:d2}' -f [int]$minute
'{0:d2}' -f [int]$hour

@learley @alencar Could one or other of you add a PR for this? I don't use Windows and I don't think @rhyeal does either, so we've no easy way to test small changes like this. Thanks :-)

Easy fix is

(Get-Random -Maximum 60).toString("d2")

I have tested this in windows

image

This is now fixed in master, thanks to PR #64 by @arun6445, but as there is currently an issue with the circleci build process, the fix is not yet included in the built version at https://aws-rotate-iam-keys.com/aws-rotate-iam-keys.ps1

For anyone else running into this issue, downloading the raw aws-rotate-iam-keys.ps1 file from https://raw.githubusercontent.com/rhyeal/aws-rotate-iam-keys/master/Windows/aws-rotate-iam-keys.ps1 should do the trick.