Frightlin
Frightlin

Reputation: 161

How can I initialize the names in this loop and create the parameter?

 param(
 [string]$UserName
 )

 $DCs =  Get-ADGroupMember "Domain Controllers"

 foreach ($DC in $DCs)
 {
Write-Host $DC.name
 }
 $EventList = Get-Eventlog –ComputerName $ComputerName -LogName Security -InstanceID 4740         -UserName *Username* -After "07/20/2014"

 $EventList | Format-List -Property TimeGenerated,Message 

I'm writing a script to figure out lockout events for specific users within all the domains. I'm trying to figure out how to the computername variable from the $DC.name, I know at some point i'll have to move the $Eventlist = Get-Eventlog line within the loop.Then would I just delete the write-host $DC.name?

Upvotes: 0

Views: 43

Answers (1)

Benjamin Hubbard
Benjamin Hubbard

Reputation: 2917

Get-EventLog can take an array of computernames. You don't even need the foreach loop. So what you could do is this:

$DCs = Get-ADGroupMember "Domain Controllers"
$EventList = Get-Eventlog –ComputerName $DCs.Name -LogName Security -InstanceID 4740 -UserName *Username* -After "07/20/2014"
$EventList | Format-List -Property TimeGenerated,Message 

Note that you should only use Format-List if you do not intend to use the information for anything other than displaying in the console. If you want to use the object for anything, don't use Format-List or Format-Table.

Upvotes: 1

Related Questions