从正在运行的进程检查发送带有 CSV 的电子邮件
Posted
技术标签:
【中文标题】从正在运行的进程检查发送带有 CSV 的电子邮件【英文标题】:Sending email with CSV from a Running Process check 【发布时间】:2022-01-23 00:24:18 【问题描述】:我正在尝试检查一个进程是否正在远程计算机上运行(最终将是大约 100 台计算机)。如果进程没有运行,我希望它把计算机名/IP 放入 CSV,然后通过电子邮件发送出去。如果该过程在所有机器上运行,我希望脚本根本不发送电子邮件。为此,我想先测试机器以检查它们是否在线(如果它们离线,我们要么遇到更大的问题,要么出于某种原因关闭,但这不是这个过程正在检查的内容。
目前我将在几台机器上使用记事本进程测试这个脚本,因为我可以在测试机器上相对快速地完成它。
我现在有点卡住,因为我不知道如何将流程检查的结果放入 CSV 然后通过电子邮件发送。在下面的代码 sn-p 中,它没有生成输出文件,而是留下了我正在测试的变量以及附件在发送邮件消息中的路径。任何建议都将不胜感激,我目前仍在学习 powershell,所以还不知道所有的技巧和技巧。
干杯
# Mail server Configuration
$MailServer = "mail.server.co.uk"
$MailFrom = MailFrom@server.co.uk"
# Mail Content Configuration
$MailTo = "Recipient@Server.co.uk"
$MailSubjectFail = "INS Process not running on $DAU"
$MailBodyFail = "The INS Process on the DAU $DAU is not running. Please manually start process on DAU $DAU"
# Process Info
$Process = "Notepad"
$ProcessIsRunning = Get-Process $Process -ErrorAction SilentlyContinue
#Results Info
$Exportto = "C:\Scripts\Content\INSChecker\Results.csv"
# Get DAU Information
foreach($line in (Get-Content C:\Scripts\Content\INSChecker\INSList.cfg))
$line = $line.split(",")
$DAU = $line[0]
$DAUIP = $line[1]
# Test Connection to INS DAU
write-host "Testing: $DAU / $DAUIP"
$TestDAU = Test-Connection $DAU -quiet
$TestDAUIP = Test-Connection $DAUIP -quiet
write-host "Tests: $TestDAU / $TestDAUIP"
If($TestDAU -ne 'True')
If($TestDAUIP -ne 'True')
write-host "DNS Not resolved for $DAU"
Write-Output "INS $DAU/$DAUIP is OFFLINE" | Out-File C:\Scripts\Content\INSChecker\INSProcessCheck.log -append
Else
# Get Process Running State and Send Email
if(!$ProcessIsRunning.Invoke())
Send-MailMessage -To $MailTo -From $MailFrom -SmtpServer $MailServer -Subject $MailSubjectFail -Body $MailBodyFail -Attachments C:\Scripts\Content\INSChecker\Results.csv
else
"Running"
【问题讨论】:
【参考方案1】:希望这能给你一个关于从哪里开始以及如何解决问题的提示,我已经删除了脚本中不相关的部分,只留下了我个人会遵循的逻辑。
$report
的结果应该是一个object[]
(对象数组),应该很容易操作并且很容易导出为 CSV:
@($report).where( $_.SendMail ) | Export-Csv $exportTo -NoTypeInformation
剩下的任务(附加 CSV、发送电子邮件等)留给您自己研究和设计。
$ErrorActionPreference = 'Stop'
# Process Info
$Process = "Notepad"
$ProcessIsRunning =
param($computer, $process)
# On Windows PowerShell -ComputerName is an option,
# this was removed on PS Core
try
$null = Get-Process $process -ComputerName $computer
# If process is running return 'Running'
'Running'
catch
# else return 'Not Running'
'Not Running'
# send a Warning to the console to understand why did this
# fail ( couldn't connect or the process is not running? )
Write-Warning $_.Exception.Message
#Results Info
$ExportTo = "C:\Scripts\Content\INSChecker\Results.csv"
$exportProps = 'Server', 'IP', 'Ping', 'DNSResolution', 'Process', 'SendMail'
# Get DAU Information
$report = foreach($line in Get-Content path/to/file.txt)
$status = [ordered]@ | Select-Object $exportProps
$DAU, $DAUIP = $line = $line.split(",")
$status.SendMail = $false
$status.Server = $DAU
$status.IP = $DAUIP
# Test ICMP Echo Request and DNS Resolution
$ping = Test-Connection $DAUIP -Quiet
$dns = Test-Connection $DAU -Quiet
$status.Ping = ('Failed', 'Success')[$ping]
$status.DNSResolution = ('Failed', 'Success')[$dns]
$status.Process = & $ProcessIsRunning -computer $DAUIP -process $Process
if(-not $ping -or -not $dns -or $status.Process -eq 'Not Running')
$status.SendMail = $true
[pscustomobject]$status
@($report).where( $_.SendMail ) # => This is what should be mailed
【讨论】:
以上是关于从正在运行的进程检查发送带有 CSV 的电子邮件的主要内容,如果未能解决你的问题,请参考以下文章