【问题标题】:Finding Old computers using Powershell, then sending via email使用 Powershell 查找旧计算机,然后通过电子邮件发送
【发布时间】:2015-11-06 14:15:58
【问题描述】:

我是 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

【问题讨论】:

    标签: powershell-4.0


    【解决方案1】:

    这是因为您将 $OUs 变量作为正文导出。 首先你需要将你的计算机存储在一个变量中,试试这个

    # 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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2018-11-01
      相关资源
      最近更新 更多