如何发送谁注销的电子邮件?

时间:2019-05-24 02:42:29

标签: powershell

我希望能够发送一封电子邮件,告知谁注销了每台计算机。我该怎么做?

$ username =读取主机-提示“输入用户名”

创建一个新变量以收集结果。您可以根据需要使用此输出

$ SessionList =“活动服务器会话报告-” + $ today +“ n n”

在Active Directory中查询运行服务器操作系统的计算机

获取内容-Path'\ server \ share $ \ 1-管理工具\ Power Shell脚本\注销用户Servers \ Serverlist.csv'|选择-跳过1 |设置内容“ \ data01 \ it $ \ 1-管理工具\ Power Shell脚本\注销用户\ Servers \ servers.csv”

$ Servers =获取内容-Path'\ server \ share $ \ 1-管理工具\ Power Shell脚本\注销用户Servers \ Servers.csv'

浏览列表以查询每个服务器的登录会话

ForEach($ Servers中的$ Server){

$list = $Server -replace '"', "" 

# When running interactively, uncomment the Write-Host line below to show which server is being queried 
$message = "Querying" +" "+ $list 


$message

# Run the qwinsta.exe and parse the output 
$queryResults = (qwinsta $username /server:$Server | foreach { (($_.trim() -replace "\s+",","))} | ConvertFrom-Csv)

# Pull the session information from each instance 
ForEach ($queryResult in $queryResults){ 
    $RDPUser = $queryResult.USERNAME 
    $sessionType = $queryResult.ID
    if ($sessionType -eq "Disc"){$sessionType = $RDPUser} else { $sessionType = $queryResult.ID}

    Write-Host "Logging $username off of $list with Session# $sessionType"

     logoff $sessionType /server:$Server

      } 

}

1 个答案:

答案 0 :(得分:0)

我看到您已经有了用户名。因此,如果您安装了AD module,则可以使用

$Mail = (Get-ADUser $username -properties Mail).mail

获取用户的电子邮件ID。然后,您可以使用

Send-MailMessage -to $mail -From <random mail id for eg noreplyAD@mail.com> -Body "Your $username has been logged off of $server" -Subject "Logoff notification" -SmtpServer <yourcompany's smtpserver>

向用户发送通知