Friday, February 13, 2015

Everyday Powershell - Part 31 - Send email with multiple attachments

So back in Part 24 reader sivaramsharma asked if send-mailmessage could handle multiple attachments so we threw together this post...

$files = get-childitem C:\reports | where name -like "*.png"
$body = @()
$attachments = @()
foreach($file in $files){
    $filename = []::GetFileName($file.FullName)
    $attachments += $file.fullname
    $body += "TEST <br />
<img src='" + $filename + "'/>TEST <br />"
$body = $body | Out-String
Send-MailMessage -to -From -SmtpServer somemailserver -Subject "Test" -BodyAsHtml $body -Attachments $attachments

It's a neat little example of generating some HTML in a loop then sending it off using send-mailmessage.

Thursday, February 5, 2015

Everyday Powershell - Part 30 - Beware of reinventing the wheel

Hmmm bit of a funny one today... So I spent a few minutes writing this;

function get-recursegroupmembers{
    $users = @()
    $groups = get-adgroupmember $groupname
    foreach($object in $groups){
        if ($object.objectclass -eq "group"){
            get-recursegroupmembers $object
        if ($object.objectclass -eq "user"){
            $users += $object

When I realised that this would do just fine;
get-adgroupmember $group -Recursive

So yeah, lesson for the day is don't spend valuable time writing stuff when the problem has already been solved.