Last active
July 8, 2016 10:08
-
-
Save janegilring/6c67cd9eebc37571d2e2c4b61099ade9 to your computer and use it in GitHub Desktop.
This is a demo script intended to be imported as a runbook in Azure Automation to demonstrate the alerts integration between Microsoft Operations Management Suite (OMS) and Azure Automation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| NAME: Invoke-OMSAlertDiskCleanup.ps1 | |
| AUTHOR: Jan Egil Ring (@JanEgilRing) | |
| COMMENT: This is a demo script intended to be imported as a runbook in Azure Automation to demonstrate the alerts integration | |
| between Microsoft Operations Management Suite (OMS) and Azure Automation. | |
| The runbook is intended to be run on a Hybrid Runbook Worker. | |
| More information about the context for this demo script is available in this article on PowerShell Magazine: | |
| http://www.powershellmagazine.com/?p=12377 | |
| Prerequisites for using notifications (comment out or remove if not needed): | |
| The PSSlack module (available on PowerShell Gallery) | |
| The Send-ToastyMessage PowerShell function to send a message to the Toasty app for Windows mobile devices (available here: https://gist.github.com/janegilring/265068afa9969e1d01675ee143d00bfc) | |
| You have a royalty-free right to use, modify, reproduce, and | |
| distribute this script file in any way you find useful, provided that | |
| you agree that the creator, owner above has no warranty, obligations, | |
| or liability for such use. | |
| VERSION HISTORY: | |
| 1.0 07.07.2016 - Initial release | |
| #> | |
| param( | |
| [Object]$WebhookData | |
| ) | |
| Import-Module -Name PSSlack | |
| #region Variables | |
| $credential = Get-AutomationPSCredential -Name cred-AutoRemediation | |
| $OperatorToastyDeviceID = Get-AutomationVariable -Name var-OperatorToastyDeviceID | |
| $SlackChannel = Get-AutomationVariable -Name var-SlackChannel | |
| $SlackToken = Get-AutomationVariable -Name var-SlackToken | |
| $SlackUri = Get-AutomationVariable -Name var-SlackUri | |
| $RequestBody = ConvertFrom-JSON -InputObject $WebhookData.RequestBody | |
| $Computers = $RequestBody.SearchResults.value | Sort-Object -Property Computer -Unique | |
| #endregion | |
| #region Notifications | |
| $NotificationTitle = "$($WebhookData.WebhookName) was triggered" | |
| $NotificationMessage = "Computers affected: (($Computers | Select-Object -Expand Computer) -join ',')" | |
| Send-ToastyMessage -DeviceID $OperatorToastyDeviceID -Title $NotificationTitle -Message $NotificationMessage | |
| Send-SlackMessage -Token $SlackToken -Channel $SlackChannel -SlackMessage ($NotificationTitle + $NotificationMessage) -Uri $SlackUri | |
| #endregion | |
| #region Remediate - run Windows Update cleanup | |
| foreach ($Computer in $Computers.Computer) { | |
| $CimSession = New-CimSession -Credential $credential -ComputerName $computer | |
| $VolumeBefore = Get-Volume -DriveLetter C -CimSession $CimSession | |
| Write-Output "Invoking Component Store Cleanup on computer $Computer" | |
| $Cleanup = Invoke-Command -ComputerName $Computer -ScriptBlock {dism.exe /online /Cleanup-Image /StartComponentCleanup} -Credential $credential | |
| Write-Output $Cleanup | |
| $VolumeAfter = Get-Volume -DriveLetter C -CimSession $CimSession | |
| Write-Output "Sending notifications" | |
| $BeforeSizeRemaining = [string]::Format("{0:0.00} GB free before cleanup", $VolumeBefore.SizeRemaining / 1GB) | |
| $AfterSizeRemaining = [string]::Format("{0:0.00} GB free after cleanup", $VolumeAfter.SizeRemaining / 1GB) | |
| $NotificationTitle = "Automated Windows Update cleanup was triggered for $computer" | |
| $NotificationMessage = "Free space before cleanup: $BeforeSizeRemaining Free space before cleanup: $AfterSizeRemaining" | |
| Send-ToastyMessage -DeviceID $OperatorToastyDeviceID -Title $NotificationTitle -Message $NotificationMessage | |
| Send-SlackMessage -Token $SlackToken -Channel $SlackChannel -SlackMessage ($NotificationTitle + $NotificationMessage) -Uri $SlackUri | |
| } | |
| #endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment