GUI and IF THEN ?

Tagged: 

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Windows LiveUser66 Windows LiveUser66 1 year, 4 months ago.

  • Author
    Posts
  • #31786
    Profile photo of Windows LiveUser66
    Windows LiveUser66
    Participant

    Hello ladies and gentlemen . Faced with a problem. I write a script that shows the parameters of the user's password in AD ,, use the GUI for beauty. But there is a problem. If the user's password never expires – still it shows the time when the user must change the password . The text of the script is as follows:

    Import-Module ActiveDirectory
    Add-Type -assembly System.Windows.Forms
    #
    #trap {
    #continue
    #}

    function Fall{[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
    #
    $use = [Microsoft.VisualBasic.Interaction]::InputBox('Info About PWD: ', ' ', "Login")
    #
    $maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge | Select Days
    #
    $s = Get-ADUser -Filter {samaccountname -like $use} -properties PasswordExpired, PasswordLastSet, PasswordNeverExpires |`
    sort-object name, samaccountname | Select-Object name, samaccountname, PasswordExpired, PasswordLastSet, PasswordNeverExpires
    #

    $Result = New-Object PSObject -Property @{
    'err'=$s.samaccountname
    'User:' = $s.name
    'Password changed:' = $s.PasswordLastSet
    'PasswordNeverExpires:' = $s.PasswordNeverExpires
    'Maximum password age:' = $maxPasswordAgeTimeSpan.Days
    'PasswordExpires:'= get-date (Get-Date $s.PasswordLastSet).adddays(+45) -f ("dd.MM.yyy HH:mm:ss")
    }
    $Result1 = $Result | Select 'User:'
    $Result2 = $Result | Select 'Password changed:'
    $Result3 = $Result | Select 'PasswordExpires:'
    $Result4 = $Result | Select 'Maximum password age'
    $Result5 = $Result | Select 'PasswordNeverExpires:'
    #
    $main_form = New-Object System.Windows.Forms.Form
    $main_form.BackColor = [System.Drawing.Color]::FromArgb(235,255,255,235)
    $main_form.Text = "PWD info"
    $main_form.Width = 50
    $main_form.Height = 300
    $main_form.AutoSize = $true
    #
    $Okbutton = New-Object System.Windows.Forms.Button
    $Okbutton.Location = New-Object System.Drawing.Size(20,20)
    $Okbutton.Size = New-Object System.Drawing.Size(90,70)
    $Okbutton.Text = "Enter Other login"
    $Okbutton.Add_Click({Fall;$main_form.Close()})
    $main_form.Controls.Add($Okbutton)
    $Okbutton2 = New-Object System.Windows.Forms.Button
    $Okbutton2.Location = New-Object System.Drawing.Size(190,20)
    $Okbutton2.Size = New-Object System.Drawing.Size(90,70)
    $Okbutton2.Text = "Close Form"
    $Okbutton2.Add_Click({$main_form.Close()})
    $main_form.Controls.Add($Okbutton2)
    #
    $ListBox = New-Object System.Windows.Forms.ListBox
    $ListBox.Location = New-Object System.Drawing.Point(0,100)
    $ListBox.Width = 300
    $ListBox.Height = 120
    $ListBox.Items.Add($Result1);
    $ListBox.Items.Add($Result2);
    $ListBox.Items.Add($Result3);
    $ListBox.Items.Add($Result4);
    $ListBox.Items.Add($Result5);
    #
    $main_form.Controls.add($ListBox)
    $main_form.ShowDialog()}fall
    —————————————————–
    So, it turns out that even if the parameter PasswordNeverExpires = true
    it is still setting PasswordExpires gives the password change date .
    I tried various combinations , including this:

    if ($result5 -eq $False) {
    $ListBox.Items.Add($Result1);
    $ListBox.Items.Add($Result2);
    $ListBox.Items.Add($Result3);
    $ListBox.Items.Add($Result4);
    $ListBox.Items.Add($Result5);

    } else {
    $ListBox.Items.Add($Result1);
    $ListBox.Items.Add($Result2);
    $ListBox.Items.Add($Result4);
    $ListBox.Items.Add($Result5);
    } —- whiteout RESULT3 param...

    The parameter Result3 I do not need , if the password never expires . But it is still displayed .
    Please help!

  • #31788
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    You are retrieving the property object rather than just the value. You should use expandproperty with your select object to get the value.

    $Result5 = $Result | Select -ExpandProperty 'PasswordNeverExpires:'
    
  • #31789
    Profile photo of Windows LiveUser66
    Windows LiveUser66
    Participant

    OK! Great! It work! Real thanks!
    And also some question :
    if I want to restart the program after the entered name and she gave me some information about it? That is not to re-run the script and that he continued to work and I could enter another user samaccountname?

  • #31790
    Profile photo of Windows LiveUser66
    Windows LiveUser66
    Participant

    Ok...found it...I used – While
    Thanks for help about -ExpandProperty
    Real cool!

  • #31792
    Profile photo of Windows LiveUser66
    Windows LiveUser66
    Participant

    excuse me. There is still a question
    Could somebody tell how to press the button and close the main form, and open it again to enter a new user. With me it turns out that this form of hanging, and opens a new, old is not closed.
    Do something like this but this is doesnt work:
    $Okbutton2.Add_Click({$main_form.Close();$Okbutton.Add_Click({fall})}) fall – is the function.
    necessary to cause the parameter $ use and the program is reset again after pressing. If you do no GUI is simple:
    Import-Module ActiveDirectory
    Clear-Host
    function fall {
    Trap { Write-Host "User not exsist"
    Continue}
    $maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge | Select Days, Minutes
    $use = Read-Host "enter username"
    $s = Get-ADUser -Filter {samaccountname -like $use} -properties PasswordExpired, PasswordLastSet, PasswordNeverExpires |`
    sort-object name, samaccountname | Select-Object name, samaccountname, PasswordExpired, PasswordLastSet, PasswordNeverExpires
    $Result = New-Object PSObject -Property @{
    'user:' = $s.Name
    'Pwdlastsert:' = $s.PasswordLastSet
    'PasswordNeverExpires:' = $s.PasswordNeverExpires
    maxPasswordAgeTimeSpan' = $maxPasswordAgeTimeSpan.Days
    PasswordExpires'= get-date (Get-Date $s.PasswordLastSet).adddays(+$maxPasswordAgeTimeSpan.Days) -f ("dd.MM.yyy HH:mm:ss")}
    $Result1 = $Result | Select -ExpandProperty "user::'
    $Result2 = $Result | Select -ExpandProperty Pwdlastsert:'
    $Result3 = $Result | Select -ExpandProperty 'PasswordNeverExpires:'
    $Result4 = $Result | Select -ExpandProperty maxPasswordAgeTimeSpan"
    $Result5 = $Result | Select -ExpandProperty 'PasswordNeverExpires:':'
    if (-not $Result1) {
    }
    if ($Result5 -eq $False){
    Write-Host "username:" $Result1
    Write-Host "" $Result2
    Write-Host "" $Result3
    Write-Host "" $Result4
    Write-Host ":" $Result5
    }
    elseif($Result5 -eq $true) {
    Write-Host "user": $Result1
    Write-Host "" $Result2
    Write-Host ": NeverExpires"
    }} while ($use -ne 'off') {
    fall
    }

You must be logged in to reply to this topic.