So I just recently started investigating PowerShell workflows and loops with the -parallel switch. My first thought, when I started reading about it was: “This is great. I should be able to write a script that queries every domain controller in our environment for a user’s ‘lastlogon’ attribute, and instead of waiting for every DC to respond (we’ve got about 80 in our environment) query every DC, essentially simultaneously.”
However, my first attempt at a script didn’t work out the way I wanted. He’re what I tried:
---------------------------begin script-----------------------------------------
workflow querydcs {
param([string]$computers)
foreach -parallel ($computer in $computers)
{
get-aduser “MyUserAccount” -server $computer -properties lastlogon | select @{name=“DC” ; expression={$computer}},lastlogon
}
}
$computers = get-addomaincontroller -filter * | select -expandproperty name
querydcs $computers
--------------------------------------------end script----------------------------------------------------
(Note: for simplicity’s sake, in this example I’m not converting the lastlogon attribute to human readable form)
So, the script appears to work as intended EXCEPT my output only includes the “lastlogon” value from the select statement but not the “DC” attribute value, which is just the value of the looping variable.
Output looks something like:
DC:
LastLogon: 5/10/2017 10:50:23 PM
PSComputerName: localhost
PsSourceJobInstanceID: hexhexhexh-hex-hex-hex-hexhexhex
Clearly there’s a fair amount going on under the hood that I don’t understand, here (why are PSComputerName and PsSourceJobInstanceID being returned, for instance). My biggest gripe, though, is that knowing the last time a user authenticated against a random DC is significantly less useful than also knowing the what DC he authenticated against. So what am I doing wrong here?