When the beans landed on vsphere vcenter today, they stumbled upon a datastore alert message that the individual datastore used more than 90% of the space and needed to clear the snapshot of the San volume. This is a common problem with operations, so by the way to write a script with PowerShell, regular check to send the mail OK.
The script itself is easy, but I want him to be as beautiful as possible.
Before I wrote a blog post can customize the style of Sytle http://beanxyz.blog.51cto.com/5570417/1786712, but now looks a bit troublesome, Still feel like looking for a better-looking CSS file can be called directly.
Search the Internet to search the table there are any ready-made CSS templates, casually find a https://codepen.io/anon/pen/vJmLWL, look at the
Download his CSS down
650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M00/9E/0C/wKiom1mKniWQ91A9AACDNAfVm2M863.jpg "title=" a.jpg "alt=" Wkiom1mkniwq91a9aacdnafvm2m863.jpg "/>
Download the CSS file, saved in the C:\tmp directory
@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;}
The following is a formal script, Set-cellcolor is also written by others ready-made, I directly used, the main function is to change the HTML file according to the conditions of the table grid color, such as a value greater than the alarm line is marked red and so on. Load Vsphere Snapin In the middle of the script, query the state of Datastore, and finally convert the result to HTML, send the message out by 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 &NBsp;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): &NBSP;CRITERIA&NBSP;MET!&NBSP;&NBSp 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 "&NBSP;&NBSP;&NBSP;&NBSP;}} #加载Vspherefunction 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-pssnapinif ($snapins .name -eq "VMware.VimAutomation.Core") { write-host "vsphere snapin is loaded" -ForegroundColor Cyan}else{ LOAD-POWERCLI} #绑定VCenterConnect-viserver sydvcs2012# gets the data for 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格式, specify the color of the cell according to the criteria $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
e-mail Effects I received
650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M00/9E/0C/wKiom1mKpNexRLYAAAHHhOOGEME536.jpg "title=" b.jpg "alt=" Wkiom1mkpnexrlyaaahhhoogeme536.jpg "/>
Finally, add the script to the Task Scheduler and let him run it automatically every day.
650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M01/9E/0C/wKioL1mKpwuTi-p7AAEhEOccS8g997.jpg "title=" c.jpg "alt=" Wkiol1mkpwuti-p7aaeheoccs8g997.jpg "/>
This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1954755
PowerShell sends beautiful vsphere datastore alerts