Skip to content

Instantly share code, notes, and snippets.

@gregjhogan
gregjhogan / random-string-generator.ps1
Last active February 5, 2025 17:14
Generate random string in powershell
#lowercase letters/numbers only
-join ((48..57) + (97..122) | Get-Random -Count 32 | % {[char]$_})
# all characters
-join ((33..126) | Get-Random -Count 32 | % {[char]$_})
@gregjhogan
gregjhogan / catch-all-command-errors-in-powershell.ps1
Created August 1, 2016 17:39
Catch all errors from commands (including native) run in a powershell script
$ErrorActionPreference = 'Stop'
try
{
npm install bogus-package 2>&1
}
catch
{
Write-Host "Error: $_"
}
@gregjhogan
gregjhogan / windows-10-fix-cortona-start-menu.ps1
Last active April 23, 2017 13:38
Fix cortona search in Windows 10
Get-AppXPackage -AllUsers | Where {$_.Name -like '*cortana*'} | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}
@gregjhogan
gregjhogan / generate-api-key.sh
Created October 21, 2016 14:45
Generate an API key
dd if=/dev/urandom bs=1 count=64 2>/dev/null | base64 --wrap 0 && echo
@gregjhogan
gregjhogan / openssl-new-ssl-key-cert-csr-pfx.sh
Last active October 25, 2016 02:46
generate new openssl SSL key/cert/csr/pfx
NAME=test-cert
# generate key
openssl genrsa -out $NAME.key 2048
# generate certificate request
openssl req -new -sha256 -key $NAME.key -out $NAME.csr
# self-sign certificate (if not sending to CA)
openssl x509 -req -days 3650 -in $NAME.csr -signkey $NAME.key -out $NAME.crt
# convert to pfx
openssl pkcs12 -export -out $NAME.pfx -inkey $NAME.key -in $NAME.crt
@gregjhogan
gregjhogan / winrm-https-self-signed-cert.ps1
Created November 11, 2016 23:33
Configure WinRM HTTPS w/ self-signed certificate
# configure
$cert = New-SelfSignedCertificate -CertstoreLocation Cert:\LocalMachine\My -DnsName $env:COMPUTERNAME
Enable-PSRemoting -SkipNetworkProfileCheck -Force
New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address * -CertificateThumbPrint $cert.Thumbprint –Force
New-NetFirewallRule -DisplayName "Windows Remote Management (HTTPS-In)" -Name "Windows Remote Management (HTTPS-In)" -Profile Any -LocalPort 5986 -Protocol TCP
# connect
Enter-PSSession -ComputerName {X.X.X.X} -Credential (Get-Credential) -SessionOption (New-PsSessionOption -SkipCACheck -SkipCNCheck) -UseSSL
@gregjhogan
gregjhogan / sqlconnection-sql-test.ps1
Last active April 23, 2017 13:39
PowerShell SQL connection test
Write-Output "starting ..."
$dataSource = 'tcp:{server},1433'
$user = '{user}'
$pwd = '{pass}'
$database = '{db}'
$connectionString = "Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;"
try
{
$query = "SELECT 1"
@gregjhogan
gregjhogan / tcp-listener-trace.ps1
Last active January 16, 2017 22:19
Listen on a tcp port
function Trace-Port([string]$IPAddress="127.0.0.1", [int]$Port=80, [switch]$Echo=$false){
$listener = new-object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse($IPAddress), $port)
$listener.start()
[byte[]]$bytes = 0..255|%{0}
write-host "Waiting for a connection on port $port..."
$client = $listener.AcceptTcpClient()
write-host "Connected from $($client.Client.RemoteEndPoint)"
$stream = $client.GetStream()
while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0)
{
@gregjhogan
gregjhogan / change-ps-history-location.ps1
Last active January 28, 2017 14:42
Change location of PS history file
Add-Content $Profile @"
Set-PSReadlineOption -HistorySavePath "$env:USERPROFILE\OneDrive\AppData\PSReadLine\ConsoleHost_history.txt"
"@
@gregjhogan
gregjhogan / cpu-load.ps1
Created March 13, 2017 18:19
Generate CPU load
for($i=(get-date);$i.AddMinutes(1) -gt (get-date);){}