locked
Trying to get password expiration data but get last logon. What have I done wrong? RRS feed

  • Question

  • Ok, I have created a GUI/form using PrimalForms, everything works fine, enter the user ID or name to get a list of details like account expiration, email address, date created etc. I also have a button to display last log on, again, this works fine. However, When I add code to provide password expiration details it gives me the last logon instead. Using almost the same code in a menu based script, as below, the information provided is correct

    Import-Module ActiveDirectory
    $prompt = @"
    ********************************************************************************
    **                  Please select from the options below                      **
    **                                                                            **
    **                  p = Find Password expiration details                      **
    **                  c = Clear screen                                          **
    **                  x = exit                                                  **
    **                                                                            **
    ********************************************************************************
    
    "@
    Clear-host
    Do{
    	$originalcolor = $host.UI.RawUI.ForegroundColor
    	$host.UI.RawUI.ForegroundColor = "yellow"
    	$choice = Read-Host -Prompt $prompt
        $maxPasswordAge = 90
    	$host.UI.RawUI.ForegroundColor = $originalcolor
        Clear-host
    	Switch($choice){
            p {$user = Read-Host 'Enter the user ID to query' ; Get-ADuser -Filter "sAMAccountName -eq '$user'" -Properties PasswordLastSet |
        Select sAMAccountName, name, PasswordLastSet,
              @{N='PasswordExpirationDate';E={(Get-Date $_.PasswordLastSet).AddDays($maxPasswordAge)}} ,          
               @{N='PasswordLifeRemaining';E={$maxPasswordAge - ((Get-Date) - $_.PasswordLastSet).Days}} | format-list}
            c {Clear-host}
            x {break}
    
    		default {write-host "Invalid selection, please select one of the options listed and try again." -ForegroundColor green}
    	}
    }Until($choice -eq "x")

    Running the above against an account called CRMTEST I get the result below

    However, when this is added, with required modification to work in a PrimalForms GUI, as below

    $ExpirationButton_OnClick= {
         
    Get-Aduser -Filter "sAMAccountName -eq '$($EntryBox.text)'"  -Properties PasswordLastSet |
        Select SamAccountName, name, 
               PasswordLastSet,
               @{N='PasswordExpirationDate';E={(Get-Date $_.PasswordLastSet).AddDays(90)} ,
               @{N='PasswordLifeRemaining';E={90 - ((Get-Date) - $_.PasswordLastSet).Days}}} | out-string
               $results.Focus()
        }

    This is just the code for the password expiration query, the entire script is over 600 lines long so I will not post it here.

    For info, $ExpirationButton is the button which, when clicked, runs the code.

    $EntryBox is the text box the user enters the user ID into

    $results is the text box the results are displayed in. Below is what this returns

    So, apart from the variables listed above it is basically the same code as used in the menu based version, but instead of giving the same results as the menu based version it gives last logon, it also, despite being set to display sAMAccountName, name, PasswordLastSet, Password expiration and the number of days until it expires all I get is the date and time of the last logon, which is not mentioned in this code at all, it is in the script but assigned to another button.

    I don't get what I have done wrong here, can anyone point me in the right direction?

    To be clear, This code runs and displays the results where I want them, but it gives the wrong answer (I have run into issues where I am told this will not display the results, it does display them, as shown in the screenshot)

    I know the code is ugly and needs to be cleaned up, but until I can get it working correctly I do not want to get involved in a cleanup. I jus do not understand why it does 2 totally different things despite being essentially the same code


    • Edited by GADavies Tuesday, March 10, 2015 3:54 PM replace image
    • Moved by Bill_Stewart Thursday, April 9, 2015 8:12 PM This is not "fix script for me" forum
    Tuesday, March 10, 2015 3:50 PM

Answers

  • The issue seems to have been the GUI. I created a new one, used the same code for the password query and it worked.

    For whatever reason including the code in the original GUI did not work. I don't understand what is different between the 2, especially given the code I could not get working is good in all other forms and when used standalone.

    • Marked as answer by GADavies Monday, April 20, 2015 3:43 PM
    Monday, April 20, 2015 3:43 PM

