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!