Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell Password Info

Posted on 2016-09-19
11
Medium Priority
?
91 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:Jason Chlopek
[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:Jason Chlopek
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:Jason Chlopek
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:Jason Chlopek
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 2000 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:Jason Chlopek
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:Jason Chlopek
ID: 41828844
Works great.  Thank you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

636 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