Solved

Powershell Password Info

Posted on 2016-09-19
11
79 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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