All replies

  • I don't remember now how many times I have made this point. "$results.Focus()" does nothing useful.  You must assign the output to a control.  By just arbitrarily copying and pasting code you cannot get a correct or reliable outcome.

    We assing values to controls like this:

    $results.Text ='my value'

    The results you are seeing are caused by not understanding how to use output in forms.  What shows in the console is from Write-Host or Write-Verbose statements elsewhere in your code.

    This will get you the exact same report in the box but your box is only one line so it won't look very good.

    $ExpirationButton_OnClick= {
         $props=@(
              'SamAccountName',
              'name', 
               'PasswordLastSet',
               @{N='PasswordExpirationDate';E={(Get-Date $_.PasswordLastSet).AddDays(90)}},
               @{N='PasswordLifeRemaining';E={90 - ((Get-Date) - $_.PasswordLastSet).Days}}
        )
    
        $results.Text = Get-Aduser -Filter "sAMAccountName -eq '$($EntryBox.text)'"  -Properties PasswordLastSet |
                    Select  $props | Format-Table -Auto | Out-String
    }


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, March 10, 2015 5:40 PM
    Tuesday, March 10, 2015 5:40 PM
  • I dug up an old example and pasted you code into it with one change.  You can copy and paste it into a PS CLI and it will run.  Look at how it displays the results and look at how it is done in  code.

    Add-Type -AssemblyName System.Windows.Forms
    Import-Module ActiveDirectory
    
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $form1 = New-Object 'System.Windows.Forms.Form'
    $tbPwdLastSet = New-Object 'System.Windows.Forms.TextBox'
    $tbPwdExpires = New-Object 'System.Windows.Forms.TextBox'
    $tbPwdAge = New-Object 'System.Windows.Forms.TextBox'
    $tbDisplayName = New-Object 'System.Windows.Forms.TextBox'
    $results = New-Object 'System.Windows.Forms.TextBox'
    $buttonGetPwdInfo2 = New-Object 'System.Windows.Forms.Button'
    $buttonGetPwdInfo1 = New-Object 'System.Windows.Forms.Button'
    $tbSamName = New-Object 'System.Windows.Forms.TextBox'
    $buttonOK = New-Object 'System.Windows.Forms.Button'
    $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    
    $buttonGetPwdInfo1_Click={
         $props=@(
              'SamAccountName',
              'name', 
               'PasswordLastSet',
               @{N='PasswordExpirationDate';E={$_.PasswordLastSet.AddDays(90)}},
               @{N='PasswordLifeRemaining';E={90 - ([datetime]::Today - $_.PasswordLastSet).Days}}
        )
    
        $results.Text=Get-Aduser -Filter "sAMAccountName -eq '$($tbSamName.Text)'"  -Properties PasswordLastSet |
                    Select  $props | Format-List| Out-String
    }
    
    $buttonGetPwdInfo2_Click={
    	Try{
    	    $user=Get-Aduser -Filter "sAMAccountName -eq 'jvierra'"  -Properties PasswordLastSet,DisplayName
    	    $tbDisplayName.Text=$user.DisplayName
    		$tbPwdLastSet.Text=$user.PasswordLastSet
    		$tbPwdExpires.Text=($user.PasswordLastSet).AddDays(90)
    		$tbPwdAge.Text=90 - ([datetime]::Today - $user.PasswordLastSet).Days
        }
    	Catch{
    		Write-Host "$_" -fore red 
        }
    	
    }
    
    $buttonOK_Click={
    	$this.FindForm().Close()	
    }
    
    $form1.SuspendLayout()
    $form1.Controls.Add($tbPwdLastSet)
    $form1.Controls.Add($tbPwdExpires)
    $form1.Controls.Add($tbPwdAge)
    $form1.Controls.Add($tbDisplayName)
    $form1.Controls.Add($results)
    $form1.Controls.Add($buttonGetPwdInfo2)
    $form1.Controls.Add($buttonGetPwdInfo1)
    $form1.Controls.Add($tbSamName)
    $form1.Controls.Add($buttonOK)
    $form1.ClientSize = '569, 286'
    $form1.FormBorderStyle = 'FixedDialog'
    $form1.MaximizeBox = $False
    $form1.MinimizeBox = $False
    $form1.Name = "form1"
    $form1.StartPosition = 'CenterScreen'
    $form1.Text = "Form"
    
    #
    $tbPwdLastSet.Location = '411, 77'
    $tbPwdLastSet.Name = "tbPwdLastSet"
    $tbPwdLastSet.Size = '146, 20'
    $tbPwdLastSet.TabIndex = 8
    
    #
    $tbPwdExpires.Location = '411, 103'
    $tbPwdExpires.Name = "tbPwdExpires"
    $tbPwdExpires.Size = '146, 20'
    $tbPwdExpires.TabIndex = 7
    #
    $tbPwdAge.Location = '411, 129'
    $tbPwdAge.Name = "tbPwdAge"
    $tbPwdAge.Size = '146, 20'
    $tbPwdAge.TabIndex = 6
    #
    $tbDisplayName.Location = '411, 51'
    $tbDisplayName.Name = "tbDisplayName"
    $tbDisplayName.Size = '146, 20'
    $tbDisplayName.TabIndex = 5
    #
    $results.Font = "Lucida Console, 9pt, style=Bold"
    $results.Location = '30, 53'
    $results.Multiline = $True
    $results.Name = "results"
    $results.Size = '375, 190'
    $results.TabIndex = 4
    #
    $buttonGetPwdInfo2.Location = '411, 13'
    $buttonGetPwdInfo2.Name = "buttonGetPwdInfo2"
    $buttonGetPwdInfo2.Size = '107, 23'
    $buttonGetPwdInfo2.TabIndex = 3
    $buttonGetPwdInfo2.Text = "Get Pwd Info # 2"
    $buttonGetPwdInfo2.UseVisualStyleBackColor = $True
    $buttonGetPwdInfo2.add_Click($buttonGetPwdInfo2_Click)
    #
    $buttonGetPwdInfo1.Location = '287, 13'
    $buttonGetPwdInfo1.Name = "buttonGetPwdInfo1"
    $buttonGetPwdInfo1.Size = '107, 23'
    $buttonGetPwdInfo1.TabIndex = 2
    $buttonGetPwdInfo1.Text = "Get Pwd Info # 1"
    $buttonGetPwdInfo1.UseVisualStyleBackColor = $True
    $buttonGetPwdInfo1.add_Click($buttonGetPwdInfo1_Click)
    #
    $tbSamName.Location = '30, 13'
    $tbSamName.Name = "tbSamName"
    $tbSamName.Size = '240, 20'
    $tbSamName.TabIndex = 1
    #
    $buttonOK.Anchor = 'Bottom, Right'
    $buttonOK.Location = '482, 251'
    $buttonOK.Name = "buttonOK"
    $buttonOK.Size = '75, 23'
    $buttonOK.TabIndex = 0
    $buttonOK.Text = "&OK"
    $buttonOK.UseVisualStyleBackColor = $True
    $buttonOK.add_Click($buttonOK_Click)
    $form1.ResumeLayout()
    
    $InitialFormWindowState = $form1.WindowState
    $form1.add_Load($Form_StateCorrection_Load)
    $form1.ShowDialog()
    


    ¯\_(ツ)_/¯

    Tuesday, March 10, 2015 6:38 PM
  • I get your point, but you are missing mine. The results appear where I want them, they are just NOT THE CRRECT RESULTS.

    using this

         $props=@(
              'SamAccountName',
              'name', 
               'PasswordLastSet',
               @{N='PasswordExpirationDate';E={$_.PasswordLastSet.AddDays(90)}},
               @{N='PasswordLifeRemaining';E={90 - ([datetime]::Today - $_.PasswordLastSet).Days}}
        )
    
        $results.Text=Get-Aduser -Filter "sAMAccountName -eq '$($tbSamName.Text)'"  -Properties PasswordLastSet |
                    Select  $props | Format-List| Out-String
    }
    

    gives exactly the same result, the display shows the last logon and not the information it should. So instead of lecturing me on how to assign a value, which I understand, as I said I know the code needs cleaning up, I need to work out why this does not display what it should and gives me the last logon date and time and not the password expiration. This has been my question all along.

    Tuesday, March 10, 2015 6:52 PM
  • Ok, I have created a GUI/form using PrimalForms, everything works fine, enter the user ID or name to get a list of details like account expiration, email address, date created etc. I also have a button to display last log on, again, this works fine. However, When I add code to provide password expiration details it gives me the last logon instead. Using almost the same code in a menu based script, as below, the information provided is correct

    Import-Module ActiveDirectory
    $prompt = @"
    ********************************************************************************
    **                  Please select from the options below                      **
    **                                                                            **
    **                  p = Find Password expiration details                      **
    **                  c = Clear screen                                          **
    **                  x = exit                                                  **
    **                                                                            **
    ********************************************************************************
    
    "@
    Clear-host
    Do{
    	$originalcolor = $host.UI.RawUI.ForegroundColor
    	$host.UI.RawUI.ForegroundColor = "yellow"
    	$choice = Read-Host -Prompt $prompt
        $maxPasswordAge = 90
    	$host.UI.RawUI.ForegroundColor = $originalcolor
        Clear-host
    	Switch($choice){
            p {$user = Read-Host 'Enter the user ID to query' ; Get-ADuser -Filter "sAMAccountName -eq '$user'" -Properties PasswordLastSet |
        Select sAMAccountName, name, PasswordLastSet,
              @{N='PasswordExpirationDate';E={(Get-Date $_.PasswordLastSet).AddDays($maxPasswordAge)}} ,          
               @{N='PasswordLifeRemaining';E={$maxPasswordAge - ((Get-Date) - $_.PasswordLastSet).Days}} | format-list}
            c {Clear-host}
            x {break}
    
    		default {write-host "Invalid selection, please select one of the options listed and try again." -ForegroundColor green}
    	}
    }Until($choice -eq "x")

    Running the above against an account called CRMTEST I get the result below

    However, when this is added, with required modification to work in a PrimalForms GUI, as below

    $ExpirationButton_OnClick= {
         
    Get-Aduser -Filter "sAMAccountName -eq '$($EntryBox.text)'"  -Properties PasswordLastSet |
        Select SamAccountName, name, 
               PasswordLastSet,
               @{N='PasswordExpirationDate';E={(Get-Date $_.PasswordLastSet).AddDays(90)} ,
               @{N='PasswordLifeRemaining';E={90 - ((Get-Date) - $_.PasswordLastSet).Days}}} | out-string
               $results.Focus()
        }

    This is just the code for the password expiration query, the entire script is over 600 lines long so I will not post it here.

    For info, $ExpirationButton is the button which, when clicked, runs the code.

    $EntryBox is the text box the user enters the user ID into

    $results is the text box the results are displayed in. Below is what this returns

    So, apart from the variables listed above it is basically the same code as used in the menu based version, but instead of giving the same results as the menu based version it gives last logon, it also, despite being set to display sAMAccountName, name, PasswordLastSet, Password expiration and the number of days until it expires all I get is the date and time of the last logon, which is not mentioned in this code at all, it is in the script but assigned to another button.

    I don't get what I have done wrong here, can anyone point me in the right direction?

    To be clear, This code runs and displays the results where I want them, but it gives the wrong answer (I have run into issues where I am told this will not display the results, it does display them, as shown in the screenshot)

    I know the code is ugly and needs to be cleaned up, but until I can get it working correctly I do not want to get involved in a cleanup. I jus do not understand why it does 2 totally different things despite being essentially the same code


    You seem to be very lost. I will leave you to sort through this., I cannot get you to see why what you are doing is wrong. I have been building forme in Windows since the first version. I have taught many programmers to program Windows. I have never seen anyone who misses the exact same things on the screen.

    I have no clue how to show you what you are doing wrong.  What ever I showyou you just put the same bad lines back in. 

    Good luck.


    ¯\_(ツ)_/¯

    Tuesday, March 10, 2015 7:36 PM
  • The issue seems to have been the GUI. I created a new one, used the same code for the password query and it worked.

    For whatever reason including the code in the original GUI did not work. I don't understand what is different between the 2, especially given the code I could not get working is good in all other forms and when used standalone.

    • Marked as answer by GADavies Monday, April 20, 2015 3:43 PM
    Monday, April 20, 2015 3:43 PM