如何创建一个批处理文件来告诉我正在使用的内存量?

时间:2019-05-13 13:23:14

标签: powershell batch-file cmd

我想创建一个批处理文件,该文件为我提供了所需文件的内存。例如,Chrome.exe。如果所需内存超过1GB,请给我发送电子邮件。

要检查的应用程序在终端服务器上,并且需要越来越多的内存。它应该发送给我一封电子邮件,告诉我该应用程序是否需要超过1GB的内存。

作为信息,该批处理文件将在终端服务器上运行。每隔10分钟由任务计划开始。

到目前为止,我已经尝试过了。

 powershell.exe -command send-mailmessage -to test@example.de -cc test.e@example.de 
-from test@example.de -subject '"Batch-Testmail"' 
-smtpserver smtp.testserver.de -body '"Testmail"'

那只是一个例子。

并检查我的程序需要多少工作区。例如Chrome浏览器

dir C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

4 个答案:

答案 0 :(得分:2)

如果您要查询正在运行的进程使用的内存量,则可以使用以下命令开始体验:

wmic process where name='chrome.exe' get WorkingSetSize

它将把您打开的每个Chrome标签所使用的内存量转储到屏幕上。

希望有帮助。 :)

我从这里取样:

https://superuser.com/questions/1128692/extract-used-ram-of-program-by-batch

然后,使用您提供的有关电子邮件的详细信息,您可以执行以下操作:

$app_loaded=wmic process where "workingsetsize>=1000000000" get name 
$EmailFrom = “yourgmailadress@gmail.com”
$EmailTo = “destination@somedomain.com”
$Subject = “The subject of your email”
$Body = “The application”.$app_loaded." is using more memory than expected."
$SMTPServer = “smtp.gmail.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“usr”, “pass”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

亲切的问候。

答案 1 :(得分:0)

尝试一下?

@echo off
setlocal ENABLEDELAYEDEXPANSION
set CHROME_MEM=0
for /F "tokens=5" %%a in ('tasklist ^| find "chrome"') do (set MEM=%%a
 set /A CHROME_MEM=!CHROME_MEM! + !MEM:,=!
)
echo Total Memory is %CHROME_MEM% K

答案 2 :(得分:0)

我还认为PowerShell解决方案更好。 WMI很不错,我们也可以使用Get-ProcessGroup-Object,例如

$mem = get-process | Group-Object -Property Chrome.exe | 
    Format-Table @{n='Mem';e={'{0:N0}' -f (($_.Group|Measure-Object WorkingSet -Sum).Sum / 1MB)};a='right'}  -HideTableHeaders -AutoSize | Out-String

if($mem.Trim()/1 -gt 1024) {
    $EmailFrom = "noreply@do.too"
    $EmailTo = "you@do.too" 
    $Subject = "Memory Warning: $($mem.Trim()) MB accumulated" 
    $Body = "this is a notification from server x" 
    $SMTPServer = "smtp.gmail.com" 
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
    $SMTPClient.EnableSsl = $true 
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($username, $credentials); 
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
}

答案 3 :(得分:0)

解决方案是:

$mem = get-process | Group-Object -Property chrome.exe | 
Format-Table @{n='Mem';e={'{0:N0}' -f (($_.Group|Measure-Object WorkingSet -Sum).Sum / 1MB)};a='right'}  -HideTableHeaders -AutoSize | Out-String

if($mem.Trim()/1 -gt 1024) {
   command send-mailmessage -to example@mail.com -cc examplecc@mail.com -from noreply@mail.com -subject '"Memory Warning: 1 GB accumulated"' -smtpserver smtp.server.com -body '"this is a notification from server x"' -priority High
}