none
Assistance with calculations?? with comparing dates RRS feed

  • Question

  • I have a script (that I thought was working) that runs on local computers daily and checks the local admin password  last set time stamp and if it is older than 30 days, it will write the computer name and password age to a file. I thought everything was working until an entry showed up that was only 29 days old... So I decided to test the query and it doesn't seem to be working as expected, here's the test I ran:

    PS C:\> $AdmPwd = $(net user Administrator | find /i 'Password last set') -replace 'Password last set' -replace '(^\s+|\s+$)',''
    PS C:\> $AdmPwd
    2/1/2018 8:58:08 AM
    PS C:\> get-date -format g
    5/17/2018 2:43 PM
    PS C:\> (get-date).AddDays(-30).ToString("g")
    4/17/2018 2:43 PM
    PS C:\> If ($($AdmPwd) –gt (get-date).AddDays(-30).ToString("g")) { "More than 30" } else { "Less than 30" }
    Less than 30
    PS C:\>

    I might not be looking at the logic correctly but shouldn't the output say "More than 30"??? 

    If I change it to -lt

    PS C:\> If ($($AdmPwd) –lt (get-date).AddDays(-30).ToString("g")) { "More than 30" } else { "Less than 30" }
    More than 30

    Here's the script that is scheduled to run and thought the output was correct

    #Variables
    $FileName = ("\\USPRI-100631\Share$\Temp\Test\" + (Get-Date).tostring("MMddyy") + “AdmPwd.txt”)
    $AdmPwd = $(net user Administrator | find /i 'Password last set') -replace 'Password last set' -replace '(^\s+|\s+$)',''

    #Values
    $Value = @($env:COMPUTERNAME + "," + $AdmPwd + "," + $(Get-Date -format 'g'))

    #Script
    If ($(net user Administrator | find /i "Password last set") –gt (get-date).AddDays(-30).ToString("g"))
    {
    $Value | Out-file $FileName -append -NoClobber
    Write-Host "Job completed"
    }


    Jason Ostrowski

    Thursday, May 17, 2018 7:10 PM

Answers

  • Ahhh... I think I get what you are saying and thanks so much for you guidance! Let me know if I am following you correctly... I've been scripting for about 30 days now so I am trying to get my head around all of this...

    So, this  $admpwd.GetType().Name is getting the string from my variable $AdmPwd = $(net user Administrator | find /i 'Password last set') -replace 'Password last set' -replace '(^\s+|\s+$)',''

    $lastset = [datetime]$AdmPwd is taking the output of  $AdmPwd = $(net user Administrator | find /i 'Password last set') -replace 'Password last set' -replace '(^\s+|\s+$)','' which is 2/1/2018 8:58:08 AM 

    $lastSet.GetType().Name converts 2/1/2018 8:58:08 AM from sting to date

    $lastset -lt [datetime]::ToDay.AddDays(-30) is the command (string) that determines if the Administrator password is older that today's date - 30 days....

    Am I following it correctly?


    Jason Ostrowski

    Monday, May 21, 2018 2:30 PM

All replies

  • So, the script runs correctly on Windows 7 machines but not on Windows 10... On windows 10 it seems to be opposite. In the script I don't use the $admPwd variable in the if command, I exclude the -replace 'Password last set' -replace '(^\s+|\s+$)','' because it would screw up the command; just use it for formatting the out-file to get rid of unwanted spaces and such

    Still don't get why in windows 10 it acts differently 


    Jason Ostrowski

    Thursday, May 17, 2018 7:24 PM
  • You cannot convert all dates to strings to compare them.  Just compare the dates.  You cannot compare strings as dates are too specific so the strings will never compare.  A string is not a date.


    \_(ツ)_/


    • Edited by jrv Thursday, May 17, 2018 7:44 PM
    • Proposed as answer by BOfH-666 Thursday, May 17, 2018 9:42 PM
    Thursday, May 17, 2018 7:43 PM
  • Not sure what I am doing wrong but here's an example of how the script get 2 different outputs using the same command... Both windows 7 pc's but there are 2 differences: Example 1 has a much older version of pwershell (not sure if that means anything) and the local admin password timestamp is almost 3 years old while Example 2 has Powershell version 5 and the local admin password timestamp is about 78 days old... Any ideas? 

    Example1

    Powershell version 2:

    PS C:\> $AdmPwd = $(net user Administrator | find /i 'Password last set') -replace 'Password last set' -replace '(^\s+|\s+$)',''
    PS C:\> $30days = (get-date).AddDays(-30).ToString("G")
    PS C:\> $AdmPwd
    5/25/2016 4:32:06 PM
    PS C:\> $30days
    4/21/2018 9:43:07 AM
    PS C:\> If ($30days -gt $AdmPwd) { "More than 30" } else { "Less than 30" }
    Less than 30
    PS C:\> If ($AdmPwd -gt $30day) { "More than 30" } else { "Less than 30" }
    More than 30

    Example 2

    Powershell version 5:
    PS C:\> $AdmPwd = $(net user Administrator | find /i 'Password last set') -replace 'Password last set' -replace '(^\s+|\s+$)',''
    PS C:\> $30days = (get-date).AddDays(-30).ToString("G")
    PS C:\> $AdmPwd
    2/1/2018 8:58:08 AM
    PS C:\> $30days
    4/21/2018 9:52:04 AM
    PS C:\> If ($30days –gt $AdmPwd) { "More than 30" } else { "Less than 30" }
    More than 30
    PS C:\> If ($AdmPwd –gt $30days) { "More than 30" } else { "Less than 30" }
    Less than 30
    PS C:\>


    Jason Ostrowski


    Monday, May 21, 2018 2:05 PM
  • You cannot compare dates that are strings.  THey must be date objects.

    When you do this: " $30days = (get-date).AddDays(-30).ToString("G")" the date is converted to a string and cannot be compared.

    "AdminPwd" is a string and must be converted into a date.

    $admpwd.GetType().Name
    $lastset = [datetime]$AdmPwd
    $lastSet.GetType().Name
    $lastset -lt [datetime]::ToDay.AddDays(-30)


    \_(ツ)_/



    • Edited by jrv Monday, May 21, 2018 2:16 PM
    Monday, May 21, 2018 2:11 PM
  • Ahhh... I think I get what you are saying and thanks so much for you guidance! Let me know if I am following you correctly... I've been scripting for about 30 days now so I am trying to get my head around all of this...

    So, this  $admpwd.GetType().Name is getting the string from my variable $AdmPwd = $(net user Administrator | find /i 'Password last set') -replace 'Password last set' -replace '(^\s+|\s+$)',''

    $lastset = [datetime]$AdmPwd is taking the output of  $AdmPwd = $(net user Administrator | find /i 'Password last set') -replace 'Password last set' -replace '(^\s+|\s+$)','' which is 2/1/2018 8:58:08 AM 

    $lastSet.GetType().Name converts 2/1/2018 8:58:08 AM from sting to date

    $lastset -lt [datetime]::ToDay.AddDays(-30) is the command (string) that determines if the Administrator password is older that today's date - 30 days....

    Am I following it correctly?


    Jason Ostrowski

    Monday, May 21, 2018 2:30 PM
  • PS C:\> If ($lastset -lt [datetime]::ToDay.AddDays(-30)  –eq 'True') { "More than 30 days old" } else { "Less than 30 day
    s old" }
    More than 30 days old
    PS C:\>


    Jason Ostrowski


    Monday, May 21, 2018 2:34 PM
  • Mostly no.

    I recommend that you first learn PowerShell before pursuing this any further.  You cannot guess about how to write a program.

    The following was added to show you what the variable types are at each step.  Because you have no knowledge of PowerSHell or programming you are not able to understand this. It is what you learn in chapter 1 of any PowerShell training.

    .$admpwd.GetType().Name


    \_(ツ)_/

    Monday, May 21, 2018 2:35 PM
  • I understand now... and I think my script would have worked if I just converted the AdmPwd string to a date and then just removed ToString("G") from the date variable; then everything would have been OK... I tested it with just the string to date conversion and removed the string conversion from the date variable and it seems to be the fix... Thanks! I am reading up and trying to learn as much as I can in a short period of time while at the same time creating a script for this project that I was assigned so... You guys are a tremendous resource and I get a little more understanding each time I interact with you guys so I really do appreciate it!!

    Jason Ostrowski

    Monday, May 21, 2018 3:10 PM
  • You will spend the most part of a year almost learning what this tutorial can teach you in a few hours:

    Learn PowerShell  


    \_(ツ)_/

    Monday, May 21, 2018 3:25 PM