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下来

下载的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 first
    Add-PSSnapin VMware.VimAutomation.Core
   # Add-PSSnapin VMware.VimAutomation.Vds
   # Add-PSSnapin VMware.VumAutomation
    . "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
}
#判断是否已经加载SnapIn
$snapins=Get-PSSnapin
if($snapins.name -eq "VMware.VimAutomation.Core")
{
    Write-Host "Vsphere SnapIn is loaded" -ForegroundColor Cyan
}
else{
    Load-PowerCLI
}
#绑定VCenter
Connect-VIServer sydvcs2012

#获取DataStore的数据

$report = @()

foreach($cluster in Get-Cluster){
    Get-VMHost -Location $cluster | Get-Datastore | %{
        $info = "" | select DataCenter, Cluster, Name, Capacity, Free, ‘UsagePercentage(%)‘
        $info.Datacenter = $_.Datacenter
        $info.Cluster = $cluster.Name
        $info.Name = $_.Name 
        $info.Capacity = [math]::Round($_.capacityMB/1024,2) 
        $info.Free="{0:N1}" -f $_.FreeSpaceGB
        $info.‘UsagePercentage(%)‘=[math]::round(100*($_.CapacityGB-$_.FreeSpaceGB)/$_.CapacityGB,2)
        $report += $info
    }
}

#通过Office365发送邮件
$from = "[email protected]"
$to = "[email protected]"
$smtp = "smtp.office365.com" 
$sub = "DataStore list" 
$secpasswd = ConvertTo-SecureString "Password" -AsPlainText -Force 
$mycreds = New-Object System.Management.Automation.PSCredential ($from, $secpasswd)
#指定css模板转换数据为html格式,根据条件指定cell的颜色
$htmlbody=$report| ConvertTo-Html -Body "<H1> DataStore </H1>" -CssUri C:\tmp\table.css | Set-CellColor -Property "UsagePercentage(%)" -Color red -Filter "UsagePercentage(%) -gt 80" 
#发送邮件
Send-MailMessage -To $to -From $from -Subject $sub -Body ($htmlbody|Out-String) -Credential $mycreds -SmtpServer $smtp -DeliveryNotificationOption Never -BodyAsHtml -UseSsl -port 587

我收到的邮件效果

最后添加脚本到task scheduler里面让他每日自动运行就行了

时间: 2024-08-13 17:06:40

PowerShell 发送美观的Vsphere DataStore警报的相关文章

Exchange 2013 PowerShell发送SMTP邮件

作为一个Exchange管理员,我们可能需要使用PowerShell脚本自动化的发送电子邮件.下面一起学习下如何使用PowerShell脚本在你的组织中发送SMTP电子邮件信息. 怎么做到这点呢? PowerShell V2以及后续版本都提供了核心命令可以通过SMTP发送电子邮件信息.使用下面的语法可以发送一份电子邮件信息: Send-MailMessage -To [email protected] ` -From [email protected] ` -Subject "Test E-ma

通过Powershell 发送微信消息

