首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell字典过滤掉ping失败

PowerShell字典过滤掉ping失败
EN

Stack Overflow用户
提问于 2016-06-21 22:15:23
回答 1查看 34关注 0票数 0

我正在尝试过滤status=Offline打印机,并在电子邮件中将输出发送到$Body,您有什么想法可以做到这一点吗?到目前为止,输出显示了所有打印机和IT,而不考虑联机或脱机状态。

代码语言:javascript
复制
    Clear-Host
$printerip = @( 
"192.168.141.104","192.168.141.250","192.168.141.251","192.168.141.53","192.168.141.157","192.168.141.167",
"192.168.141.115","192.168.141.116","192.168.142.25","192.168.142.23","192.168.1 44.80","192.168.144.57","192.168.150.51",
"192.168.150.50","192.168.150.40"
)

$printername = @(
"Shoreline Color", 
"Dockside B/W", 
"Shoreline B/W",
"Spinnaker",
"BA Ricoh Printer",
"BA Xerox Color",
"BA Lab 1",
"BA Lab 2",
"Dowagiac Ricoh",
"Dowagiac Xerox Color",
"Paw Paw Ricoh",
"Paw Paw Xerox Color",
"330 Finance 1",
"330 Finance 2",
"330 Copierr"
)

$status = @()
foreach($printer in $printerip){
if(Test-Connection $printer -Count 2 -Quiet){
$status += "Online"
}else{
$status += "Offline"
}}

$count   = $printerip.Count
$counter = 0
$report  = @()
do{
$report +=$status[$counter]+" - "+$printerip[$counter]+" -     "+$printername[$counter] 
$counter++
}until($counter -ge $count)

if($report -match 'offline'){$body = ("We should probably do something about     this! `n `n") + $report | Out-String
Send-MailMessage -From "PrinterTest@domain.org" -To  "name@domain.org" -Subject "Printer Test" -Body $body  -SmtpServer   "mail.domain.org"}
EN

回答 1

Stack Overflow用户

发布于 2016-06-21 22:26:04

do...until循环中,您需要一条if语句来检查$status[$counter]是否等于' offline‘,以便只捕获脱机打印机,但是,我会尝试这样做,或者尝试将打印机信息放入它自己的单独csv文件中

代码语言:javascript
复制
$printers = @'
IP,Name,Status
192.168.141.104,Shoreline Color,
192.168.141.250,Dockside B/W,
192.168.141.251,Shoreline B/W,
192.168.141.53,Spinnaker,
192.168.141.157,BA Ricoh Printer,
192.168.141.167,BA Xerox Color,
192.168.141.115,BA Lab 1,
192.168.141.116,BA Lab 2,
192.168.142.25,Dowagiac Ricoh,
192.168.142.23,Dowagiac Xerox Color,
192.168.144.80,Paw Paw Ricoh,
192.168.144.57,Paw Paw Xerox Color,
192.168.150.51,330 Finance 1,
192.168.150.50,330 Finance 2,
192.168.150.40,330 Copierr,
'@ | ConvertFrom-Csv

$printers | ForEach-Object {
    if (Test-Connection $_.ip -Count 2 -Quiet) {
        $_.status = 'Online'
    } else {
        $_.status = 'Offline'
    }
}

if ($printers.status -contains 'Offline') {
    $offlineprinters = $printers | Where-Object status -eq 'Offline' | Format-Table -AutoSize
    $body = "We should probably do something about this!`n`n" + $offlineprinters | Out-String
    Send-MailMessage -From 'email@domain.com' -To 'email@domain.com' -Subject 'Printer Test' -Body $body -SmtpServer 'smtp.domain.com'
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37946895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档