Solved

Powershell Password Info

Posted on 2016-09-19
11
62 Views
Last Modified: 2016-10-04
I need to add the following information to this script but I'm not sure how.
Date password last set / Password Age
Local Group Memberships
Global Group Memberships

	$GetAccountsInfoScriptblock = {$Obj = @()
			$AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" -Filter "LocalAccount='$True'"
				Foreach($LocalAccount in $AllLocalAccounts)
					{
						$Object = New-Object -TypeName PSObject
						$Object|Add-Member -MemberType NoteProperty -Name "Name" -Value $LocalAccount.Name
						$Object|Add-Member -MemberType NoteProperty -Name "Caption" -Value $LocalAccount.Caption
						$Object|Add-Member -MemberType NoteProperty -Name "Disabled" -Value $LocalAccount.Disabled
						$Object|Add-Member -MemberType NoteProperty -Name "LockOut" -Value $LocalAccount.LockOut
						$Object|Add-Member -MemberType NoteProperty -Name "Password Changeable" -Value $LocalAccount.PasswordChangeable
						$Object|Add-Member -MemberType NoteProperty -Name "Password Expires" -Value $LocalAccount.PasswordExpires
						$Object|Add-Member -MemberType NoteProperty -Name "Password Required" -Value $LocalAccount.PasswordRequired
						$Object|Add-Member -MemberType NoteProperty -Name "Description" -Value $LocalAccount.Description
						$Obj+=$Object
					}
		$Obj
		}

Open in new window

0
Comment
Question by:coitadmin
  • 5
  • 5
11 Comments
 
LVL 18

Expert Comment

by:x-men
ID: 41806254
$pwdLAstSet = [datetime]::fromfiletime(($LocalAccount .Properties.pwdlastset)[0])
0
 

Author Comment

by:coitadmin
ID: 41806816
Unexpected token '.Properties.pwdlastset' in expression or statement.

+             $PwdLastSet = [datetime]::fromfiletime(($LocalAccount .Properties.pwdlastset <<<< )[0])
    + CategoryInfo          : ParserError: (.Properties.pwdlastset:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41807608
I was using a function to get the local admin users list, here is a modified version based on your requirement.. Check and let me know if it works for you..
Function Get-Localadmin {
Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='$True'" | %{
$LocalAccount = $_
$Comp = $env:computername
$Groups = Get-WMIObject win32_group -filter "LocalAccount='True'" | Select PSComputername,Name,@{N="Members";E={$_.GetRelated("win32_useraccount").Name}}
$User = Try {
     Add-Type -AssemblyName System.DirectoryServices.AccountManagement 
     $PrincipalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine,$comp)
     [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($PrincipalContext,$LocalAccount.Name)
    }
    Catch {
        Write-Warning -Message "$($_.Exception.Message)"
    }
 
 New-Object PSObject -Property @{
   Name = $LocalAccount.Name
	Caption = $LocalAccount.Caption
	Disabled = $LocalAccount.Disabled
	LockOut = $LocalAccount.LockOut
	"Password Changeable" = $LocalAccount.PasswordChangeable
	"Password Expires" = $LocalAccount.PasswordExpires
	"Password Required" = $LocalAccount.PasswordRequired
   Description = $LocalAccount.Description
   LastPasswordSet = $User.LastPasswordSet
   passwordage = "$((New-TimeSpan $User.LastPasswordSet (Get-Date)).Days) Days"
   memberOf = ($Groups | ?{$_.Members -contains $LocalAccount.Name} | Select -Expand name) -join ","
   }
 }
}

Get-Localadmin

Open in new window

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:coitadmin
ID: 41808840
This is excellent.  The only part that doesn't seem to be working is the Groups component.  I'm not getting anything back from that part.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41809109
Does the groups have members?
Check the result of command..
Get-WMIObject win32_group -filter "LocalAccount='True'" | Select PSComputername,Name,@{N="Members";E={$_.GetRelated("win32_useraccount").Name}}

Open in new window

0
 

Author Comment

by:coitadmin
ID: 41822633
Yes.  The Administrator group for example does contain the Administrator account but it doesn't show up using that command.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41822636
It's working for me without any issue and showing the groups..
What was the result of command which I posted in my last comment?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 41822663
Also Try this updated code..
Function Get-Localadmin {
Function  Get-LocalGroupMember {
  Param  ($Group)
 $Computer = $env:COMPUTERNAME
 $ADSIComputer = [ADSI]("WinNT://$Computer,computer")
 $group = $ADSIComputer.psbase.children.find($Group,  'Group') 
 $group.Invoke('members')  | ForEach {
 $_.GetType().InvokeMember("Name",  'GetProperty',  $null,  $_, $null)
  }
}
Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='$True'" | %{
$LocalAccount = $_
$Comp = $env:computername
$Groups = Get-WMIObject win32_group -filter "LocalAccount='True'" | Select Name,@{N="Members";E={Get-LocalGroupMember $_.Name}}
$User = Try {
     Add-Type -AssemblyName System.DirectoryServices.AccountManagement 
     $PrincipalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine,$comp)
     [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($PrincipalContext,$LocalAccount.Name)
    }
    Catch {
        Write-Warning -Message "$($_.Exception.Message)"
    }
 
 New-Object PSObject -Property @{
   Name = $LocalAccount.Name
	Caption = $LocalAccount.Caption
	Disabled = $LocalAccount.Disabled
	LockOut = $LocalAccount.LockOut
	"Password Changeable" = $LocalAccount.PasswordChangeable
	"Password Expires" = $LocalAccount.PasswordExpires
	"Password Required" = $LocalAccount.PasswordRequired
   Description = $LocalAccount.Description
   LastPasswordSet = $User.LastPasswordSet
   passwordage = "$((New-TimeSpan $User.LastPasswordSet (Get-Date)).Days) Days"
   memberOf = ($Groups | ?{$_.Members -contains $LocalAccount.Name} | Select -Expand name) -join ","
   }
 }
}

Get-Localadmin

Open in new window

0
 

Author Comment

by:coitadmin
ID: 41823436
This is what it gives me:

Name                                                        Members
----                                                        -------
Administrators
Backup Operators
Cryptographic Operators
Distributed COM Users
Event Log Readers
Guests
IIS_IUSRS
Network Configuration Operators
Performance Log Users
Performance Monitor Users
Power Users
Remote Desktop Users
Replicator
Users
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41823570
What is the result for the updated script?
0
 

Author Closing Comment

by:coitadmin
ID: 41828844
Works great.  Thank you.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question