简体   繁体   中英

Send email to multiple address in powershell

$PE is holds the email address of a user which is retrieved from AD user profile. I need to sent email to that user. As it keeps on changing as per user profile I am finding it difficult to pass it into this email function.


$PE=$(Get-aduser -filter {name -like $name} -properties mail).mail
Function abc($subject,$body,$PE)
{
    
    $Htmlbody="____"
    $toaddress = $PE
    
    If (attachment -eq "")
    {
        Send-emailmessage -from "abc@xym.com" -bodyashtml -To $toaddress -body $htmlbody -subject $subject
    }
}

I tried the above but I am getting an error for -To field and I don't see email address in $PE whereas $PE is holding the value before calling this function and inside the function it's empty.

Here is the error :Cannot validate argument on parameter 'To' .the argument is null or empty . Provide an argument that is not null or empty . Then try again

(1) Variables defined in a function are applicable only inside that function unless you scope otherwise - see Powershell documentaiton about "Scoping" for details

(2) Create the function then PASS the parameters to the function. It's easier to understand this if you use the "longhand" way of passing parameters

$PE=$(Get-aduser -filter {name -like $name} -properties mail).mail



Function abc {
    Param(
    $subject,
    $body,
    $toaddress)

    Send-emailmessage -from "abc@xym.com" -bodyashtml -To $toaddress -body $body -subject $subject
}

abc -subject "MySubject" -body "____" -toaddress $PE

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