Sorry Jeff, a little quick on the draw. Instead of foreach ($DC in $DCs), it should have been foreach ($DC in $AllDCs) as per your code. I’ve updated that post. Give it a shot.
It runs without error and simply outputs the Name of the DC and no values under Filepath. I tested it with a keyword (TestUser@myCompany.com) for which I know exists in a .ps1 file but the Get-ChildItem isn’t finding it.
Thanks Jeff, I realized more needed to be addressed here and I spaced on the pscustomobject.
If you use the -Include parameter, you do need an asterisk (*) at the end of -Path E:</code>, otherwise you’ll get nothing back. So, here is what should work:
Now, keep in mind that though this code would work for what it is, the reference to DCs is irrelevant. The code is just looping through DCs in a text file (assumingly Domain Controller names), and for each one found in the file, it outputs the DC name with a file path found against some pattern you specify.
If 150 files are found matching the pattern, those results will output for each DC in the text file. This means if you have 10 DCs in the text file, the output will render 1500 results, almost all of which are redundant.
Are you intending to run this against domain controller found in the .txt file? What is your plan for this script?
ah, I had no idea about the * in the -Include, so thank you.
They are indeed DC’s and I’m looking for that one script (either .ps1 or .xml) that has a username, generating lockouts etc. So wouldn’t expect it to be on all DCs but rather, one.
There is a glaring issue in the code as posted. You are not initiating a remote session to the DCs nor are you defining a UNC with the -LiteralPath parameter. As it appears now, its going to list the DCs and invoke GCI against your local E:\ for reach DC in AllDCs.
~added “*” to the end of those paths (i.e. “C:\users*”)
~ added -ErrorAction SilentlyContinue at the end of the Invoke-command, to ignore the access denied warnings to some folders
I also learned a lot from this, so thank you Aapeli.