Skip to content

Instantly share code, notes, and snippets.

function Post-ToSlack
{
Param(
[Parameter(Mandatory = $true,Position = 0,HelpMessage = 'Chat message to send')]
[ValidateNotNullorEmpty()]
[String]$Message,
[Parameter(Mandatory = $true,Position = 1,HelpMessage = 'Slack webhook URL')]
[ValidateNotNullorEmpty()]
[String]$uri,
[Parameter(Mandatory = $false,Position = 2,HelpMessage = 'Slack channel')]
@rossnz
rossnz / Exit-TsSession.ps1
Last active June 8, 2016 09:35
Log yourself off from old TS / RDP sessions. Remote computer name can be passed as a parameter, or on the pipeline. Useful if you have to log out of a lot of machines :)
function Exit-TsSession {
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True)]
[string[]]$computername
)
begin {
$username = $env:USERNAME
}
#-------------------------------------------------------------------------------
# Name: cookies.py
# Purpose: Import cookies as JSON and output in WPT script format
#
# Author: Ross Brown
#
# Created: 23/05/2014
# Copyright: (c) RBrown 2014
# Licence: Beerware
#-------------------------------------------------------------------------------
@rossnz
rossnz / gist:8519986
Last active January 3, 2025 19:27
Convert a DateTime object to Unix timestamp in PowerShell
Function Convert-ToUnixDate ($PSdate) {
$epoch = [timezone]::CurrentTimeZone.ToLocalTime([datetime]'1/1/1970')
(New-TimeSpan -Start $epoch -End $PSdate).TotalSeconds
}
@rossnz
rossnz / gist:8519905
Last active January 3, 2016 21:09
Convert a Unix datestamp to a .Net DateTime object in PowerShell
Function Get-UnixDate ($UnixDate) {
[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($UnixDate))
}
# It returns a PowerShell DateTime object, which can be manipulated
# using the standard DateTime methods, eg
PS> $logtime = Get-UnixDate 1269313872.893866062
PS> $logtime
@rossnz
rossnz / gist:8519848
Last active January 3, 2016 21:09
Masking a phone number
PS> 'My phone number is 01-234-5678' -replace '[\d-]','#'
My phone number is ###########
@rossnz
rossnz / gist:8519834
Created January 20, 2014 13:29
String replace - only keep digits
$myString = "99 Bottles of beer on the wall!"
$myValue = $myString -replace '[ !,.a-zA-Z]'
Write-Host $myValue
@rossnz
rossnz / gist:8519825
Created January 20, 2014 13:28
String remove - only keep numbers
PS> '1 is a number, so is 2. I quite like 3 and 4 too' -replace '[ ,.a-zA-Z]'
1234
@rossnz
rossnz / gist:8519809
Created January 20, 2014 13:27
Removing unwanted characters with PowerShell
PS> '1,2,3,4,5,6,,,' -replace ','
123456
@rossnz
rossnz / nginxtopsites.sh
Created January 20, 2014 13:22
List most active Nginx sites, for when you deploy Nginx as a forward proxy
cat /var/log/nginx/access.log | awk '$9 ~ "200" {print $2}' | sort | uniq -c | sort -hr