前期需要注册一个微信企业号(度娘可以找到好多帖子)这部分就不再详细说了. 主要通过Powershell 调取微信企业号API来实现发送消息的目的. 下面是一个写好的函数大家可以直接调用: function send-weixin { Param(         [Parameter(Mandatory = $True, Position = 1)]         [String]$Username,         [Parameter(Mandatory = $True, Position

为什么默认配置下嵌套的VMware vSphere/ESXi上安装的虚拟机网络连接失败?

VMware vSphere/ESXi允许嵌套安装.所谓嵌套安装如下图所示,即将vSphere/ESXi虚拟化软件安装在vSphere/ESXi的虚拟机中.而在这样嵌套的vSphere/ESXi虚拟机上还可以部署虚拟机,然而在默认的配置中,这些虚拟机网络连接是失败的.本文将介绍其中的机理以及解决方法. 那么在默认配置下这些嵌套vSphere/ESXi上的虚拟机为什么网络不通呢?这要从vSphere/ESXi的虚拟网络交换机的工作原理说起.该虚拟交换机虽然被称作交换机但是与物理交换机的工作原理还是

MSSQL/WMI/PowerShell结合篇(三)执行PowerShell远程脚本

实时监控的基本原理:WMI Monitor->数据库服务器(内网)->PowerShell->监控服务器(内外网)->发送微信 前面已介绍如何创建WMI Monitor,本文介绍如何执行PowerShell远程脚本,实现将WMI获取到的信息从数据库服务器传到监控服务器,即数据库服务器(内网)->PowerShell->监控服务器(内外网) 一.帐号密码信息加密 设置密钥,并将密钥.帐号.密码加密后信息存放于文本中 1.设置加密密钥 function Set-Key {

信息安全

信息安全本身包括的范围很大,其中包括如何防范商业企业机密泄露.防范青少年对不良信息的浏览.个人信息的泄露等. 网络环境下的信息安全体系是保证信息安全的关键,包括计算机安全操作系统.各种安全协议.安全机制(数字签名.消息认证.数据加密等),直至安全系统,其中任何一个安全漏洞便可以威胁全局安全. 信息安全服务至少应该包括支持信息网络安全服务的基本理论,以及基于新一代信息网络体系结构的网络安全服务体系结构. 概述 信息安全学科可分为狭义安全与广义安全两个层次,狭义的安全是建立在以密码论为基础的计算机安

思科1242 AP无法连接到无线控制器

思科1242 AP 证书有效期为10年,如果证书到期后,首先,把WLC升级成ios 7.4.140或者8.0.120以上版本,其次,在WLC上输入以下命令关闭AP证书的检测功能 config apcert-expiry-ignore {mic|ssc} enable(注意:这个命令只有IOS 7.4.140或者8.0.120以上才有的) 最后,在WLC上设置一下,让AP下载WLC的证书即可 这样AP就可以正常注册到WLC,本人亲测,欢迎转载~ LAP / WLC MIC或SSC寿命到期导致DTL

db2服务端安装图解

一. 准备工作 1. db2服务端安装包,版本:10.1.2 二. 安装图解过程 1. 响应文件是一个包含安装和配置信息的纯英文文本文件.可无需任何用户交互进行db2的批量安装.非必须的. 2. 点击下一步,选择要安装的功能部件和位置 3. 点击下一步,ssh server 为ssh客户端和ssh服务端之间的通讯提供安全通道 4. 点击下一步,db2副本,可在同一台计算机上支持多个版本共存,但是只能安装一个相同发行版级别的副本. 具体地说,可以在同一系统上安装版本 8.版本 9.1 和版本 9.

测试用例设计——场景分析法

转载 测试用例设计——场景分析法 定义 分析软件应用的场景,从用户的角度出发,从场景的角度来设计测试用例,是一种面向用户的测试用例设计方法. 关心用户做什么,而不是关心产品做什么 优点:实用性强,有效,设计出来的用例有价值 缺点:可能使用的场景不一定能对事件系列进行全面的分析,设计出来的用例不完整. 场景分析是通过描述流经用例路径来确定的过程,这个流经过程要从用例开始到结束遍历其中所有基本流 :直黑线表示基本流,是最基本.最简单的路径:(软件功能按照正确的事件流实现的一条正确流程无任何错,程序从

案例分析之系统安全性

案例分析之系统安全性 Table of Contents 1 案例分析之系统安全性 1.1 访问控制和PMI权限管理 1.2 网络安全 1.2.1 信息系统安全威胁 1.2.2 网络层次的安全保障 1.3 橘皮书 1.4 漏洞 1 案例分析之系统安全性 1.1 访问控制和PMI权限管理 访问控制也叫身份认证,基于用户知道什么(口令),拥有什么(私钥和令牌)和是什么(生物 特征)三个要表认证.计算机网络系统中常用的认证有以下三种方式: 口令认证:不安全,容易造成密码泄露和被截获 令牌认证:分为质询