The content below is taken from the original ( Why did I not learn to use ValueFromPipeline earlier – This is awesome!), to continue reading please visit the site. Remember to respect the Author & Copyright.
I’ve been redoing our password expiration reminder script for my company, and due to some convoluted things it needs to do, I decided to invest some time learning some of the Advanced Powershell Function options.
The new script has only a single line outside of functions and using the "process" part of an Advanced Function, I do all the iteration via this, instead of foreach loops.
This ends with a nice single line that pipes the AD users that needs to receive an email, to the function that creates the object used by Send-MailMessage, then pipes that object and splats it to be used in the Send-MailMessage.
Can really encourage anyone writing scripts to take some time utilising this.
A code example of how that looks:
$accountsToSendEmail | New-PreparedMailObject -includeManager | Foreach-Object { Send-MailMessage @_ }
submitted by /u/ostekages to r/PowerShell
[link] [comments]