#将需要读取的域名和端口列表保存在名为ports01.txt、ports02的文件中,文件与脚本位于相同目录下
$CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.LastIndexOf('\')+1)
$file = "ports01.txt"
$FilePath = $CurrentPath + $file

$Infors = Get-Content $FilePath
$Temp_PortStatushtm = "C:\Windows\Temp\PortStatushtm.htm"

If (Test-Path $Temp_PortStatushtm)
{Remove-Item $Temp_PortStatushtm}

$Domainname = $Infors[0].Split(",")[0]
#将域名解析为IP,因为可能会解析出多个IP,此处只取第一个IP
$IP = [System.Net.Dns]::GetHostAddresses($Domainname)[0]
Start-Sleep -Milliseconds 500
#判断IP是否格式正确
$Address = [System.Net.IPAddress]::Parse($IP)
$IPstring = $Address.IPAddressToString

#端口连通性测试函数
Function Port-Test ($Address,$Port)
{
$Timeout = 1000
$Socket = New-Object System.Net.Sockets.TCPClient
$Connect = $Socket.BeginConnect($Address,$Port,$null,$null)
Start-Sleep -Milliseconds 500
If ( $Connect.IsCompleted )
{
$Wait = $Connect.AsyncWaitHandle.WaitOne($TimeOut,$false)
If(!$Wait)
{
$Socket.Close()
Return $false
}
Else{
$Socket.EndConnect($Connect)
$Socket.Close()
Return $true
}
}
Else{Return $false}

}

$Server = $Domainname + " : " + $IPstring
#将结果临时保存到名为csv的hash表中,便于html格式的输出
$csv=@()
Foreach ($infor in $Infors)
{
$port = $Infor.Split(",")[1]
$Status= Port-Test $Address $port
$content=""|select Port,Status
$content.Port = $port
$content.Status = $status
$csv += $content
}

#输出HTML格式:
#-head:控制整体样式
#{$_ -replace "<table>","<table align=center>"}:表格居中显示
#$_ -replace "<td>","<td bgcolor=cyan>"}:改变表格列背景颜色
$csv|Select Port,Status | ConvertTo-Html `
-head '<style type="text/css"> body,table,td,th {font-family:Tahoma; color:Black; Font-Size:20pt} body {text-align:center} th {font-weight:bold}</style>' `
-Title "Port Monitor" `
-Body "<H4>$server</H4>" | `
ForEach{$_ -replace "<table>","<table align=center>"} |`
ForEach{$_ -replace "<td>","<td bgcolor=cyan>"} | `
Foreach{
If($_ -like "*false</td>*")
{$_ -replace "<td bgcolor=cyan>false","<td bgcolor=red>False"}
#Elseif($_ -like "*true</td>*")
# {$_ -replace "<td bgcolor=cyan>true","<td bgcolor=green>True"}
Else{$_}
} >>$Temp_PortStatushtm

#Invoke-Item $Temp_PortStatushtm
##############################################################################

$file = "ports02.txt"
$FilePath = $CurrentPath + $file

$Infors = Get-Content $FilePath
$Temp_PortStatushtm = "C:\Windows\Temp\PortStatushtm.htm"

$Domainname = $Infors[0].Split(",")[0]
$IP = [System.Net.Dns]::GetHostAddresses($Domainname)[0]
Start-Sleep -Milliseconds 500
$Address = [System.Net.IPAddress]::Parse($IP)
$IPstring = $Address.IPAddressToString

$t = " √ "
$f = " × "
Function Port-Test ($Address,$Port)
{
$Timeout = 1000
$Socket = New-Object System.Net.Sockets.TCPClient
$Connect = $Socket.BeginConnect($Address,$Port,$null,$null)
Start-Sleep -Milliseconds 500
If ( $Connect.IsCompleted )
{
$Wait = $Connect.AsyncWaitHandle.WaitOne($TimeOut,$false)
If(!$Wait)
{
$Socket.Close()
Return $false
}
Else{
$Socket.EndConnect($Connect)
$Socket.Close()
Return $true
}
}
Else{Return $false}

}

$Server = $Domainname + " : " + $IPstring
$csv=@()
Foreach ($infor in $Infors)
{
$port = $Infor.Split(",")[1]
$Status= Port-Test $Address $port
$content=""|select Port,Status
$content.Port = $port
$content.Status = $status
$csv += $content
}
$csv|Select Port,Status | ConvertTo-Html `
-head '<style type="text/css"> body,table,td,th {font-family:Tahoma; color:Black; Font-Size:20pt} body {text-align:center} th {font-weight:bold}</style>' `
-Title "Port Monitor" `
-Body "<H4>$server</H4>" | `
ForEach{$_ -replace "<table>","<table align=center>"} |`
ForEach{$_ -replace "<td>","<td bgcolor=cyan>"} | `
Foreach{
If($_ -like "*false</td>*")
{$_ -replace "<td bgcolor=cyan>false","<td bgcolor=red>False"}
Else{$_}
} >>$Temp_PortStatushtm

#打开html输出结果
Invoke-Item $Temp_PortStatushtm

参考:http://www.pstips.net/creating-colorful-html-reports.html

====================================

导出html格式文件:

Get-Website|Select @{Label="Sitename";Expression={$_.Name}}, @{Label="Logdir";Expression={"W3SVC" + $_.id}} |sort Sitename |ConvertTo-Html |out-file d:\iislog\Readme.htm -Force

导出html格式文件,包含超链接:

Add-Type -AssemblyName System.Web
$ip = (gwmi Win32_NetworkAdapterConfiguration -Filter "IPEnabled='true'" | ? {$_.DefaultIPGateway -ne $null}).IPAddress[0]
$html = Get-Website|Select @{Label="Website";Expression={$_.Name}}, @{Label="Website Log";Expression={'<a href="http://' + $ip + ":11000/W3SVC" + $_.id + '">' + "http://" + $ip + ":11000/W3SVC" + $_.id + '</a>'}} |sort Website |ConvertTo-Html
$html = $html | % { if($_ -match 'a href' ) { [System.Web.HttpUtility]::HtmlDecode($_) } else { $_ } }
$html |out-file d:\iislog\Readme.htm -Force

HTML输出 一 控制列背景颜色的更多相关文章

  1. HTML输出 二 控制行背景颜色

    $Infors = Get-Content ports01.txt$Temp_PortStatustxt = "C:\Windows\Temp\PortStatustxt.txt" ...

  2. VC编程中如何设置对话框的背景颜色和静态文本颜色

    晚上编一个小程序,涉及到如何设置对话框的背景颜色和静态文本颜色.这在VC6.0中本来是一句话就搞定的事.在应用程序类中的InitInstance()函数添加: //设置对话框背景和文本颜色 SetDi ...

  3. 第十三篇-通过Button设置文本背景颜色

    MainActivity.java package com.example.aimee.buttontest; import android.annotation.SuppressLint; impo ...

  4. [转]如何在Windows 10中更改文件夹背景颜色

    ini文件.我们甚至可以使用相同的技术将图片设置为文件夹背景. 已有工具可以更改Windows 7中Windows资源管理器背景的颜色,并将图像设置为Windows 7中的文件夹背景,但这些工具与Wi ...

  5. []如何在Windows 10中更改文件夹背景颜色

    ini文件.我们甚至可以使用相同的技术将图片设置为文件夹背景. 已有工具可以更改Windows 7中Windows资源管理器背景的颜色,并将图像设置为Windows 7中的文件夹背景,但这些工具与Wi ...

  6. qt tableview列头背景颜色设置

    设置表列头背景颜色 QHeaderView::section { background: rgb(255, 255, 127); }

  7. Silverlight DataGrid数据行背景颜色控制

    sdk:DataGrid数据绑定后,部分特殊的行需要用不同的背景颜色来显示.(注册DataGrid的LoadingRow事件) private void radGridView_LoadingRow( ...

  8. ext js 4.0 grid表格根据列值的不同给行设置不同的背景颜色

    Code: Ext.create('Ext.grid.Panel', { ... viewConfig: { getRowClass: function(record) { return record ...

  9. linux BASH shell设置字体与背景颜色

    linux BASH shell下设置字体及背景颜色的方法. BASH shell下设置字体及背景颜色  echo -e "\e[31mtest\e[41m"  \e[30m 将字 ...

随机推荐

  1. noip2002提高组题解

    再次280滚粗.今天早上有点事情,所以做题的时候一直心不在焉,应该是三天以来状态最差的一次,所以这个分数也还算满意了.状态真的太重要了. 第一题:均分纸牌 贪心.(昨天看BYVoid的noip2001 ...

  2. Android中ListView嵌套进ScrollView时高度很小的解决方案

    package com.example.test.util; import android.view.View; import android.view.ViewGroup; import andro ...

  3. 【转】linux : waitpid函数

    原文网址:http://blog.csdn.net/jifengszf/article/details/3067841 [waitpid系统调用]       功能描述: 等待进程改变其状态.所有下面 ...

  4. 获取某月第一天,最后一天的sql server脚本 【转】http://blog.csdn.net/chaoowang/article/details/9167969

    这是计算一个月第一天的SQL 脚本:    SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一天 SELECT DATEADD(mm, DA ...

  5. [转载] python+Eclipse+pydev环境搭建

    转自:http://www.cnblogs.com/Bonker/p/3584707.html 编辑器:Python 自带的 IDLE 简单快捷, 学习Python或者编写小型软件的时候.非常有用. ...

  6. DevExpress licenses.licx 的解决方法 z

    在 使用DevExpress控件的时候.每次对窗体进行更改的时候,都会出现一个对话框.发布的时候 也会出现一个对话框.之前的解决方法是在发布的时候把licenses.licx给删除掉,但是这个方法治标 ...

  7. C# winform 若要在加载设计器前避免可能发生的数据丢失,必须纠正以下错误

    winform中有时添加了新控件之后编译会报错: 若要在加载设计器前避免可能发生的数据丢失,必须纠正以下错误,如图: 解决方案: 1.“解决方案”→“批生成”→“清理”→“确定”: 2.“解决方案”→ ...

  8. Delphi RichEx 图像

    unit RichEx; {2005-03-04 LiChengbinAdded:Insert bitmap or gif into RichEdit controls from source fil ...

  9. 【剑指offer 面试题12】打印1到最大的n位数

    思路: 用n位字符数组表示n位数,通过递归的方式逐层(位)遍历,递归终止时打印. #include "stdio.h" #include "string.h" ...

  10. [Tommas] 测试用例覆盖率(三)

    三.测试数据的设计 每一个测试思路最终都要转化成具体的数据才能来执行.关于测试数据设计的方法也不外乎那几种,就不再赘述了.此处单就一些经常易犯的错误,提出一些注意点,作为用例数据设计时的参考: 1.尽 ...