使用Powershell查找旧计算机,然后通过电子邮件发送

时间:2015-11-06 14:15:59

标签: powershell-v4.0

我是Powershell的新手,我试图通过电子邮件发送输出。如果我在Powershell中运行脚本,它会给我正确的输出,但电子邮件只给我一个SearchBase OU。

# The 60 is the number of days from today since the last logon
$xDays = (Get-Date).AddDays(-20)

# List of SearchBase locations
$OUs = @('OU1','OU2','OU3','OU4')

$OUs | ForEach-Object {Get-ADComputer -Property Name,CanonicalName,OperatingSystem,LastLogonDate -Filter {LastLogonDate -le $xDays -and Name -NotLike '*-vm'} -SearchBase $_ } | Fl Name,OperatingSystem,CanonicalName,lastLogonDate | Out-String

$From = "x@y.com"
$Rcpnt = "y@y.com"
$SMTP = "smtp.y.com"
$SUBJECT = "Old Computers Report from $env:ComputerName.$env:USERDNSDOMAIN - $((Get-Date).ToShortDateString())"
$Mail_Body = $OUs | Out-String

1 个答案:

答案 0 :(得分:0)

这是因为您正在将$ OU变量导出为正文。 首先,您需要将计算机存储在变量中,请尝试此

# The 60 is the number of days from today since the last logon
$xDays = (Get-Date).AddDays(-20)

# List of SearchBase locations
$OUs = @('OU1','OU2','OU3','OU4')

$OUs | ForEach-Object {$computers = Get-ADComputer -Property Name,CanonicalName,OperatingSystem,LastLogonDate -Filter {LastLogonDate -le $xDays -and Name -NotLike '*-vm'} -SearchBase $_ } | Fl Name,OperatingSystem,CanonicalName,lastLogonDate | Out-String

$From = "x@y.com"
$Rcpnt = "y@y.com"
$SMTP = "smtp.y.com"
$SUBJECT = "Old Computers Report from $env:ComputerName.$env:USERDNSDOMAIN - $((Get-Date).ToShortDateString())"
$Mail_Body = $computers | Out-String