2015-11-06 46 views
0

我是Powershell的新手,我试图通过电子邮件发送输出。如果我在Powershell中运行该脚本,它会给我提供正确的输出,但电子邮件只会为我提供SearchBase OU。使用Powershell查找旧电脑,然后通过电子邮件发送

# 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 = "[email protected]" 
$Rcpnt = "[email protected]" 
$SMTP = "smtp.y.com" 
$SUBJECT = "Old Computers Report from $env:ComputerName.$env:USERDNSDOMAIN - $((Get-Date).ToShortDateString())" 
$Mail_Body = $OUs | Out-String 

回答

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 = "[email protected]" 
$Rcpnt = "[email protected]" 
$SMTP = "smtp.y.com" 
$SUBJECT = "Old Computers Report from $env:ComputerName.$env:USERDNSDOMAIN - $((Get-Date).ToShortDateString())" 
$Mail_Body = $computers | Out-String