51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

PowerShell 发送美观的Vsphere DataStore警报

豆子今天登陆Vsphere VCenter的时候,无意中发现有DataStore的警报信息,个别DataStore的使用空间超过90%了,需要清空一下SAN Volume的Snapshot。这个是运维常见的问题,那么顺便就用PowerShell写个脚本,定期检查发送邮件好了。

脚本本身很容易,但是我想让他尽量的美观一些。

之前我写过一个博文可以自定义sytle的样式 http://beanxyz.blog.51cto.com/5570417/1786712, 不过现在看起来有些麻烦,还是觉得找找如果有比较好看的现成的css文件可以直接调用就好了。

上网搜了搜 table有哪些现成的css模板,随便找了一个https://codepen.io/anon/pen/vJmLWL,看着还成

下载他的css下来

PowerShell 发送美观的Vsphere DataStore警报_https://www.tiejiang.org_Powershell_第1张

下载的css文件,保存在C:\tmp 目录

@import url(https://fonts.googleapis.com/css?family=Roboto:400,500,700,300,100);
body {
  background-color: #3e94ec;
  font-family: "Roboto", helvetica, arial, sans-serif;
  font-size: 16px;
  font-weight: 400;
  text-rendering: optimizeLegibility;
}
div.table-title {
   display: block;
  margin: auto;
  max-width: 600px;
  padding:5px;
  width: 100%;
}
.table-title h3 {
   color: #fafafa;
   font-size: 30px;
   font-weight: 400;
   font-style:normal;
   font-family: "Roboto", helvetica, arial, sans-serif;
   text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
   text-transform:uppercase;
}
/*** Table Styles **/
.table-fill {
  background: white;
  border-radius:3px;
  border-collapse: collapse;
  height: 200px;
  margin: auto;
  max-width: 600px;
  padding:5px;
  width: 100%;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
  animation: float 5s infinite;
}
th {
color:#D5DDE5;;
background:#1b1e24;
border-bottom:4px solid #9ea7af;
border-right: 1px solid #343a45;
font-size:23px;
font-weight: 100;
padding:24px;
text-align:left;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
vertical-align:middle;
}
th:first-child {
border-top-left-radius:3px;
}
th:last-child {
border-top-right-radius:3px;
border-right:none;
}
tr {
border-top: 1px solid #C1C3D1;
border-bottom-: 1px solid #C1C3D1;
color:#666B85;
font-size:16px;
font-weight:normal;
text-shadow: 0 1px 1px rgba(256, 256, 256, 0.1);
}
tr:hover td {
background:#4E5066;
color:#FFFFFF;
border-top: 1px solid #22262e;
border-bottom: 1px solid #22262e;
}
tr:first-child {
border-top:none;
}
tr:last-child {
border-bottom:none;
}
tr:nth-child(odd) td {
background:#EBEBEB;
}
tr:nth-child(odd):hover td {
background:#4E5066;
}
tr:last-child td:first-child {
border-bottom-left-radius:3px;
}
tr:last-child td:last-child {
border-bottom-right-radius:3px;
}
td {
background:#FFFFFF;
padding:20px;
text-align:left;
vertical-align:middle;
font-weight:300;
font-size:18px;
text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
border-right: 1px solid #C1C3D1;
}
td:last-child {
border-right: 0px;
}
th.text-left {
text-align: left;
}
th.text-center {
text-align: center;
}
th.text-right {
text-align: right;
}
td.text-left {
text-align: left;
}
td.text-center {
text-align: center;
}
td.text-right {
text-align: right;
}

下面是正式的脚本,Set-CellColor也是别人写好的现成的,我直接拿来用了,主要功能是根据条件来更改html文件table的格子的颜色,比如某个值大于警报线就标记为红色等等。脚本中间加载Vsphere SnapIn,查询datastore的状态,最后把结果转换为html,通过Office365发送邮件出去

#修改颜色块的设定
Function Set-CellColor
{   
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory,Position=0)]
        [string]$Property,
        [Parameter(Mandatory,Position=1)]
        [string]$Color,
        [Parameter(Mandatory,ValueFromPipeline)]
        [Object[]]$InputObject,
        [Parameter(Mandatory)]
        [string]$Filter,
        [switch]$Row
    )
``Begin {
    Write-Verbose "$(Get-Date): Function Set-CellColor begins"
    If ($Filter)
    {   If ($Filter.ToUpper().IndexOf($Property.ToUpper()) -ge 0)
        {   $Filter = $Filter.ToUpper().Replace($Property.ToUpper(),"`$Value")
            Try {
                [scriptblock]$Filter = [scriptblock]::Create($Filter)
            }
            Catch {
                Write-Warning "$(Get-Date): ""$Filter"" caused an error, stopping script!"
                Write-Warning $Error[0]
                Exit
            }
        }
        Else
        {   Write-Warning "Could not locate $Property in the Filter, which is required.  Filter: $Filter"
            Exit
        }
    }
}

Process { ForEach ($Line in $InputObject) { If ($Line.IndexOf("<tr><th") -ge 0) { Write-Verbose "$(Get-Date): Processing headers..." $Search = $Line | Select-String -Pattern '<th ?[a-z-:;"=]>(.?)</th>' -AllMatches $Index = 0 ForEach ($Match in $Search.Matches) { If ($Match.Groups[1].Value -eq $Property) { Break } $Index ++ } If ($Index -eq $Search.Matches.Count) { Write-Warning "$(Get-Date): Unable to locate property: $Property in table header" Exit } Write-Verbose "$(Get-Date): $Property column found at index: $Index" } If ($Line -match "<tr( style=""background-color:.+?"")?><td") { $Search = $Line | Select-String -Pattern '<td ?[a-z-:;"=]>(.?)</td>' -AllMatches $Value = $Search.Matches[$Index].Groups[1].Value -as [double] If (-not $Value) { $Value = $Search.Matches[$Index].Groups[1].Value } If (Invoke-Command $Filter) { If ($Row) { Write-Verbose "$(Get-Date): Criteria met! Changing row to $Color..." If ($Line -match "<tr style=""background-color:(.+?)"">") { $Line = $Line -replace "<tr style=""background-color:$($Matches[1])","<tr style=""background-color:$Color" } Else { $Line = $Line.Replace("<tr>","<tr style=""background-color:$Color"">") } } Else { Write-Verbose "$(Get-Date): Criteria met! Changing cell to $Color..." $Line = $Line.Replace($Search.Matches[$Index].Value,"<td style=""background-color:$Color"">$Value</td>") } } } Write-Output $Line } }

End { Write-Verbose "$(Get-Date): Function Set-CellColor completed" } `` } #加载Vsphere function Load-PowerCLI { #pls download and install module fi


赞(0)
未经允许不得转载:工具盒子 » PowerShell 发送美观的Vsphere DataStore警报