【问题标题】:Outputting DHCP script into body of email将 DHCP 脚本输出到电子邮件正文中
【发布时间】:2016-11-16 02:31:55
【问题描述】:

我问了一个与我即将发布的问题类似的问题。我尝试使用该解决方案创建自己的版本,但没有成功。我有在另一个网站上找到的以下脚本,它非常适合我正在寻找的内容,但输出作为电子邮件正文发送而不是输出到文件。我该怎么做呢?另外,如果有人有耐心解释一下,将不胜感激:)。

$a = (netsh dhcp server 172.20.102.1 scope 172.20.104.0 show clients 1)

$lines = @()

foreach ($i in $a){
    if ($i -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"){
        If ($i -match "[0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}"){    
            $lines += $i.Trim()
        }
    }
}
$csvfile = @()

foreach ($l in $lines){
    $Row = "" | select Hostname,IP
    $l = $l -replace '[0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}', ''
    $l = $l -replace ' - ',','
    $l = $l -replace '\s{4,}',''
    $l = $l -replace '--','-'
    $l = $l -replace '-D-','-'
    $l = $l -replace '[-]{1}\d{1,2}[/]\d{1,2}[/]\d{4}',''
    $l = $l -replace '\d{1,2}[:]\d{2}[:]\d{2}',''
    $l = $l -replace 'AM',''
    $l = $l -replace 'PM',''
    $l = $l -replace '\s{1}',''
    $l = $l + "`n"
    $l = $l -replace '[,][-]',','
    $Row.IP = ($l.Split(","))[0]
    #Subnet mask not used, but maybe later
    #$Row.SubNetMask = ($l.Split(","))[1]
    $Row.Hostname = ($l.Split(","))[2]
    $csvfile += $Row
}


$csvfile | sort-object Hostname | Export-Csv "Out_List.csv"


$a = "<style>"
$a = $a + "body {margin: 10px; width: 600px; font-family:arial; font-size: 12px;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color: rgb(179,179,179);align='left';}"
$a = $a + "TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color: white;}"
$a = $a + "</style>"


$html = $csvfile | sort-object Hostname | ConvertTo-HTML | out-string 
Send-MailMessage -SmtpServer "exchange" -Body $html -BodyAsHtml -From "DHCP@.com.au" -To "@.com.au" -Subject "DHCP Report"

提前致谢, 丹尼尔

【问题讨论】:

    标签: email powershell output dhcp


    【解决方案1】:

    作为@Tim 答案的补充:

    您甚至可以像这样将 CSS 添加到转换后的 html 中:

    $style = @"
    <style>
    table {
        width: 50%;
        margin: auto;
        border-collapse: collapse;
    
    }
    
    th, td {
        text-align: left;
        padding: 8px;
        border-bottom: 1px solid #fff;
    }
    
    tr:nth-child(even){background-color: #f2f2f2}
    
    th {
        background-color: #4CAF50;
        color: white;
    }
    </style>
    "@
    $html =import-Csv "Out_List.csv" |ConvertTo-Html -head $style |out-string
    

    【讨论】:

    • 我得到:Send-MailMessage:无法将“System.Object[]”转换为参数“Body”所需的“System.String”类型。不支持指定的方法。我可能已经把它弄坏了 xD。
    • 我在谷歌搜索后尝试添加 |out-string。不幸的是没有工作。不过,对于您的修复,我只是将其粘贴到 html 格式下吗?
    • @rdd2 你能用你修改过的脚本和错误更新问题吗?
    【解决方案2】:

    你可以试试:

    $html = $csvfile | sort-object Hostname | ConvertTo-HTML | out-string
    Send-MailMessage -SmtpServer "mail server" -Body $html -BodyAsHtml -From "noreply@mailserver.com.au" -To "me@mailserver.com" -Subject "DHCP Report"
    

    谢谢,蒂姆。

    【讨论】:

    • 我得到:Send-MailMessage:无法将“System.Object[]”转换为参数“Body”所需的“System.String”类型。不支持指定的方法。我会摆弄一下,看看我能不能让它工作,但我对 PS 很垃圾。
    • $html = $csvfile |排序对象主机名 |转换为 HTML |字符串外
    • PS C:\Windows\system32> $html | gm 类型名称:Microsoft.PowerShell.Commands.HtmlWebResponseObject PS C:\Windows\system32> $html |外串 | gm TypeName: System.String out-string 应该为您将其转换为字符串。
    • 我上面使用的 $html 是 $html = invoke-webrequest -uri www.google.com 这就是它的类型:Microsoft.PowerShell.Commands.HtmlWebResponseObject
    • Ooooh 它工作正常,但同时出现错误 - Get-Member:找不到接受参数“Microsoft.PowerShell.Commands.HtmlWebResponseObject”的位置参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    相关资源
    最近更新 更多