Skip to content

Instantly share code, notes, and snippets.

View ctigeek's full-sized avatar

Steven Swenson ctigeek

View GitHub Profile
@ctigeek
ctigeek / Cache.cs
Last active August 29, 2015 14:08
Generic .net cache....
class Cache<T> where T : someBaseType
{
public readonly TimeSpan LifeSpan;
public readonly SemaphoreSlim semaphore;
private Dictionary<string, CacheItem<T>> itemDict;
public Cache(TimeSpan lifespan)
{
this.LifeSpan = lifespan;
@ctigeek
ctigeek / AlertInput.ino
Last active August 29, 2015 14:09
Sending an email from Arduino
const int sensorPin = A0; // the number of the pushbutton pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 150; // the debounce time; increase if the output flickers
int sensorValue = 0;
int alreadyAlerted = 0;
void setup() {
Serial.begin(9600);
Serial.println(F("Serial is working.... "));
@ctigeek
ctigeek / CreateDomainForest.ps1
Created January 27, 2015 19:45
Create Domain Forest
Install-WindowsFeature -Name Ad-Domain-Services -IncludeManagementTools
###local admin password....
$pass = ConvertTo-SecureString "XXXXXXXXXX" -AsPlainText -Force
Install-ADDSForest -DomainName "MyNewDomain.local" -SafeModeAdministratorPassword $pass
@ctigeek
ctigeek / scheduling.cs
Last active August 29, 2015 14:16
Sudo code for scheduling
//The idea here is there's a difference between how the event repeats (Repetition) and when that repetition occurs (When/NotWhen).
// Between 9am and 6pm, run every 5 minutes, exactly on minute 0,5,10,etc., except on the last day of the month.
// Before 9am and after 6pm, run every 10 minutes, exactly on minute 0,10,20, etc, except on the last day of the month.
// On the last day of the month, run every 20 minutes, exactly on minute 0,20,40.
var config = ScheduleConfig.EveryMinute(5).OnTheMinute(0).When(Between(Hour(9),Hour(18))).WhenNot(DayOfMonth.Last).Named("DayTime")
.AddRepetition(Repetition.EveryMinute(10).OnTheMinute(0).When(Before(Hour(9)), After(Hour(18))).WhenNot(DayOfMonth.Last).Named("NightTime")
.AddRepetition(Repetition.EveryMinute(20).OnTheMinute(0).When(DayOfMonth.Last).Named("LastDayOfMonth");
var schedule = new Schedule(scheduleConfig); //This could also be IObservable....
@ctigeek
ctigeek / fileserver.js
Created March 18, 2015 13:18
Simple static file server
var http = require('http');
var url = require("url");
var path = require("path");
var fs = require('fs');
http.createServer(function(req, res) {
var uri = url.parse(req.url).pathname;
var filename = path.join(process.cwd(), uri);
if (fs.existsSync(filename)) {
var stats = fs.lstatSync(filename);
@ctigeek
ctigeek / EncryptString.cs
Created April 3, 2015 00:39
Encrypt String
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace SandboxThing
{
class EncryptedString
{
private const int AesBlockSize = 128;
@ctigeek
ctigeek / StaticIP.ps1
Created April 7, 2015 16:27
Change server from DHCP to static...
$adapter = Get-NetAdapter -Name "Ethernet"
$ipv4 = $adapter | Get-NetIPAddress -AddressFamily IPv4
## Remove IPv6 Address and disable IPv6 binding....
$ipv6 = $adapter | Get-NetIPAddress -AddressFamily IPv6 -ErrorAction SilentlyContinue
if ($ipv6) {
$ipv6 | Remove-NetIPAddress -Confirm:$false
}
$binding = (Get-NetAdapterBinding | Where-Object { $_.ComponentID -eq "ms_tcpip6" })
@ctigeek
ctigeek / RunPowershell.cs
Last active September 16, 2016 12:22
Run powershell file from c#
using System.Management.Automation;
//....
public static async Task<string> RunPowershellScript(string scriptName, Dictionary<string,string> parameters)
{
using (var ps = PowerShell.Create())
{
var script = string.Format(". \"{0}\" {1}", scriptName, string.Join(" ", parameters.Select(p => "-" + p.Key + " '" + p.Value + "'")));
if (debug)
{
log.Debug("Running the following script:\r\n " + script);
@ctigeek
ctigeek / Clear-Memcache.ps1
Created May 14, 2015 20:01
Flush memcache from powershell...
function Clear-Memcache($server, $port = 11211)
{
$msg = [System.Text.Encoding]::ASCII.GetBytes("flush_all`r`nquit`r`n")
$c = New-Object System.Net.Sockets.TcpClient($server, $port)
$str = $c.GetStream()
$str.Write($msg, 0, $msg.Length)
$buf = New-Object System.Byte[] 4096
$count = $str.Read($buf, 0, 4096)
[System.Text.Encoding]::ASCII.GetString($buf, 0, $count)
$str.Close()
@ctigeek
ctigeek / Get-XApiSignature.ps1
Last active November 9, 2022 19:22
Creates an X-Api-Signature for making a REST API Call.
function Get-XapiSignature($userAgent, $apiKey, $apiSecret) {
$dateTime = [DateTime]::UtcNow.ToString("yyyyMMddHHmmss");
$dataToSign = [String]::Format("{0}{1}{2}{3}", $apikey, $userAgent, $dateTime, $apiSecret);
$sha1 = [System.Security.Cryptography.SHA1]::Create();
$signedBytes = $sha1.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($dataToSign));
$signature = [System.Convert]::ToBase64String($signedBytes);
$sha1.Dispose()
[String]::Format("{0}:{1}:{2}", $apikey, $dateTime, $signature);
}