Skip to content

Instantly share code, notes, and snippets.

View deangrant's full-sized avatar

Dean Grant deangrant

View GitHub Profile
Process
{
Try
{
# Sends HTTPS request to the Slack Web API service.
$WebRequest = Invoke-WebRequest -Uri $Uri -Body $PostMessage
# Conditional logic to generate custom error message if JSON object response contains a top-level error property.
If ($WebRequest.Content -like '*"ok":false*')
{
# Terminates with error if the response contains an error property, parses the error code and stops processing of the command.
# Generates POST message to be sent to the slack channel.
$PostMessage = @{token="$Token";channel="$Channel";text="$Text";username="$Bot";icon_url="$Icon"}
Function Get-Uptime {
<#
.SYNOPSIS
Gets the uptime for a local or remote computer.
.DESCRIPTION
The Get-Uptime function gets the uptime for specified computers by retrieving the LastBootUpTime from Win32_OperatingSystem WMI class. The uptime period is calculated in days by comparing the interval between the LastBootUpTime and the current date and time.
The function sends an Internet Control Message Protocol (ICMP) echo request packet to determine if a the specified computer may be contacted. If the computer is offline this will report a status of 'Offline. If an echo reponse is received, the uptime status will
@deangrant
deangrant / Regular Expression Cheat Sheet
Last active July 22, 2016 05:40
Regular Expression Cheat Sheet
# Validate Base64 string.
[0-9a-zA-Z\+=]{20,}
# Validate Subnet in CIDR notation.
^(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\/(\d{1}|[0-2]{1}\d{1}|3[0-2])$
# Validate IP address range.
^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3}-(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3}$
# Validate IP address.
@deangrant
deangrant / getRestHostByName.js
Last active July 18, 2022 08:57
com.deangrant.http-rest/getRestHostByName
// Returns a collection of REST:RESTHost object types from the inventory service plugin
var getHosts = RESTHostManager.getHosts()
// Iterates the collection to return the REST:RESTHost object type and perform a match on the name attribute provided as an input parameter and returns the REST:RESTHost type if true.
for(var restHostId in getHosts){
var restHost = RESTHostManager.getHost(getHosts[restHostId])
if(restHost.name == name){
return restHost
} // if(restHost.name == name)
} // var restHost = RESTHostManager.getHost(getHosts[restHostId])
@deangrant
deangrant / setResourceElementMimeType.js
Last active September 13, 2017 15:30
com.deangrant.library.vro/setResourceElementMimeType
// Retrieves the resource element as a mime attachment from the specified input parameter (resourceElement).
var content = resourceElement.getContentAsMimeAttachment();
System.debug('The resource element ' + resourceElement.name + ' has been returned with the mime type ' + content.mimeType)
content.mimeType = mime;
// Sets the mime attachement to the type specifed from the input parameter (mime).
resourceElement.setContentFromMimeAttachment(content);
System.log('The resource element ' + resourceElement.name + ' mime type type has been set to ' + mime)
@deangrant
deangrant / setResourceElementMimeType.js
Created September 14, 2018 06:24
setResourceElementMimeType.js
// Retrieves the resource element as a mime attachment from the specified input parameter (resourceElement).
var content = resourceElement.getContentAsMimeAttachment();
System.debug('The resource element ' + resourceElement.name + ' has been returned with the mime type ' + content.mimeType)
content.mimeType = mime;
// Sets the mime attachement to the type specifed from the input parameter (mime).
resourceElement.setContentFromMimeAttachment(content);
System.log('The resource element ' + resourceElement.name + ' mime type type has been set to ' + mime)]]
@deangrant
deangrant / localized_time_to_epoch.py
Last active May 24, 2022 09:26
A python function to convert a localized timestamp to epoch / unix time
from dateutil import parser
from pytz import timezone
from datetime import datetime
import time
def localized_time_to_epoch(timestamp, time_zone):
strptime = "%Y-%m-%d %H:%M:%S"
timestamp = parser.parse(timestamp)
localize = timezone(timezone).localize(datetime.strptime(str(timestamp), strptime))
@deangrant
deangrant / .bashrc
Last active May 26, 2022 07:00
Sample terminal session and ssh configuration to be used in combination with Pomerium and Microsoft Azure
# ~/.bashrc
# exported variable for SSH jump host
export SSH_JUMPHOST='{{ hostname }}'
# Creates a short lived SSH certificate signed by AAD.
# Requires public key file creating using the command - ssh-keygen -t rsa -b 4096
alias az_ssh_cert='az ssh cert --public-key-file ~/.ssh/id_rsa.pub --file ~/.ssh/id_rsa-aadcert.pub'
# Connects to the target host first by making a connection to the jump host and then establishing
@deangrant
deangrant / on_change_date.js
Created May 26, 2022 07:27
Functions to change the start and end date if exceeding limit by number of days
var date_limit = {{ number_of_days }}
$("#startDate").on("change", function(e) {
var maxDate = new Date($(this).val())
var endDate = new Date($('#endDate').val())
var maxDate = maxDate.setDate(maxDate.getDate() + date_limit)
if (endDate > maxDate){
$('#endDate').val(new Date(maxDate).toJSON().slice(0,10));
}