Skip to content

Instantly share code, notes, and snippets.

Steven Swenson ctigeek

Block or report user

Report or block ctigeek

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ctigeek
ctigeek / PowershellAes.ps1
Last active Sep 20, 2019
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 / Compare-XmlDocs.ps1
Last active Aug 4, 2019
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) {
@ctigeek
ctigeek / SendMailgunEmail.ps1
Last active Jun 12, 2019
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 / 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)
@ctigeek
ctigeek / Retry.cs
Created Apr 11, 2018
Retry pattern
View Retry.cs
public static class Retry
{
public static void Do(Action action, TimeSpan retryInterval, int maxAttemptCount)
{
try
{
action();
}
catch
{
@ctigeek
ctigeek / Import-XlsData.ps1
Last active Mar 13, 2018
Query XLS spreadsheet from powershell
View Import-XlsData.ps1
function Import-XlsData($filePath) {
## You have to have these drivers...
##https://www.microsoft.com/en-us/download/confirmation.aspx?id=13255
##The first row will be the column names
##This only looks at the first sheet. If you want a different sheet you'll have to adjust the table name retrieved from $schema.
$strProvider = "Provider=Microsoft.ACE.OLEDB.12.0"
$strDataSource = "Data Source=$filePath"
$strExtend = "Extended Properties='Excel 12.0;IMEX=1'"
You can’t perform that action at this time.