Skip to content

Instantly share code, notes, and snippets.

View ctigeek's full-sized avatar

Steven Swenson ctigeek

View GitHub Profile
@ctigeek
ctigeek / Tjson.cs
Created January 24, 2019 15:34
Tjson Deserializer....
// 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 January 24, 2019 15:16
Split a string on a character, but don't split based on start-end characters.
/// <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 June 19, 2018 15:36
File Encryption example.
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 April 11, 2018 00:34
Retry pattern
public static class Retry
{
public static void Do(Action action, TimeSpan retryInterval, int maxAttemptCount)
{
try
{
action();
}
catch
{
@ctigeek
ctigeek / Import-XlsData.ps1
Last active March 13, 2018 18:13
Query XLS spreadsheet from powershell
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'"
@ctigeek
ctigeek / Config.cs
Last active February 4, 2018 23:52
Save name-object pairs securely.
public static class Config
{
public const int MaxValueLength = 7990; //If you change the size of the [secret] column then this needs to be changed.
public static string ConnectionString { get; set; }
public static string AesKey { get; set; }
public static int SecretPaddingSize { get; set; } = 1000; //e.g. any secret less than 1000 bytes will be padded so that it is at least 1000 bytes. This is to prevent information leaking regarding the size of the secret. Set to 0 to disable padding.
@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 / QuartzClustered.config
Created November 28, 2017 21:29
Quartz clustered configuration.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<quartz>
<add key="quartz.scheduler.instanceName" value="test1" />
<add key="quartz.scheduler.instanceId" value="AUTO" />
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
<add key="quartz.threadPool.threadCount" value="10" />
@ctigeek
ctigeek / Remove-Service.ps1
Last active November 8, 2017 15:28
Delete a windows service.
function Remove-Service {
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)] [object] $service
)
BEGIN { }
PROCESS {
$serviceName = $service.Name
if (-not $serviceName) {
@ctigeek
ctigeek / Configure-Https.ps1
Last active September 22, 2017 19:54
Configure self-hosted app for HTTPS
## the cert dns doesn't really matter since it's self-signed.
$certName = "myappname.local"
$cert = New-SelfSignedCertificate -CertStoreLocation Cert:\LocalMachine\My -DnsName $certName
Write-Host "Created self signed cert: $($cert.Thumbprint) "
$guid = [guid]::NewGuid()
$ip = "0.0.0.0:443"
"http add sslcert ipport=$ip certhash=$($cert.Thumbprint) appid={$guid} " | netsh
#### netsh http delete sslcert ipport=$ip