Skip to content

Instantly share code, notes, and snippets.

View ctigeek's full-sized avatar

Steven Swenson ctigeek

View GitHub Profile
@ctigeek
ctigeek / Periodic.cs
Created December 5, 2016 01:35
Run an Action periodically....
public class Periodic : IDisposable
{
private static readonly object lockObject = new object();
private readonly List<PeriodicEvent> history = new List<PeriodicEvent>();
private readonly Action action;
private readonly Timer timer;
private long id = 0;
public PeriodicEvent LatestEvent { get; private set; }
@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 May 27, 2024 06:48
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 May 27, 2024 06:31
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 October 7, 2024 19:46
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
}