PowerShell - 结果发送到电子邮件正文 [英] PowerShell - Results to the body of the email

查看:501
本文介绍了PowerShell - 结果发送到电子邮件正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

# From address of email notification
[String]$emailFromAddress = "someone@company.com"
# To address of email notification
[String]$emailToAddress = "someone@company.com"
# Subject of email notification
[String]$emailSubject = "Subject"
# Exchange server
[String]$emailMailserver = "someserver.company.com"


$names = Get-ADUser -Filter 'SamAccountName -like "ahs_*"'

foreach($name in $names){

function Get-ADUserLastLogon([string]$userName)
{
  $dcs = Get-ADDomainController -Filter {Name -like "*"}
  $time = 0
  foreach($dc in $dcs)
  { 
    $hostname = $dc.HostName
    $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon 
    if($user.LastLogon -gt $time) 
    {
      $time = $user.LastLogon
    }
  }
  $dt = [DateTime]::FromFileTime($time)
  $Body = Write-Host $username "last logged on at:" $dt | Out-String }

  Get-ADUserLastLogon -UserName $name
  }

# Send the results in an email
Send-MailMessage -From $emailFromAddress -To $emailToAddress -Subject $emailSubject -Body $Body -SmtpServer $emailMailserver


我是PowerShell的新手,我遇到了脚本问题。 请帮忙。我试图从AD中提取特定用户并确定他们的上次登录时间,然后将输出通过电子邮件发送给组。 该脚本将收集我想要的信息
,它将在控制台上显示,但始终无法将电子邮件发送为:

I am new to PowerShell and am having an issue with a script.  Please help. I am trying to pull specific user's from AD and determine when their last logon time was and then email the output to a group.  The script will collect the information I want and it will display it on the console but it always fails to send the email as:

Send-MailMessage:无法验证参数'Body'的参数。参数为null或空。提供
不为null或为空的参数,然后再次尝试该命令。




推荐答案

您不能使用Write-Host,即用于输出到控制台而不是其他任何内容。将其更改为Write-Output。
You cannot use Write-Host, that is for outputting to the console and nothing else. Change it to Write-Output.


这篇关于PowerShell - 结果发送到电子邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