简体   繁体   中英

Im trying to send an automated email to users with current AD details using powershel ISE to grab AD details..But Ive never done this before

What I have I've never done this before but my boss gave me a hint how to go about this..but im still confused..can anyone help me out

enter code here$a = get-aduser -filter * -SearchBase 'OU=_Users,OU=Head Office,OU=1_A,OU=1_Oceania,OU=L,DC=l,DC=global' -SearchScope OneLevel -Properties employeeId,pager | Select name,EMPLOYEEid,PAGER | Where-Object -Property EmployeeId -EQ $null| Out-GridView


FOREACH ($B IN $A) {if (($B.pager-ne $null) -AND ($B.EmployeeID -eq $null)) {

$emp_Email = ''
$emp_Name = ''
$emp_Job = ''
$emp_Company = ''
$emp_Mobile = ''
$emp_DirectPhone= ''
$emp_empyloeeID = ''
$emp_mainnumber= ''
$emp_url=''
$emp_from=''

Hi <<UserName>>,
On the Jan 20th a new email sign software will be installed. This will standardize all email signs across all devices. Please check the below to ensure the details are correct. If there is missing data please reply to this email to have this addressed:
Name: <<Name>>
EmployeeId : <<EmpId>>
Job Title: <<Position>>
Company: <<Company>>
Mobile : <<Mobile>>
Direct Land Line: <<Direct>>
Main Number: <<Telephone>>
URL: <<URL>>
Please note that if some of the fields provided are empty do not panic the fields will be filled in.
}
}

I'm guessing the properties to use for the labels you give them in your code, but you could build on this.
It uses Here-Strings , ConvertTo-Html and Splatting

# create a nice CSS style to format the table using a Here-String
$style = @'
<style>
    body {font-family: Calibri, Tahoma, Helvetica, sans-serif; color: black;}
    table {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
    td {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
'@

# the text to start the email with. This has one placeholder {0} that will be filled in with the user's first name in the loop
$preText = @'
Hi {0},<br /><br />
On the Jan 20th a new email sign software will be installed. This will standardize all email signs across all devices.<br />
Please check the below to ensure the details are correct. If there is missing data please reply to this email to have this addressed:
'@

# the static text to end the email with
$postText = '<br />Please note that if some of the fields provided are empty do not panic the fields will be filled in.'

# Get-ADUser already returns objects with these properties:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
# anything else you need to ask through the Properties parameter
$props = 'EmployeeID', 'pager', 'Company', 'MobilePhone', 'OfficePhone', 'HomePhone', 'HomePage', 'Title', 'EmailAddress'
$users = Get-ADUser -Filter * -SearchBase 'OU=_Users,OU=Head Office,OU=1_A,OU=1_Oceania,OU=L,DC=l,DC=global' -SearchScope OneLevel -Properties $props | 
         Where-Object {[string]::IsNullOrWhiteSpace($_.EmployeeId) -and ($null -ne $_.pager)}

foreach ($user in $users) {
    # fill in the '{0}' placeholder for the leading text with the users first name
    $leaderText = $preText -f $user.GivenName
    # create a sub selection of the properties needed in the email
    $properties = $user | Select-Object Name, EmployeeId,
                                        @{Name = 'Job Title'; Expression = {$user.Title}},
                                        Company,
                                        @{Name = 'Mobile'; Expression = {$user.MobilePhone}},
                                        @{Name = 'Direct Land Line'; Expression = {$user.OfficePhone}},
                                        @{Name = 'Main Number'; Expression = {$user.HomePhone}},
                                        @{Name = 'URL'; Expression = {$user.HomePage}}


    # create the email body using the style above for the table
    $body = $properties | ConvertTo-Html -Head $style -PreContent $leaderText -PostContent $postText -As List

    # use Splatting on cmdlets that take a lot of parameters:
    $mailParams = @{
        From       = 'you@yourcompany.com'
        To         = $user.EmailAddress
        Subject    = 'New email sign-in software'
        Body       = $body -join "`r`n"
        BodyAsHtml = $true
        Priority   = 'High'
        SmtpServer = 'smtpmail.yourcompany.com'
        Port       = 587
        # more parameters go here
    }
    # send the email
    Send-MailMessage @mailParams
}

Of course, try this out on a dummy user first and make sure the email is sent to yourself to test the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM