【问题标题】:PowerShell Dictonary filtering out the failing pingPowerShell 字典过滤掉失败的 ping
【发布时间】:2016-06-21 14:15:23
【问题描述】:

我正在尝试过滤掉 status=Offline Printers 并将该输出发送到电子邮件中的 $Body,关于如何执行此操作的任何想法?到目前为止,无论在线还是离线状态,输出都会显示所有打印机和 IT。

    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"}

【问题讨论】:

    标签: powershell dictionary output


    【解决方案1】:

    在您的 do...until 循环中,您需要一个 if 语句来检查 $status[$counter] 是否等于“离线”以仅获取离线打印机,但是,我会尝试这样的事情或尝试将打印机信息放入它自己单独的 csv 文件

    $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'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 2021-07-05
      • 1970-01-01
      相关资源
      最近更新 更多