Email logon alert windows server

Welcome Forums General PowerShell Q&A Email logon alert windows server

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
1 week, 4 days ago.

  • Author
    Posts
  • #175291

    Participant
    Topics: 1
    Replies: 0
    Points: 12
    Rank: Member

    Greetings, I'm working on a powershell code that alerts you by email when a user is right on the server.
    When the login is local on the machine itself the alert is sent correctly, but when directly via terminal server, the email does not show the username of the logged in user.
    How do I fix this error? Here's a copy of the code below:

    
    

    Function Send-Mail {

    $User = (Get-WmiObject -Class win32_computersystem -ComputerName .).UserName
    $strHost = [Environment]::MachineName

    $Content = "

    Alerta de email

    " +
    "
    " +
    "" +
    "The user " + $User + " logged in to the server: " + $strHost + "" +
    "
    " +
    "Schedule: " + (Get-Date).toString("dd/MM/yyyy HH:MM:ss") + "
    "

    $Assunto = "The user " + $User + " logged in to server: " + $strHost

    $strDe = "xxx@xxx.com.br"
    $strPara = "yyy@xxx.com.br"
    $strPwd = "xyxyxyxyxy"
    $strConteudo = $Content
    $SMTPServer = "smtp.xxx.com.br"
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($strDe,$strPara,$Assunto,$strConteudo)
    $SMTPMessage.IsBodyHtml = $true
    $SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587)
    $SMTPClient.EnableSsl = $false
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($strDe, $strPwd);
    $SMTPClient.Send($SMTPMessage)
    Remove-Variable -Name SMTPClient
    Remove-Variable -Name strPwd
    }

    Send-Mail

    [/ pre]

  • #175309

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    its difficult to understand the code(formatting), can you post it in gist.github.com and share the link here ?

  • #175474

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    Wanderton,

    Can you clarify if you are running this code remotely or locally on the computer? If I understand correctly you are looking to pull the users from a remote server as well?

     

You must be logged in to reply to this topic.