Skip to content

Instantly share code, notes, and snippets.

View ctigeek's full-sized avatar

Steven Swenson ctigeek

View GitHub Profile
@ctigeek
ctigeek / Get-ResponseCode.ps1
Created November 1, 2016 15:02
GET the response code from a URL.
function Get-ResponseCode($url) {
try {
$response = Invoke-WebRequest $url
$response.StatusCode
}
catch {
$_.Exception.Response.StatusCode.Value__
}
}
@ctigeek
ctigeek / RunStuffAndWait.cs
Last active November 1, 2016 14:22
Run stuff and wait in program.cs
class Program
{
static void Main(string[] args)
{
ManualResetEventSlim manualResetEvent = null;
Console.CancelKeyPress += (sender, eventArgs) =>
{
Console.WriteLine("cancel!");
manualResetEvent?.Set();
@ctigeek
ctigeek / Stop-ServiceAndWaitForItToDie.ps1
Last active December 11, 2017 15:24
Stop a windows service. Wait for the process to die. Option to kill it if it doesn't stop.
function Stop-ServiceAndWaitForItToDie {
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
[object] $Service,
[Parameter(Mandatory=$False,ValueFromPipeline=$False)]
[int] $TimeoutInSeconds = 30,
[Parameter(Mandatory=$False,ValueFromPipeline=$False)]
[Switch] $KillAfterTimeout,
[Parameter(Mandatory=$False,ValueFromPipeline=$False)]
@ctigeek
ctigeek / CallRest.ps1
Created September 16, 2016 18:32
Clean up after Invoke-RestMethod after PUT or DELETE.
$baseUrl = "https://blah.com"
$servicePoint = [System.Net.ServicePointManager]::FindServicePoint($baseUrl)
$url = "$baseUrl/blah/blah/blah"
Invoke-RestMethod -Uri $url -Method Delete -ErrorAction Stop
##without this cleanup, the above line will stop working after two calls
$ServicePoint.CloseConnectionGroup("")
@ctigeek
ctigeek / EncryptionAbstract.txt
Created August 31, 2016 22:33
Encryption for Developers
**Encryption doesn't have to be difficult!**
In this pragmatic talk we'll be discussing how to use cryptography in your application.
As an example, we'll walk through how to use various cryptographic technologies to design an end-to-end encrypted chat application.
What kind of encryption do you use?
What are the most common and secure configurations?
And most importantly how do you integrate it into your software?
We'll address all these questions and walk through code samples in c#.
We'll cover symmetrical (AES) and public-key cryptographic algorithms, along with multiple types of hashing,
and various use-cases for each.
Cryptography configuration can sometimes be tricky, but we'll look at the most secure and versatile configurations for each methodology.
@ctigeek
ctigeek / Send-SlackNotification.ps1
Created May 20, 2016 20:21
Send-SlackNotification (Slack Web RPC API)
function Send-SlackNotification($message) {
$url = "https://slack.com/api/chat.postMessage"
$headers = @{"Content-Type"="application/x-www-form-urlencoded";"User-Agent"="SlackBot"}
$body = @{ token="PUT_YOUR_BOT_KEY_HERE"; channel="PUT_CHANNEL_HERE"; text=$message; as_user="true"; }
##If you are using powershell v5 you can change this to Invoke-RestMethod.
##The UsePasicParsing option is not supported in v4 for Invoke-RestMethod, and you really want that option.
$result = Invoke-WebRequest -Uri $url -Body $body -Headers $headers -Method Post -ErrorAction SilentlyContinue -UseBasicParsing
$result.Content | ConvertFrom-Json
}
@ctigeek
ctigeek / ISessionExtensions.cs
Created May 18, 2016 19:10
Helper functions for storing data in session.
public static void SetInt64(this ISession session, string key, long lng)
{
var bytes = BitConverter.GetBytes(lng);
session.Set(key, bytes);
}
public static long GetInt64(this ISession session, string key, long defaultIfNull)
{
var bytes = session.Get(key);
if (bytes == null || bytes.Length == 0)
@ctigeek
ctigeek / Compare-XmlDocs.ps1
Last active July 5, 2023 21:01
Powershell - Compare two XML documents.
function Compare-XmlDocs($actual, $expected) {
if ($actual.Name -ne $expected.Name) {
throw "Actual name not same as expected: actual=" + $actual.Name
}
##attributes...
if ($actual.Attributes.Count -ne $expected.Attributes.Count) {
throw "attribute mismatch for actual=" + $actual.Name
}
for ($i=0;$i -lt $expected.Attributes.Count; $i =$i+1) {
@ctigeek
ctigeek / Start-Sleep.ps1
Created March 23, 2016 14:44
Powershell sleep function, with progress bar.
function Start-Sleep($seconds) {
$doneDT = (Get-Date).AddSeconds($seconds)
while($doneDT -gt (Get-Date)) {
$secondsLeft = $doneDT.Subtract((Get-Date)).TotalSeconds
$percent = ($seconds - $secondsLeft) / $seconds * 100
Write-Progress -Activity "Sleeping" -Status "Sleeping..." -SecondsRemaining $secondsLeft -PercentComplete $percent
[System.Threading.Thread]::Sleep(500)
}
Write-Progress -Activity "Sleeping" -Status "Sleeping..." -SecondsRemaining 0 -Completed
}
@ctigeek
ctigeek / config.js
Last active February 2, 2016 20:33
Enable SSL in MongoExpress....
// **snip***
} else {
mongo = {
db: 'db',
host: 'localhost',
password: 'pass',
port: 27017,
url: '"mongodb://localhost:27017/db',
username: 'admin',
ssl: true // <<<--------------Add this....