Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.
Sign up to join this community
Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
I can’t figure out what I am doing wrong here in powershell. I am trying to email myself logged in user accounts on user change but I can’t get it to work properly, it just gives me garbled unformatted messages. It works to a point but I would like it formatted instead of emailing me a single line.
Is this possible?
$users = query user /server:localhost $SmtpServer = 'xx.xx.xx.xx' # SMTP Server name $Port = 25 # SMTP server port number – default is 25 $From = 'email@example.com' # from address - doesn't have to be valid, just in the format firstname.lastname@example.org and something your mail filter will not block $To = 'email@example.com' # email address to send test to $Subject = 'xxx.xx.xx.xx - Server login detected of account.' # email subject $Body = 'This is an automated alert to notify you that there has been a change in user status: '+$users+' ' # email body Send-MailMessage -SmtpServer $SmtpServer -Port $Port -From $From -To $To -Subject $Subject -Body $Body ``` I get the email as follows: USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME accountname rdp-tcp#39 3 Active . 3/30/2021 5:54 PM accountname2 rdp-tcp#94 9 Active 29 3/30/2021 9:01 PM