none
Unable to get a value in an expression RRS feed

  • Question

  • Hello,

        I am using a script  to output all the system accounts soon to expire into a file. The code is generating the file successfully but the value of the expression DaysUntilExpired which is derived using the formula $expiredDate - $_.PasswordLastSet is not getting computed, What am I doing wrong here ?



    IA


    Thursday, July 30, 2020 2:44 PM

Answers

  • This is not a duplicate post, this question is to find out why I am getting null when I do a subtraction from PAswdLastset and Expirepasswd date ? 

    IA

    Please post code correctly and maybe your questions will be better understood.

    You cannot use an unset value in a date expression.  You cannot use a null in a date expression.  You cannot use a non-date in a date expression.


    \_(ツ)_/

    • Marked as answer by Irfan Ahmed11 Thursday, July 30, 2020 4:23 PM
    Thursday, July 30, 2020 4:22 PM

All replies

  • This is not a duplicate post, this question is to find out why I am getting null when I do a subtraction from PAswdLastset and Expirepasswd date ? 

    IA

    Thursday, July 30, 2020 4:08 PM
  • This is not a duplicate post, this question is to find out why I am getting null when I do a subtraction from PAswdLastset and Expirepasswd date ? 

    IA

    Please post code correctly and maybe your questions will be better understood.

    You cannot use an unset value in a date expression.  You cannot use a null in a date expression.  You cannot use a non-date in a date expression.


    \_(ツ)_/

    • Marked as answer by Irfan Ahmed11 Thursday, July 30, 2020 4:23 PM
    Thursday, July 30, 2020 4:22 PM
  • When you run $PWdLastSet does it have any null values?

    Ramu V Ramanan

    Please try to understand that pwdLastSet cannot be used as a date,  Take the time to look up the attribute to find out what it is.

    It can never be null but it can have no value due to the nature of the objects definition.

    When working with Windows technology or with AD or any Windows subsystems you must refer to the documentation to learn what things are an how they are used.  This who have no basic computer training will not understand this but can learn by using the documentation.

    The best advice I have is to stop guessing and use the reference materials. To be literate means to know how to read and to read.  We invented teh printing press to make knowledge available to the masses.  Only farm animals are prevent3ed from gaining knowledge the way humans do.  Reading is your keys to the kingdom.


    \_(ツ)_/

    Friday, July 31, 2020 3:17 AM
  • Irfan asked a question and instead of being helpful, all you have done is rant on this forum .

    Stop being a d1ckh3ad dude.


    Ramu V Ramanan

    I gave the answer and it was marked as correct.  The OP just didn't post his fixed code.  Your answer does not explain why the issue existed in the first place.  Your suggestion would work if that is what the OP wants to do.

    We can't know that at this point.  Try to be a little less insulting in the future.


    \_(ツ)_/

    Friday, July 31, 2020 4:05 AM
  • Hi Ramu,

       I really appreciate your taking your time and helping me with your answer, to be honest I also was not happy with the reply  jrv had given me earlier and in another thread in the same forum,  I really appreciate you helping a newbie like me.

     To answer your question yes I can see values when I run $PWdLastSet  , 

    The issue I am having is it not working when I use it in an expression. I am not sure why. If what  jrv writes is true then what is the best way to retrive PAsswordlast set and use it in my expressions, All I am trying to do is add 365 days to the PAsswordlast set date and get the new date. Then I would like to know the difference in days from this new date to current date.

    Hope you can help me with this Ramu, I once again appreciate your positive feedback whole heartedly.

    Regards

    IQ


    IA

    Friday, July 31, 2020 4:13 AM
  • Again - this is about understanding what you are asking.  The pwdLastSet value is not a date.  The PasswordLastSet is a value inserted by teh PowerShell Cmdlet.  To add days use the date method "AddDays" and you will have the results required.

    All of your issues are the result of not learning basic PowerShell.  My intent is to get you to remedy that.  You will need all of this to move forward independently.

    You also need to post code according to teh forum rules which I posted a lijnk to in my first reply7.  Your code is unreadable in most browsers and cannot be correctly copied so fixing it for you is an issue.  In a tech forum you need to follow the procedures of teh forum.

    Here is a good place to get the specifics of how to do this: How to ask questions in a technical forum

    You can learn how to use dates here: https://devblogs.microsoft.com/scripting/adding-and-subtracting-dates-with-powershell/


    \_(ツ)_/

    Friday, July 31, 2020 4:27 AM
  • You can argue this all you want but the original code is just wrong.  It is full of undefined variables and syntax errors.  This would be obvious if you would format and post the code correctly.

    Please look carefully at the code.  Why are variables used and not defined or assigned.  

    To get all values of password dates not set for some number of days:

    $searchBase = 'OU=Service Accounts,OU=SG1,OU=wt,DC=wt,DC=ad,DC=cit,DC=cc'
    Get-ADUser -Filter {PasswordNeverExpires -eq $false -and Enabled -eq $true} -Properties PasswordLastSet, Mail -SearchBase $searchBase |                                          
        Where-Object{([datetime]::Now - $_.PasswordLastSet).Days  -in  @(1,15,11,25,30)} |
        Select-Object Mail, @{n='DaysUntilExpired';e={([datetime]::Now - $_.PasswordLastSet).Day}} |
        Add-Content -Path C:\Temp\PasswordExpList.txt

    Note that this is very easy if you use PowerShell as intended instead of copying and pasting with little understanding of what is happening.


    \_(ツ)_/

    Friday, July 31, 2020 5:24 AM
  • Hi Ramu, I am mgetting the following error message, When I tried your suggestion.

    At C:\Users\Desktop\ps\final_test3.ps1:83 char:1
    + }
    + ~
    Unexpected token '}' in expression or statement.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : UnexpectedToken


    IA

    Saturday, August 1, 2020 4:11 AM
  • Thanks Ramu, there was a extra } I removed this but now I am getting a completely new error as shown below

    Get-ADDefaultDomainPasswordPolicy : Unable to find a default server with Active Directory Web Services
    running.
    At C:\Users\Desktop\ps\final.ps1:31 char:17
    + $MaxPwdAge   = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Day ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceUnavailable: (WT:ADDefaultDomainPasswordPolicy) [Get-ADDefaultDom
       ainPasswordPolicy], ADServerDownException
        + FullyQualifiedErrorId : ActiveDirectoryServer:1355,Microsoft.ActiveDirectory.Management.Commands.
       GetADDefaultDomainPasswordPolicy
     
    Get-ADUser : Unable to find a default server with Active Directory Web Services running.
    At C:\Users\Desktop\ps\final.ps1:38 char:17
    + ... iredUsers = Get-ADUser -Filter {(PasswordLastSet -lt $emailDate) -and ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceUnavailable: (:) [Get-ADUser], ADServerDownException
        + FullyQualifiedErrorId : ActiveDirectoryServer:1355,Microsoft.ActiveDirectory.Management.Commands.
       GetADUser
     


    IA

    Saturday, August 1, 2020 8:51 PM
  • The error cannot come from the code you posted.  This is why I asked you to format and post your code correctly.  Without accurate information this will go in circles forever.

    Please do what this forum requires which is to post the code correctly.


    \_(ツ)_/

    Saturday, August 1, 2020 10:41 PM
  • Where are you running the script at?

    did you miss running the Import-Module command?

    The error messages (probably on line 31 & 38)

    "Get-ADDefaultDomainPasswordPolicy : Unable to find a default server with Active Directory Web Services "

    "Get-ADUser : Unable to find a default server with Active Directory Web Services running."

    means you are running the scripts from a machine which can't access AD at the time you are running it.


    Ramu V Ramanan

    Please try to understand that Import-Module can have no relation to this issue.  Please take dome time to actually learn PowerShell before trying to advise at an expert level.  Before acting as an expert you must learn how to 4read and interpret error messages and the underlying technology.

    Look closely at the posted script and try to find the command indicated in the error.  Can you find it anywhere?  I don't think so.


    \_(ツ)_/

    Sunday, August 2, 2020 2:26 AM
  • Hi Ramu,

      You hit the nail on its head, you are right, I was not connected to the network.

    I can now run the script successfully without this error but the issue still remains, I am not seeing any values for the expression " DaysUntilExpired"

    What could be the reason this is showing as null ?

    PS: I have reported user jrv as "Inappropriate and irrelevant", so hopefully we should not see any messages from him now.

    Thanks

    IQ


    IA



    Sunday, August 2, 2020 2:59 AM
  • Can you please read the following and fix your posts so they can be read correctly.  DO NOT post colorized code.

    How to post code in Technet Forums

    If you continue to refuse to follow forum guidelines your account may be suspended.

    We cannot help with vague questions and badly posted and formatted code.  

    The answers 5to your issues about how to manage dates and date arithmetic have been answered. I have posted a link that will explain where you are going wrong.  


    \_(ツ)_/

    Sunday, August 2, 2020 4:01 AM
  • Hi there, I did not realise that the code was getting posted in color, I have removed that and any other references to code, just using short snippets instead, hope this helps. I am also looking at the blog you had posted on using Date variables.

    Thanks

    IQ


    IA

    Sunday, August 2, 2020 4:29 AM
  • Hi there, I did not realise that the code was getting posted in color, I have removed that and any other references to code, just using short snippets instead, hope this helps. I am also looking at the blog you had posted on using Date variables.

    Thanks

    IQ


    IA

    Why ca]'t you read the link explaining how to post code in this forum.  I have posted it three times and you still haven't read it.

    Why is it so hard for you to follow the forums requirements for posting and asking questions?


    \_(ツ)_/

    Sunday, August 2, 2020 4:33 AM
  • Done, read the guidelines indeed useful

    IA

    Sunday, August 2, 2020 4:42 AM
  • Hello Ramu Venkitaramanan, please refrain from abusing the "Report as abuse" option in this forum. Technical forums are not the place for personal feuds. Thanks --Bill (moderator)

    -- Bill Stewart [Bill_Stewart]

    Monday, August 3, 2020 11:50 PM
    Moderator
  • Hello Moderator,

       I am the one who posted the original question and fully support Ramu, he has been very helpful throughout this thread.

       On the other hand user jrv is the one who has shown disrespect and continuously keeps posting irrelevant and off topics comments, trying to distract the original topic, I suggest you please advise user jrv to show some discipline and if possible ask him to avoid posting any message for this particular thread as he has been distracting. User Ramu has been super helpful throughout.

    Thanks

    IQ


    IA


    Tuesday, August 4, 2020 1:55 AM
  • We cannot understand what you mean by disrespect.  ALl posts were correct and the answers were tested.  YOu refused to follow forum rules for posting code and also were unable to ask a clear question.  Links were posted to advise you as to how to ask a clear technical question.

    If you cannot understand the technical responses you could have asked for clarification.  You didn't.

    Ramu's answers do not match your partial question.  If you had read and tried to understand the links then most of this would not have happened.

    Personal insults for whatever reason are not allowed in technical forums.  Insults and claims of abuse will not get you correct ansers.

    I am sure if you take time to review the thread (as I have tice) you will see where the issue started.


    \_(ツ)_/

    Tuesday, August 4, 2020 2:01 AM
  • Ok so following all your suggestions on posting guidelines and after reading the blog on using Dates

    I have used the method shown in the comments section of the blog at https://devblogs.microsoft.com/scripting/adding-and-subtracting-dates-with-powershell/#comment-317

    My code snippet is as shown below, ( this has been formatted using Visual studio code editor

    $PWdLastSet1 = $PWdLastSet.Where( { $_.PasswordLastSet -ne $null })
    $expiredDate = $PWdLastSet1.PasswordLastSet.addDays(365)  
    $PWdLastSet1 = $PWdLastSet.Where( { $_.PasswordLastSet -ne $null })
    $expiredDatetmp = $PWdLastSet1.PasswordLastSet.addDays(365)  
    $expiredDate = $expiredDatetmp.tostring(“yyyy-MM-dd”)

    However when I do this , I am running into an error as shown below

    Cannot find an overload for "tostring" and the argument count: "1".
    At line:1 char:1
    + $expiredDate = $expiredDatetmp.ToString("yyyy-MM-dd")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest


    IA

    Tuesday, August 4, 2020 3:48 AM
  • Please explain what it is you are trying to do. It is impossible to guess from the code.

    This code will get the days since the password last set:

    $searchBase = 'OU=Service Accounts,OU=SG1,OU=wt,DC=wt,DC=ad,DC=cit,DC=cc'
    Get-ADUser -Filter {PasswordNeverExpires -eq $false -and Enabled -eq $true} -Properties PasswordLastSet, Mail -SearchBase $searchBase |                                          
        Where-Object{([datetime]::Now - $_.PasswordLastSet).Days  -in  @(1,15,11,25,30)} |
        Select-Object Mail, @{n='DaysUntilExpired';e={([datetime]::Now - $_.PasswordLastSet).Day}} |
        Add-Content -Path C:\Temp\PasswordExpList.txt

    Please read how to post code as your code, as posted, is unreadable. I posted that link three times.  You still haven't read it.  My code is posted according to forum standards.

    This code is the nearest I can guess from your code. It 4returns passwords last set in the last 1,15,25 or 30 days.

    Please think about what you are trying to do and explain in English what you are attempting without referring to code.


    \_(ツ)_/

    Tuesday, August 4, 2020 4:12 AM
  • Thanks again jrv, I am using the passwords last set date but the final objective is to use that and compute the expired password dates as our AD does not store expire dates or is not defined as a policy. We compute the password date by adding 365 days to password last set date. Then as the expiry dates are approaching  1,7 15,30 days the system should output all these system accounts into a txt file.

       In simple words ,  I am trying to work on a script which queries the active directory for all system accounts, finds their last password reset dates , adds 365 days to this and computes the expiry dates. Once this is done , the final step should be to put all the users accounts whose expiry dates are within 1,7 15,30 days into a text file.

     Hope this helps in narrowing down to the solution I am looking for. Thanks again. 


    IA

    Tuesday, August 4, 2020 4:33 AM
  • Just use my code and subtract 365 from "Now" then check that the days left are minus the offset values.

    That is two simple changes and the script will do what you want.

    You can also just change the match dates to be 365 minus each day.

    @(365-1,365-15,365-11,365-25,365-30)


    \_(ツ)_/


    • Edited by jrv Tuesday, August 4, 2020 4:38 AM
    Tuesday, August 4, 2020 4:37 AM
  • Thanks jrv, I will try this and let u know , can I just use Get-date instead of "Now" . Also there are two places in your code where "Now" is being used , do I need to subtract 365 from both places, can we use a variable to store this subtraction instead of doing it directly ? Please note I am signing out for today and will try your further suggestions tomorrow and let u know how it goes

    Thanks again.

    IQ


    IA

    Tuesday, August 4, 2020 5:04 AM
  • Do not use Get-Dat and do not change the code until you understand how it works.  You cannot just wildly guess how to do this.


    \_(ツ)_/

    Tuesday, August 4, 2020 5:09 AM
  • I have done as you suggested but for some reason upon subtracting 365 from "Now" it is always evaluating to null.  Not sure what is going wrong here.

    $searchBase = 'OU=Service Accounts,OU=SG1,OU=WAT,DC=wt,DC=ad,DC=cit,DC=cc'
    Get-ADUser -Filter * -Properties PasswordLastSet, Mail -SearchBase $searchBase |                                          
    Where-Object { ([datetime]::Now.AddDays(-365) - $_.PasswordLastSet).Days -in @(364, 350, 354, 340, 335) } |
    Select-Object Mail, @{n = 'DaysUntilExpired'; e = { ([datetime]::Now.AddDays(-365) - $_.PasswordLastSet).Day } } |
    Add-Content -Path C:\Temp\PasswordExpList.txt 


    IA

    Wednesday, August 5, 2020 5:42 AM
  • Paste this at a command line.  It can never be null.  You must have changed something.

    [datetime]::Now.AddDays(-365)


    \_(ツ)_/

    Wednesday, August 5, 2020 6:31 AM
  • Yes you are right this is not null  

    [datetime]::Now.AddDays(-365).

    I think the problem is with the other variable $_.PasswordLastSet

    This for some reason is null I guess at least for some entries, this could be the cause, I may be wrong though.  I have not changed anything I have pasted the code as well in my previous post.


    IA

    Wednesday, August 5, 2020 6:41 AM
  • If the passwo5rd has never been set then that wil be null.  This tells me that your users may never have set their passwords.  You will have to filter those out.


    \_(ツ)_/

    Wednesday, August 5, 2020 6:48 AM
  • Thanks I did a quick test and it returns null even for accounts which have a non null value for PasswordLastSet.

     So not sure what could be wrong with this.

    @{name=ABCD; PasswordLastSet=12/18/2011 12:18:52 AM; DaysUntilExpired=}
    @{name=abcTasks; PasswordLastSet=02/04/2020 11:24:05 AM; DaysUntilExpired=}
     


    IA

    Wednesday, August 5, 2020 7:11 AM
  • What returns null?  How do you know it returns null?  Whqat is that code you just posted.  It doesn't make much sense and is not what I asked you to use.

    This will tell you if any accounts match your criteris/

    Get-ADUser -Filter * -Properties PasswordLastSet, Mail -SearchBase $searchBase |                                          
        Where-Object{ 
            ([datetime]::Now.AddDays(-365) - $_.PasswordLastSet).Days -in @(364, 350, 354, 340, 335)
        }
    


    \_(ツ)_/

    Wednesday, August 5, 2020 7:16 AM
  • Thanks a lot jrv for all your help. I have tried your code again, unfortunately the subtraction of the two dates is continuing to give a null value

    [datetime]::Now.AddDays(-365) - $_.PasswordLastSet).Days

    is getting evaluated as null.


    IA

    Wednesday, August 5, 2020 9:48 AM
  • What you are saying is impossible if the information you are providing is accurate.  Did you run the code I asked you to run?  You haven't answered my last question.


    \_(ツ)_/

    Wednesday, August 5, 2020 10:14 AM
  • Yes upon running your code I am encountering this issue.  I am running the code you posted earlier.
    Get-ADUser -Filter * -Properties PasswordLastSet, Mail -SearchBase $searchBase |                                          
        Where-Object{ 
            ([datetime]::Now.AddDays(-365) - $_.PasswordLastSet).Days -in @(364, 350, 354, 340, 335)
        }


    IA


    Wednesday, August 5, 2020 10:17 AM
  • What makes you think you are getting nulls?  The code either returns users or returns noting.  Nulls have nothing to do with this.


    \_(ツ)_/

    Wednesday, August 5, 2020 10:20 AM
  • I am not telling that nulls are being returned, there is data being returned but the column for the subtraction between the two dates is showing up as null, this is very strange, it should show a value in days.

    IA

    Wednesday, August 5, 2020 10:44 AM
  • The code you just posted does not return a column that subtracts dates.  It just returns users.

    I recommend that you take the time to learn PowerShell before continuing or we will go around in this circle forever.


    \_(ツ)_/

    Wednesday, August 5, 2020 11:09 AM
  • Yes, I am learning PowerShell in parallel in addition to posting questions here.

    The code I am using which displays the column that subtracts dates is given below

    Get-ADUser -Filter * -Properties PasswordLastSet, Mail -SearchBase $searchBase |                                          
    Where-Object { 
        ([datetime]::Now.AddDays(-365) - $_.PasswordLastSet).Days -in @(" ", 364, 350, 354, 340, 335, ' ', '', "")
    } |
    Select-Object name, PasswordLastSet, @{n = 'DaysUntilExpired'; e = { (([datetime]::Now.AddDays(365) - [datetime]::$_.PasswordLastSet).Day).days } } |
    Add-Content -Path C:\Temp\PasswordExpList.txt 



    IA

    Wednesday, August 5, 2020 11:20 AM
  • Your code is not what I posted and won't work.  You need to learn PowerShell before posting complex questions that you clearly do not undertand.  We cannot help you if you keep changing the code and giving us incorrect and misleading information.

    When you have actually learned PowerShell then post back with a clear question.  This forum cannot give you personal instruction in basic PowerShell.  It is also nearly impossible to learn PowerShell by guessing and copying.  You must put in the effort to learn the basics on your own.

    Here is a good place to start: 

    Read the book carefully and do all examples until you have a clear understanding of what PowerShell is and how it is intended to be used.  The book was written by admins for admins and addresses the misconceptions that most admins bring to coding in Windows.  It also will teach you how to use PowerShell with AD and other common subsystems.  All of this must be learned and cannot be guessed.  Realize that most of what you find on te Internet is either wrong or misleading.  This book will help you to understand when something you find is not useful.


    \_(ツ)_/

    Wednesday, August 5, 2020 10:31 PM