Hello,
So, first time poster here, so I’m sure I’ll screw up the formatting.
I looked around, but didn’t find anything here that seemed to match my situation, but I certainly could have missed it.
I’m trying to cobble together a simple script to query a list of computers for a particular Event ID. I have sections of this working, and I’m a little lost as to why this isn’t working correctly.
I can get my list of domain controllers, no problem:
$computername = get-adgroupmember "Domain Controllers" | select name
I can get a filtered event log if I put an actual computer name into the statement:
get-eventlog -logname "Directory Service" -ComputerName DC1 | ?{$_.eventid -eq "1864"} | select MachineName,EventID,TimeGenerated
I can have the script get a list of each DC and echo it back to me:
$computername = get-adgroupmember "Domain Controllers" |select name
foreach ($computer in $computername) {$computer}
And it spits the list back out at me, so I know the loop is iterating as I expect.
So why does this not work?:
$computername = getadgroupmember "Domain Controllers" |select name
foreach ($computer in $computername)
{
get-eventlog -logname "Directory Service" -ComputerName $computer | ?{$_.eventid -eq "1864"} | select MachineName,EventID,TimeGenerated
}
-ComputerName wants a string. I’m not sure if I’m passing a string or an object down the pipeline, honestly. I tried adding “out-string” after the “select name.” I’ve tried dumping the list to a file and using
$computername = get-content "C:\folder\computername.txt"
And have verified the txt file contains the proper output. I tried removing the “name” and dash line from the top of the text file. And the “get-content” method works the same when put into the “foreach” proof loop.
But, when trying to loop this, I get:
get-eventlog : The network path was not found.
At C:\eventid\1864-3.ps1:4 char:10
+ get-eventlog -logname "Directory Service" -ComputerName $computer | ?{$ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-EventLog], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.GetEventLogCommand
This has to be something small and stupid I’m missing… any help is greatly appreciated.
Jason