我的問題是我在控制臺中得到了顯示所有機器“名稱”、LastLogonDate 描述的串列。但是當我運行完整腳本時,只有 1 臺機器資訊是通過郵件發送的。我錯過了什么?對不起,我對 PS 有點陌生。
##Contains my list of computers that I need data from
$Users = 'PCName1', 'PCName2', 'PCName3'
##Gets AdComputer info
$Computers = Get-ADComputer -Filter * -Properties LastLogOnDate, Description
$Users | foreach {
$User = $_
$selectedMachine = $Computers | where Name -eq $User
if ($selectedMachine) {
$selectedMachine | select Name, LastLogOnDate, Description
}
# if it does not exist...
}
[string[]]$recipients = [email protected]
$fromEmail = from@email.com
$server = "IP"
[string]$emailBody = "$Computer" "Mail Rapport sendt fra task scheduler $computer $time "
send-mailmessage -from $fromEmail -to $recipients -subject "Machines INFO " -body $emailBody -priority High -smtpServer $server
uj5u.com熱心網友回復:
你沒有在任何地方捕獲 ForEach-Object 回圈的輸出,所以資訊只出現在控制臺上。此外,我看不到變數$Computer(單數)和$time來自哪里......
此外,我可能會讓您感興趣,讓您使用Splatting在需要大量引數(如 Send-MailMessage)的 cmdlet 上使用。
嘗試
# Contains my list of computers that I need data from
$Computers = 'PCName1', 'PCName2', 'PCName3'
# loop through the list of computers and capture the output objects in variable $result
$result = foreach ($computer in $Computers) {
$pc = Get-ADComputer -Filter "Name -eq '$computer'" -Properties LastLogOnDate, Description
if ($pc) {
# output the object with the properties you need
$pc | Select-Object Name, LastLogOnDate, Description
}
else {
Write-Warning "Computer '$computer' not found.."
}
}
# test if we have something to email about
if (@($result).Count) {
# join the resulting object array (in this demo as simple Table)
$body = "Mail Rapport sendt fra task scheduler`r`n{0}"-f ($result | Format-Table -AutoSize | Out-String)
# use splatting
$mailProps = @{
To = '[email protected]'
From = '[email protected]'
Subject = 'Machines INFO'
Body = $body
Priority = 'High'
SmtpServer = '[email protected]'
# other parameters can go here
}
# send the email
Send-MailMessage @mailProps
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/384895.html
上一篇:檢查一個檔案是否存在于多個目錄中
