Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.58 KB | None | 0 0
  1. <#
  2. File : Get-SQLDisks.ps1
  3.  
  4. Parameters :
  5. IN : -Config
  6. OUT :
  7.  
  8. Summary :
  9.  
  10. History :
  11. -----------------------------------------------------------------------------------------------------------------------
  12. Version Date Author Description
  13. -----------------------------------------------------------------------------------------------------------------------
  14. v1.00 02/07/18 Gordon Feeney Created
  15. ----------------------------------------------------------------------------------------------------------
  16. Notes :
  17. #>
  18.  
  19. param([string]$config="config.xml")
  20.  
  21. function DeleteFiles ($path, $ext){
  22.  
  23. #Write-Host 'DeleteFiles'
  24.  
  25. #Write-Host 'path ' $path
  26. $ext = '*.' + $ext
  27. #Write-Host 'Ext' $ext
  28.  
  29. #----- get current date ----#
  30. $Now = Get-Date
  31.  
  32. #----- define amount of days ----#
  33. $Days = "30"
  34.  
  35. #----- define folder where files are located ----#
  36. $SourceFolder = $path
  37. #Write-Host 'Folder' $SourceFolder
  38.  
  39. #----- define LastWriteTime parameter based on $Days ---#
  40. $LastWrite = $Now.AddDays(-1 * $Days)
  41. #Write-Host 'Date ' $LastWrite
  42.  
  43. #----- get files based on lastwrite filter and specified folder ---#
  44. $Files = Get-Childitem $SourceFolder -Include $ext -Recurse | Where {$_.LastWriteTime -lt $LastWrite}
  45.  
  46. foreach ($File in $Files)
  47. {
  48. if ($File -ne $NULL)
  49. {
  50. #write-host ("File to delete: " + $File)
  51. #----- delete the file from the archive folder ---#
  52. Remove-Item $File
  53. #write-host("")
  54. }
  55. }
  56.  
  57. }
  58.  
  59.  
  60. function PrintMeOut ($Heading, $StuffToPrint)
  61. {
  62. Write-Host '$Heading - $StuffToPrint'
  63. }
  64.  
  65. function Format-DateTime ($datetime){
  66. if(([DBNull]::Value).Equals($datetime) -Or !($datetime)){
  67. ""
  68. } else {
  69. Get-Date -Date $datetime -Format "dd-MMM-yy HH:mm:ss"
  70. }
  71.  
  72. }
  73.  
  74. function Format-Boolean ($bool){
  75. if($bool){
  76. return "Yes"
  77. } else {
  78. return "No"
  79. }
  80.  
  81. }
  82.  
  83. function Send-Mail ($smtpServer,$smtpPort,$ssl,$user,$pwd,$from,$to,$subject,$subsubject,$body){
  84.  
  85. $SMTPClient = New-Object Net.Mail.SmtpClient($smtpServer,$smtpPort)
  86. $msg = New-Object Net.Mail.MailMessage
  87.  
  88. $msg.From = New-Object Net.Mail.MailAddress($from)
  89. #$msg.To.Add("gordon.feeney@pro-dba.com");
  90. $msg.To.Add($to);
  91. $msg.IsBodyHTML = $true
  92. $msg.Body = $body
  93. $msg.Subject = $subject + " - " + $subsubject
  94.  
  95. if($ssl){
  96. $SMTPClient.EnableSsl = $true
  97. } else {
  98. $SMTPClient.EnableSsl = $false
  99. }
  100.  
  101. if(![string]::IsNullOrEmpty($user)){
  102. $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($user,$pwd);
  103. }
  104.  
  105. $SMTPClient.Send($msg)
  106. }
  107.  
  108. function Get-SQLLocalDateTime ($instance) {
  109.  
  110. $server_date = $null
  111.  
  112. Query-SQL $instance "SELECT GETDATE() AS server_date" | ForEach-Object {
  113. $server_date = $_.server_date
  114. }
  115.  
  116. return $server_date
  117.  
  118. }
  119.  
  120. function Test-WMIConnect ($server){
  121. if($(Get-WmiObject -Class Win32_Service -Computer $server -Filter "Name='RemoteRegistry'" -ErrorAction SilentlyContinue)){
  122. return $true
  123. }
  124. else{
  125. Write-Error -Message "User can't connect to WMI service on $server"
  126. return $false
  127. }
  128. }
  129.  
  130.  
  131. function Get-ServerDiskSpace($server,$config){
  132.  
  133. ## Read in default values
  134. $defaultunittype = $config."default-unit-type"
  135. $defaultunitvalue = $config."default-unit-value"
  136.  
  137. if(([string]::IsNullOrEmpty($defaultunittype )) -or ([string]::IsNullOrEmpty($defaultunitvalue ))){
  138. $defaultunittype = 'PERCENT'
  139. $defaultunitvalue = '20'
  140. }
  141.  
  142. $config = $config.volume | ForEach {
  143.  
  144. New-Object PSObject -Property @{
  145. Name = $_.name
  146. ThresholdUnitType = $_."unit-type"
  147. ThresholdUnitValue = $_."unit-value"
  148. Enabled = [bool]([int]$_.enabled)
  149. Notes = $_.notes
  150. }
  151.  
  152. }
  153.  
  154.  
  155. Get-WmiObject Win32_Volume -Filter "DriveType='3'" -ComputerName $server | ForEach {
  156.  
  157. $name = $_.Name
  158. $label = $_.Label
  159. $freespace = [int64]$_.FreeSpace
  160. $capacity = [int64]$_.Capacity
  161. $freespacethreshold = [System.Nullable``1[[System.Int64]]] $null
  162. $enabled = $true
  163. $notes = $null
  164.  
  165. #Populate the drive object with default values initially in case there isn't a config item for some of them
  166. $ThisThresholdUnitType = $defaultunittype
  167. $thisunitvalue = $defaultunitvalue
  168.  
  169. if($ThisThresholdUnitType -eq "MB"){
  170. $freespacethreshold = ([int64]$thisunitvalue)*1024*1024
  171. }
  172. elseif($ThisThresholdUnitType -eq "GB"){
  173. $freespacethreshold = ([int64]$thisunitvalue)*1024*1024*1024
  174. }
  175. elseif($ThisThresholdUnitType -eq "PERCENT"){
  176. $freespacethreshold = ([int64]$capacity)*([int64]$thisunitvalue/100)
  177. }
  178.  
  179. $config | Where-Object {$_.Name -eq $name} | ForEach {
  180.  
  181. ## Modified on 13/10/17 by G Feeney
  182. ## Over-ride defaults with volumne-specific values
  183.  
  184. if($_.ThresholdUnitType){
  185. $ThisThresholdUnitType = $_.ThresholdUnitType
  186. $thisunitvalue = [int64]$_.ThresholdUnitValue
  187. }
  188. else{
  189. $ThisThresholdUnitType = $defaultunittype
  190. $thisunitvalue = $defaultunitvalue
  191. }
  192.  
  193. if($ThisThresholdUnitType -eq "MB"){
  194. $freespacethreshold = ([int64]$thisunitvalue)*1024*1024
  195. }
  196. elseif($ThisThresholdUnitType -eq "GB"){
  197. $freespacethreshold = ([int64]$thisunitvalue)*1024*1024*1024
  198. }
  199. elseif($ThisThresholdUnitType -eq "PERCENT"){
  200. $freespacethreshold = ([int64]$capacity)*([int64]$thisunitvalue/100)
  201. }
  202.  
  203. if(![string]::IsNullOrEmpty($_.Enabled) -And $_.Enabled){
  204. $enabled = [bool]1
  205. } elseif (![string]::IsNullOrEmpty($_.Enabled) -And !($_.Enabled)){
  206. $enabled = [bool]0
  207. }
  208.  
  209. $notes = $_.Notes
  210.  
  211. }
  212.  
  213. <#
  214. Write-Host "Server: $server"
  215. Write-Host "Name: $name"
  216. Write-Host "Label: $label"
  217. Write-Host "Enabled: $enabled"
  218. Write-Host "FreeSpace: $freespace"
  219. Write-Host "ThresholdSpace: $freespacethreshold"
  220. #>
  221.  
  222. if(($freespace -lt $freespacethreshold -Or !($freespacethreshold)) -And (($enabled) -Or [string]::IsNullOrEmpty($enabled))){
  223. New-Object PSObject -Property @{
  224. Name = $name
  225. Label = $label
  226. FreeSpace = $freespace
  227. ThresholdSpace = $freespacethreshold
  228. Capacity = $capacity
  229. Enabled = $enabled
  230. Notes = $notes
  231. } | Select-Object `
  232. Name `
  233. ,Label `
  234. ,Enabled `
  235. ,Notes `
  236. ,@{Name="FreeSpacePercent";Expression={([Math]::Round(($_.FreeSpace/$_.Capacity)*100,2))}} `
  237. ,@{Name="FreeSpaceGB";Expression={([Math]::Round($_.FreeSpace/1GB,2))}} `
  238. ,@{Name="ThresholdSpaceGB";Expression=
  239. {
  240. if(($_.ThresholdSpace)){([Math]::Round($_.ThresholdSpace/1GB,2))}
  241. }
  242. } `
  243. ,@{Name="CapacityGB";Expression={([Math]::Round($_.Capacity/1GB,2))}} `
  244. ,@{Name="Alert";Expression=
  245. {
  246. if(($_.FreeSpace -lt $_.ThresholdSpace -Or !($_.ThresholdSpace)) -And (($_.Enabled) -Or [string]::IsNullOrEmpty($_.Enabled))){
  247. [bool]1
  248. } else {
  249. [bool]0
  250. }
  251.  
  252. }
  253. } #End of New-Object | Select-Object
  254. }
  255.  
  256.  
  257. }#End of Get-WmiObject
  258. }
  259.  
  260. function FormatHTML-ServerDiskSpace($obj){
  261.  
  262. $notesCounter = [int]0
  263. $notes = @()
  264.  
  265. $obj | Where-Object {!($_.Notes -eq $null) } | ForEach {
  266.  
  267. $notesCounter += 1
  268.  
  269. $note = New-Object PSObject -Property @{
  270. ID = $notesCounter
  271. Name = $_.Name
  272. Note = $_.Notes
  273. }
  274.  
  275. $notes += $note
  276.  
  277. }
  278.  
  279. $html = "
  280. <table class='summary'>
  281. <tr>
  282. <th>Name</th>
  283. <th>Label</th>
  284. <th>Free Space(%)</th>
  285. <th>Free Space(GB)</th>
  286. <th>Threshold(GB)</th>
  287. <th>Capacity(GB)</th>
  288. </tr>
  289. "
  290.  
  291. $obj | ForEach {
  292.  
  293. $name = $_.Name
  294.  
  295. if($_.Alert){
  296. $html += "<tr class='warning'>"
  297. } else {
  298. $html += "<tr>"
  299. }
  300.  
  301. $note = $notes | Where-Object {$_.Name -eq $name }
  302.  
  303. $html += "
  304. <td"+$(if(!($_.Enabled) -And !([string]::IsNullOrEmpty($_.Enabled)) ){" class='disabled'"})+">"+$_.Name+$(if(!($note -eq $null)){"<sup> "+$note.ID+"</sup>"})+"</td>
  305. <td"+$(if(!($_.Enabled) -And !([string]::IsNullOrEmpty($_.Enabled)) ){" class='disabled'"})+">"+$_.Label+"</td>
  306. <td"+$(if(!($_.Enabled) -And !([string]::IsNullOrEmpty($_.Enabled)) ){" class='disabled'"})+">"+$_.FreeSpacePercent+"</td>
  307. <td"+$(if(!($_.Enabled) -And !([string]::IsNullOrEmpty($_.Enabled)) ){" class='disabled'"})+">"+$_.FreeSpaceGB+"</td>
  308. <td"+$(if(!($_.Enabled) -And !([string]::IsNullOrEmpty($_.Enabled)) ){" class='disabled'"})+">"+$_.ThresholdSpaceGB+"</td>
  309. <td"+$(if(!($_.Enabled) -And !([string]::IsNullOrEmpty($_.Enabled)) ){" class='disabled'"})+">"+$_.CapacityGB+"</td>
  310. "
  311.  
  312. $html += "</tr>"
  313. }
  314.  
  315. $html += "</table>"
  316.  
  317. if($notes.Count -gt 0){
  318.  
  319. $html += "<table class='disabled'>"
  320.  
  321. $notes | ForEach{
  322.  
  323. $html += "<tr>"
  324.  
  325. $html+="<td><sup>"+$_.ID+"</sup> "+$_.Note+"</td>"
  326.  
  327. $html += "</tr>"
  328.  
  329. }
  330.  
  331. $html += "</table>"
  332.  
  333. }
  334.  
  335. return $html
  336. }
  337.  
  338. function FormatHTML-ServerDiskEmailSubjects($obj){
  339.  
  340. $drivealert = ""
  341. #Write-Host "Subect: $subject"
  342.  
  343. $obj | ForEach {
  344.  
  345. #$drivealert += "`r`n"
  346. if ($drivealert -ne ""){
  347. $drivealert += "; "
  348. }
  349.  
  350. $name = $_.Name
  351.  
  352. if($_.Alert){
  353. #Write-Host "Name: $name"
  354. #Write-Host "Label: "$_.Label
  355. #Write-Host "FreeSpacePercent: "$_.FreeSpacePercent
  356. #Write-Host "FreeSpaceGB: "$_.FreeSpaceGB
  357. #Write-Host "ThresholdSpaceGB: "$_.ThresholdSpaceGB
  358. #Write-Host "CapacityGB: "$_.CapacityGB
  359. $drivealert += $_.Name + " (" + $_.FreeSpaceGB + "GB/" + $_.CapacityGB + "GB, " + $_.FreeSpacePercent + "%)"
  360. #Write-Host $drivealert
  361. }
  362. #Write-Host ""
  363. }
  364.  
  365. return $drivealert
  366. }
  367.  
  368. function FormatHTML-ReportHeader($hostName,$username,$start,$end,$version,$to,$cc,$body,$server){
  369.  
  370. $html = "
  371. <table class='summary'>
  372. <tr>
  373. <th>Property</th>
  374. <th>Value</th>
  375. </tr>
  376. <tr>
  377. <td>Host</td>
  378. <td>$hostName</td>
  379. </tr>
  380. <tr>
  381. <td>User</td>
  382. <td>$username</td>
  383. </tr>
  384. <tr>
  385. <td>Start</td>
  386. <td>$(Format-DateTime $start)</td>
  387. </tr>
  388. <tr>
  389. <td>End</td>
  390. <td>$(Format-DateTime $end)</td>
  391. </tr>
  392. <tr>
  393. <td>Commit</td>
  394. <td>"+$version.Commit+"</td>
  395. </tr>
  396. <tr>
  397. <td>Tag</td>
  398. <td>"+$version.Tag+"</td>
  399. </tr>
  400. <tr>
  401. <td>Contact</td>
  402. <td><a href='mailto:$to&cc=$cc&subject=SQL Disk Status Alert - $server&body=$body'>Report Issue</a></td>
  403. </tr>
  404. </table>
  405. "
  406.  
  407. return $html
  408.  
  409. }
  410.  
  411. function FormatHTML-ReportSummary($summary){
  412.  
  413. $html = "
  414. <table class='summary'>
  415. <tr>
  416. <th>Name</th>
  417. <th>Type</th>
  418. <th>Category</th>
  419. <th>Errors</th>
  420. </tr>
  421. "+$($summary | ForEach{
  422.  
  423. if ($_.Errors -gt 0){
  424. "<tr"+$(if($_.Alert){" class='warning'"})+">
  425. <td>"+$_.Name+"</td>
  426. <td>"+$_.Type+"</td>
  427. <td>"+$_.Category+"</td>
  428. <td>"+$(
  429. if(($_.Errors -eq 0)){
  430. "OK"
  431. } else {
  432. if ([string]::IsNullOrEmpty($_.Errors)){
  433. "-"
  434. }
  435. else {
  436. $_.Errors
  437. }
  438. }
  439. )+"</td>
  440. </tr>"
  441. }
  442.  
  443. })+"</table>"
  444.  
  445. return $html
  446.  
  447. }
  448.  
  449. function Get-SQLDisks ($directory, $config){
  450.  
  451. $executingScriptDirectory = $directory
  452.  
  453. $user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
  454. $FQhost = (Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
  455.  
  456. $reportVersion = Import-Csv -Delimiter "|" -Path "$executingScriptDirectory\version.rel"
  457.  
  458. $css = Get-Content "$executingScriptDirectory\configuration\main.css"
  459. [xml]$xml = Get-Content "$executingScriptDirectory\configuration\$config"
  460.  
  461. $client = $xml.configuration.metadata.client
  462. $subject = $xml.configuration.metadata.subject
  463. $clientcontact = $xml.configuration.metadata."client-contact"
  464. $clientcontactcc = $xml.configuration.metadata."client-contact-cc"
  465. $body = $xml.configuration.metadata.body
  466. $smtpServer = $xml.configuration.smtp.server
  467. $smtpPort = $xml.configuration.smtp.port
  468. $ssl = $(if($xml.configuration.smtp.ssl -eq 1){$true} else {$false})
  469. $smtpUser = $xml.configuration.smtp.user
  470. $smtpPassword = $xml.configuration.smtp.password
  471. $mailFrom = $xml.configuration.smtp.from
  472. $mailTo = $xml.configuration.smtp.to
  473.  
  474. $xml.configuration.server | ForEach {
  475.  
  476. # Get Data
  477. $server = $_.name
  478. #Write-host "HERE 1"
  479. #Write-Host "Server: $server"
  480. $server_connect = $_.connect
  481.  
  482. if([string]::IsNullOrEmpty($_.connect)){
  483. $server_connect = $server
  484. }
  485.  
  486. New-Variable -Name "$($server)_start" -Value $(Get-Date)
  487.  
  488. # Check WMI Connectivity
  489.  
  490. New-Variable -Name "$($server)_wmiconnect" -Value $(Test-WMIConnect $server_connect)
  491.  
  492. if($(Get-Variable -Name "$($server)_wmiconnect" -ValueOnly)) {
  493.  
  494. # Get Data
  495.  
  496. New-Variable -Name "$($server)_diskSpace" -Value $(Get-ServerDiskSpace $server_connect $_.volumes)
  497.  
  498. # Errors
  499.  
  500. New-Variable -Name "$($server)_diskSpace_errors" -Value $(@($(Get-Variable -Name "$($server)_diskSpace" -ValueOnly) | Where-Object {$_.Alert}).Count)
  501. }
  502. }
  503.  
  504. #Write-Host "smtpServer $smtpServer"
  505. #Write-Host "mailTo $mailTo"
  506.  
  507. $xml.configuration.server | ForEach {
  508.  
  509. $server = $_.name
  510. $Errors = $(if($(Get-Variable -Name "$($server)_wmiconnect" -ValueOnly)){
  511. $(Get-Variable -Name "$($server)_diskSpace_errors" -ValueOnly)
  512. } else { 0 })
  513.  
  514. if ($Errors -gt 0){
  515. $server = $_.name
  516. Write-Host "HERE #1"
  517. Write-Host "server $server "
  518.  
  519. $disks = $(Get-Variable -Name "$($server)_diskSpace" -ValueOnly)
  520. $disks | foreach{
  521.  
  522. $summary = @()
  523.  
  524. $html = "
  525. <html>
  526. <head>
  527. <style>"+$css+"</style>
  528. </head>
  529. <body>
  530. <h1>SQL Disk Status Check</h1>
  531. "
  532.  
  533. #$html += FormatHTML-ReportHeader $FQhost $user $(Get-Variable -Name "$($server)_start" -ValueOnly) $(Get-Variable -Name "$($server)_end" -ValueOnly) $reportVersion $clientcontact $clientcontactcc $body $server
  534.  
  535. $summary += New-Object PSObject -Property @{
  536. Name = $server
  537. Type = "Host"
  538. Category = "WMI"
  539. Errors = $(if($(Get-Variable -Name "$($server)_wmiconnect" -ValueOnly)){
  540.  
  541. 0
  542.  
  543. } else { 1 })
  544. }
  545.  
  546. $summary += New-Object PSObject -Property @{
  547. Name = $server
  548. Type = "Host"
  549. Category = "Disk"
  550. Errors = $(if($(Get-Variable -Name "$($server)_wmiconnect" -ValueOnly)){
  551.  
  552. $(Get-Variable -Name "$($server)_diskSpace_errors" -ValueOnly)
  553.  
  554. } else { $null })
  555. }
  556.  
  557.  
  558. $html += "<h2>Report Summary</h2><br>"
  559.  
  560. $html += FormatHTML-ReportSummary $($summary | Select-Object `
  561. Name `
  562. ,Type `
  563. ,Category `
  564. ,Errors `
  565. ,@{Name="Alert";Expression=
  566.  
  567. {
  568.  
  569. if($_.Errors -gt 0){
  570. $true
  571. } else {
  572. $false
  573. }
  574.  
  575.  
  576. }
  577.  
  578. })
  579.  
  580.  
  581. if($(Get-Variable -Name "$($server)_wmiconnect" -ValueOnly)) {
  582.  
  583. #$html += "<h2>$server - Host</h2><br>"
  584.  
  585. $html += "<h3>Volume Summary</h3>"
  586.  
  587. $serverdisk = $(Get-Variable -Name "$($server)_diskSpace" -ValueOnly) | Where-Object {$_.Name -eq $Volume}
  588. $html +=FormatHTML-ServerDiskSpace $serverdisk
  589.  
  590. }
  591.  
  592. $html += "</body></html>"
  593.  
  594. $end = Get-Date
  595.  
  596. $html | Out-File "$executingScriptDirectory\output\$($server)_$(Get-Date -format ""yyyyMMddHHmm"").html"
  597.  
  598. $subsubject = FormatHTML-ServerDiskEmailSubjects $serverdisk
  599.  
  600. Send-Mail $smtpServer $smtpPort $ssl $smtpUser $smtpPassword $mailFrom $mailTo $("$subject - $client - $server") $subsubject $html
  601. }
  602. }
  603. }
  604.  
  605. }
  606.  
  607. $directory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
  608. Get-SQLDisks $directory $config 2> "$directory\log\$(Get-Date -format "yyyyMMddHHmmssff").log"
  609.  
  610. DeleteFiles "$directory\log" "log"
  611. DeleteFiles "$directory\output" "html"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement