Skip to content

Instantly share code, notes, and snippets.

@ctigeek
ctigeek / SendMailgunEmail.ps1
Last active Jul 5, 2020
Send an email using Mailgun in Powershell.
View SendMailgunEmail.ps1
function Send-MailgunEmail($from, $to, $subject, $body, $emaildomain, $apikey) {
$idpass = "api:$($apikey)"
$basicauth = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($idpass))
$headers = @{
Authorization = "Basic $basicauth"
}
$url = "https://api.mailgun.net/v2/$($emaildomain)/messages"
$body = @{
from = $from;
@ctigeek
ctigeek / TcpListener.cs
Created Jun 22, 2020
A simple TCP listener. Assumes you're sending UTF8 text to it.
View TcpListener.cs
using System;
using System.IO;
using System.Net;
using System.Text;
namespace TcpListener
{
class Program
{
static void Main(string[] args)
@ctigeek
ctigeek / Compare-XmlDocs.ps1
Last active May 7, 2020
Powershell - Compare two XML documents.
View Compare-XmlDocs.ps1
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) {
View GenericCache.cs
public interface IGenericCache<T>
{
TimeSpan CacheTtl { get; set; }
Func<T> LoadCache { get; set; }
T ReturnCache();
T ForceRefresh();
}
public class GenericCache<T> : IGenericCache<T>
{
@ctigeek
ctigeek / PowershellAes.ps1
Last active Mar 2, 2020
Aes Encryption using powershell.
View PowershellAes.ps1
function Create-AesManagedObject($key, $IV) {
$aesManaged = New-Object "System.Security.Cryptography.AesManaged"
$aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
$aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
$aesManaged.BlockSize = 128
$aesManaged.KeySize = 256
if ($IV) {
if ($IV.getType().Name -eq "String") {
$aesManaged.IV = [System.Convert]::FromBase64String($IV)
}
@ctigeek
ctigeek / Start-Sleep.ps1
Created Mar 23, 2016
Powershell sleep function, with progress bar.
View Start-Sleep.ps1
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 / ParallelTaskRunnerExtension.cs
Created Jul 1, 2014
ParallelTaskRunnerExtension - Running tasks in parallel, but limiting the concurrency.
View ParallelTaskRunnerExtension.cs
public static class ParallelTaskRunnerExtension
{
public static void RunTasks(this IEnumerable<Task> tasks, int maxConcurrency, Action<Task> taskComplete = null)
{
if (maxConcurrency <= 0) throw new ArgumentException("maxConcurrency must be more than 0.");
int taskCount = 0;
int nextIndex = 0;
var currentTasks = new Task[maxConcurrency];
foreach (var task in tasks)
@ctigeek
ctigeek / Tjson.cs
Created Jan 24, 2019
Tjson Deserializer....
View Tjson.cs
// written by Steven Swenson
// Released under the MIT open source license...
// No warranty at all. use at your own risk.
public static class Tjson
{
//https://gist.github.com/ctigeek/4950c4b07a50a0791f012858e3bb2214
private static readonly Splitter.Preserver QuotePreserver = new Splitter.Preserver { Start = '"', End = '"', Escape = '\\' };
private static readonly Splitter.Preserver CurlyBoiPreserver = new Splitter.Preserver { Start = '{', End = '}', Escape = '\\', Recursive = true, SubPreserver = QuotePreserver };
private static readonly Splitter.Preserver BracketPreserver = new Splitter.Preserver { Start = '[', End = ']', Escape = '\\', Recursive = true, SubPreserver = QuotePreserver };
@ctigeek
ctigeek / Split.cs
Last active Jan 24, 2019
Split a string on a character, but don't split based on start-end characters.
View Split.cs
/// <summary>
/// Split a string while preserving sections of it.
/// Similar to string.Split, but you can define start-end characters (e.g. quotes, brackets, braces) inside of which it will NOT split.
/// Preservers can also be "recursive" which means it can determine if it's in nested brackets or parens, etc.
/// If the start & end characters are different, there's a good chance you want to set recursive to true.
/// See the associated unit test for an example that can parse json....
/// Also supports escape characters so the separator and start-end characters can be ignored.
/// </summary>
public class Splitter
@ctigeek
ctigeek / EncryptionExample.cs
Created Jun 19, 2018
File Encryption example.
View EncryptionExample.cs
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
namespace EncryptionExample
{
class Program
{
static void Main(string[] args)
You can’t perform that action at this time.