Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

I am a newbie please help and be nice this stuff is going over my head.

$
0
0
Hi all

Sorry for this but I am a newbie to scripting I need help with following powershell script

In AD i need to do the following

I have a container called Users, Under the users container I have Disabled Accounts, New Account, Others Accounts.

I need to pull last logon information from just the users conatiner, as I am not interested in the users in Disabled Accounts, New and other.

Is there a way to do this?

I have the following below but this pulls all user infromation from Users, Disabled Accounts, New Account, Others Accounts, but I only need the information from Users.

The below is what I have so far.

 

 

Import-Module ActiveDirectory

function Get-ADUsersLastLogon()

{

$dcs = Get-ADDomainController -Filter {Name -like "*"} $users = Get-ADUser -searchbase "OU=Users,DC=Test" -Filter * $time = 0 $exportFilePath = "c:\lastLogon.csv"

$columns = "name,username,datetime"

Out-File -filepath $exportFilePath -force -InputObject $columns

foreach($user in $users)

{

foreach($dc in $dcs)

                    {

$hostname = $dc.HostName

$currentUser = Get-ADUser $user.SamAccountName | Get-ADObject -Server $hostname -Properties lastLogon

 

if($currentUser.LastLogon -gt $time)

{

$time = $currentUser.LastLogon

}

}

$dt = [DateTime]::FromFileTime($time)

$row = $user.Name+","+$user.SamAccountName+","+$dt

Out-File -filepath $exportFilePath -append -noclobber -InputObject $row $time = 0 } } Get-ADUsersLastLogon

 

 

 

 

 

Any Help would be great.


Viewing all articles
Browse latest Browse all 6937

Trending Articles