Skip to content

Instantly share code, notes, and snippets.

@rossnz
rossnz / gist:5107076
Last active December 14, 2015 15:19
Create screenshot from URL using url2picture.com
function Get-Screenshot ($url) {
$apiKey = 'Your-API-key'
$secret = 'Your-Secret'
$urlpart = '?apikey=' + $apiKey + '&url=' + $url
[Void][Reflection.Assembly]::LoadWithPartialName("System.Web")
$token = ([System.Web.Security.FormsAuthentication]::HashPasswordForStoringInConfigFile($urlpart + $secret, 'MD5')).tolower() # Won't work if upper case
$requrl = 'http://www.url2picture.com/Picture/Png' + $urlpart + '&token=' + $token
Write-Output $requrl
@rossnz
rossnz / Get-UserAgent.ps1
Last active September 10, 2024 02:23
Generate a valid UserAgent using PowerShell's pre-built UserAgent strings. Run without parameters to get a random UA, or pass in a generic browser type to get a specific value.
function Get-UserAgent {
# Uses PowerShell's prebuilt UA strings. See
# http://goo.gl/9IGloI
param (
[ValidateSet('Firefox','Chrome','InternetExplorer','Opera','Safari')]
[string]$browsertype
)
if (!$browsertype) {
$browsers = @('Firefox','Chrome','InternetExplorer','Opera','Safari')
@rossnz
rossnz / gist:8174043
Last active January 1, 2016 16:59
Generating a UNIX timestamp
# Show current date/time as a UNIX timestamp
date +%s
# Show the timestamp for an arbitrary date/time
date -d '01/12/2013 16:42:13' +%s
# Convert a timestamp to a date/time
date -d @1357962133
@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
@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 / 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: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: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: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: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
}