Solved

Powershell Password Info

Posted on 2016-09-19
11
67 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
A procedure for exporting installed hotfix details of remote computers using powershell
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 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