Oneliners! I do like them, but no matter how nice those oneliners might look, they sometimes just isn't the best solution.
I'll give you an example:
In my last post where I wrote of using the Convert-Username CmdLet, I decided to put 2 lists of information in separate variables. $adUsers and $qUsers, like this:
$qUsers = get-quota c:\homefolders\* | where { $_.usage -gt .8 }
$adUsers = $qUsers | foreach { "MYDOMAIN\$(split-path $_.quotapath -leaf)" } |
convert-username -i nt4account -o dn | foreach { [adsi]"LDAP://$($_.Name)" }
0..($qUsers.length-1) | foreach {
$qUser = $qUsers[$_]
$adUser = $adUsers[$_]
.... <"send an email"-code was here> ....
}
One alternativ to this could have been:
get-quota c:\homefolders\* |
where { $_.usage -gt .8 } |
foreach {
add-member NoteProperty User ([ADSI](convert-username "MYDOMAIN\$(split-path $_.quotapath -leaf)" -i nt4account -o dn)) -pass
} | foreach {
.... <"send an email"-code was here> ....
} (Disclaimer: I haven't actually tested the code above, but it should work)
Even though the last example is shorter (I'm not saying easier) and doesn't use any variables, it does come with a performance-penalty.
Before I tell you what that is, I need to explain a few things:
In programming-world there's a notion about Chunky and Chatty code.
The thing is that as soon as you go on to the wire (ie the network) things take time. A Chatty application sends many small requests, paying the performance-penalty many time (no matter how small the requests are). A Chunky application on the other hand builds one big meaty request, paying the penalty only once.
Convert-Username uses the Microsoft API DsCrackNames which has the possibility to send a whole bunch of names to the DC/GC and get all those names converted in a single operation. When I created Convert-Username, I wanted to utilize that feature, so when the strings (names) are piped to Convert-Username, it just collects all of them first and then sends one request, thus making Convert-Username a Chunky CmdLet.
The problem with the second code-segment above is that instead of piping all the names to Convert-Username directly, it's used inside a foreach-loop and executed once for every name and by that forcing it to be Chatty.
So, next time you have a slow running oneliner; think about when things go on the wire and see if you can eliminate some of them.
Posted
08-16-2007 1:14
by
bjorn.osterman