Options

Power shell help

DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
Hi,

I have the following Power-shell line that will search an OU for users not enabled for Microsoft Lync and then enable them

get-csaduser -filter {Enabled -ne $True} -OU "OU=myOU,OU=Accounts,OU=domain,DC=iah,DC=ac,DC=uk" | Enable-CsUser -RegistrarPool "pool.iah.ac.uk" -SipAddressType EmailAddress -SipDomain domain.ac.uk

now part of the get-csuser information returned is the email address, so what I want to do is once the account is created is email the user to let them know all from the script.

Sending email using Powershell script - SharePoint and Others - Site Home - MSDN Blogs

I know all I want to do is take each "windowsemailaddress" value returned from the filtered get-csaduser and pass it to a mail function for use as the send to address, but my PS is very basic.

Any one who is a power shell guru and could point me in the right direction I would be really grateful.

Cheers
  • If you can't explain it simply, you don't understand it well enough. Albert Einstein
  • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.

Comments

  • Options
    QordQord Member Posts: 632 ■■■■□□□□□□
    Without actually seeing an output, I'm not sure how best to work with it to grab the email address. I'm not good enough to keep it a one-liner, so this is probably how I'd start to try and make a script that'll make it happen:
    $needtoemail =  get-csaduser -filter {Enabled -ne $True} -OU "OU=myOU,OU=Accounts,OU=domain,DC=iah,DC=ac,DC =uk" 
    foreach ($pers in $needtoemail)
    {
    Enable-CsUser -RegistrarPool "pool.iah.ac.uk" -SipAddressType EmailAddress -SipDomain domain.ac.uk
    $address = $pers.windowsemailaddress
    
    ## Email parts
    $EmailFrom = "DevilWAH@awesomesauce.org"
    ## $EmailTo = $address ## <- Not sure if you'll nee this line or not. I've put $address in the "to" field below, but if it doesn't work change that to $EmailFrom and uncomment this line
    $EmailBody = "This is the body of the email"
    $EmailSubject = "This is the subject line of the email"
    $SMTPServer = "smtp.awesomesauce.org"
    $SMTPPort = "25"
    $Message = New-Object Net.Mail.MailMessage($EmailFrom, $address, $EmailSubject, $EmailBody)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SMTPPort)
    $SMTPClient.Send($Message) 
    }
    
    I'd start by changing your filter to find *only* you so you can test it freely.

    EDIT: I think you might be better off first making sure that will properly pull the email address you want. You should be able to test that with this:
    $needtoemail =  get-csaduser -filter {Enabled -ne $True} -OU "OU=myOU,OU=Accounts,OU=domain,DC=iah,DC=ac,DC =uk" 
    foreach ($pers in $needtoemail)
    {
    $address = $pers.windowsemailaddress
    write-host $address
    }
    
    If this returns the proper email address, then the code chunk above will too.

    Disclaimer: I've never worked with any of the Lync commands and have no way to test that part of it. The rest should be good though. :)
  • Options
    DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    Qord wrote: »
    Without actually seeing an output, I'm not sure how best to work with it to grab the email address. I'm not good enough to keep it a one-liner, so this is probably how I'd start to try and make a script that'll make it happen:
    $needtoemail =  get-csaduser -filter {Enabled -ne $True} -OU "OU=myOU,OU=Accounts,OU=domain,DC=iah,DC=ac,DC =uk" 
    foreach ($pers in $needtoemail)
    {
    Enable-CsUser -RegistrarPool "pool.iah.ac.uk" -SipAddressType EmailAddress -SipDomain domain.ac.uk
    $address = $pers.windowsemailaddress
    
    ## Email parts
    $EmailFrom = "DevilWAH@awesomesauce.org"
    ## $EmailTo = $address ## <- Not sure if you'll nee this line or not. I've put $address in the "to" field below, but if it doesn't work change that to $EmailFrom and uncomment this line
    $EmailBody = "This is the body of the email"
    $EmailSubject = "This is the subject line of the email"
    $SMTPServer = "smtp.awesomesauce.org"
    $SMTPPort = "25"
    $Message = New-Object Net.Mail.MailMessage($EmailFrom, $address, $EmailSubject, $EmailBody)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SMTPPort)
    $SMTPClient.Send($Message) 
    }
    
    I'd start by changing your filter to find *only* you so you can test it freely.

    EDIT: I think you might be better off first making sure that will properly pull the email address you want. You should be able to test that with this:
    $needtoemail =  get-csaduser -filter {Enabled -ne $True} -OU "OU=myOU,OU=Accounts,OU=domain,DC=iah,DC=ac,DC =uk" 
    foreach ($pers in $needtoemail)
    {
    $address = $pers.windowsemailaddress
    write-host $address
    }
    
    If this returns the proper email address, then the code chunk above will too.

    Disclaimer: I've never worked with any of the Lync commands and have no way to test that part of it. The rest should be good though. :)

    Cheers for taking the time for that I will look in to that now
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • Options
    QordQord Member Posts: 632 ■■■■□□□□□□
    My pleasure! Let me know how it goes.
  • Options
    lsud00dlsud00d Member Posts: 1,571
    You can send an email using the 'send-mailmessage' cmdlet and make all that a one-liner.

    But Qord has the right idea of iterating through a for loop.
  • Options
    DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    OK so here is the code that does what I want, I need to write the mail function but that should be straight forward enough. (oh and add comments of course!!

    But this finds the users, adds them to the pool and then prints the email address :)
    $needtoemail =  get-csaduser -filter {Enabled -ne $True} -OU "OU=LyncTest,OU=TestAccounts,OU=Accounts,OU=domain,DC=iah,DC=ac,DC=uk"foreach ($pers in $needtoemail)
    {
    Enable-CsUser -RegistrarPool "pool.iah.ac.uk" -SipAddressType EmailAddress -SipDomain domain.ac.uk -Identity $pers.identity
    
    
    $address = $pers.windowsemailaddress
    write-host $address
    }
    

    Thank you sir
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • Options
    QordQord Member Posts: 632 ■■■■□□□□□□
    lsud00d wrote: »
    You can send an email using the 'send-mailmessage' cmdlet and make all that a one-liner.

    Maybe...?
    get-csaduser -filter {Enabled -ne $True} -OU "OU=myOU,OU=Accounts,OU=domain,DC=iah,DC=ac,DC =uk" | foreach {Enable-CsUser -RegistrarPool "pool.iah.ac.uk" -SipAddressType EmailAddress -SipDomain domain.ac.uk; send-mailmessage -to $_.windowsemailaddress -From "DW <DevilWAH@awesomesauce.org>" -Subject "Email subject line" -Body "Body of email"}
    
    I don't try one-liners often, so this is definitely a test. Might work, might not. icon_cheers.gif
  • Options
    DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    Qord wrote: »
    Maybe...?
    get-csaduser -filter {Enabled -ne $True} -OU "OU=myOU,OU=Accounts,OU=domain,DC=iah,DC=ac,DC =uk" | foreach {Enable-CsUser -RegistrarPool "pool.iah.ac.uk" -SipAddressType EmailAddress -SipDomain domain.ac.uk; send-mailmessage -to $_.windowsemailaddress -From "DW <DevilWAH@awesomesauce.org>" -Subject "Email subject line" -Body "Body of email"}
    
    I don't try one-liners often, so this is definitely a test. Might work, might not. icon_cheers.gif

    :) I think i will leave it as multi line as its easier to read and comment :) but thanks for all the hep and here is my almost finshed script
    function mail($address2)
    {
    
    
        Write-Host "Sending Email to " $address2
    
    
        #SMTP server name
        $smtpServer = "smtp.domain.ac.uk"
    
    
    
    
        $EmailFrom = "Lync@domain"
        $emailto = "aaron@domain"
        $Subject = "You have been enabled for Lync" 
        $Body = Get-Content ("c:\temp\mail.html")
    
    
        $SMTPClient = New-Object Net.Mail.SmtpClient($smtpServer)  
        $message = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)
        $message.IsBodyHtml = $true;
        $SMTPClient.Send($message)
      
    }
    
    
    
    
    $needtoemail =  get-csaduser -filter {Enabled -ne $True} -OU "OU=LyncTest,OU=Test,OU=Accounts,DC=domain,DC=ac,DC=uk"
    foreach ($pers in $needtoemail)
    {
     Enable-CsUser -RegistrarPool "pool.iah.ac.uk" -SipAddressType EmailAddress -SipDomain domain.ac.uk -Identity $pers.identity
    
    
    $address = $pers.windowsemailaddress
    mail $address
    }
    
    
    

    Which does exactly what I want... enables the user and send an email (from an HTML file) that lets them know how to log in, some of the functions, links to online help and service desk.

    Now added to a scheduled task that runs every hour and no more service desk forgetting to add new users to the Lync server :) I know I could have added it as part of the new user script that creates the AD accounts, but not all users are created like this, so this way as long as they are in OU it will pick them up.

    I changed the sendmail a bit to allow HTML body, but other than that its pretty much as you suggested, your help very much appreciated.
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
Sign In or Register to comment.