Skip to content

Instantly share code, notes, and snippets.

View ctigeek's full-sized avatar

Steven Swenson ctigeek

View GitHub Profile
@ctigeek
ctigeek / PowershellAes.ps1
Last active March 25, 2024 23:16
Aes Encryption using powershell.
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 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
}
@ctigeek
ctigeek / Compare-XmlDocs.ps1
Last active July 5, 2023 21:01
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 / Get-AllMailboxes.ps1
Last active April 6, 2023 09:11
Get all Hex and Rackspace mailboxes for your account, and any indirect accounts.
## This code released under the MIT open source license.
## See here for details: https://opensource.org/licenses/MIT
## This code is provided with NO SUPPORT and NO WARRANTY and NO GUARANTEE. Use at your own risk.
[string] $ApiKey = ""
[string] $ApiSecret = ""
## only populate $indirectAccountNumber if you want a list from one specific account, otherwise leave blank.
[string] $indirectAccountNumber = ""
[string] $path = "AllMailboxes.csv"
@ctigeek
ctigeek / SendMailgunEmail.ps1
Last active January 2, 2023 17:36
Send an email using Mailgun in Powershell.
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 / 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);
}
@ctigeek
ctigeek / SoapApiTest.cs
Created January 4, 2021 21:55
Calling the soap api
using System;
using NUnit.Framework;
namespace RackspaceSoapTest
{
[TestFixture]
public class Class1
{
[Test]
public void Test1()
@ctigeek
ctigeek / TcpListener.cs
Created June 22, 2020 21:49
A simple TCP listener. Assumes you're sending UTF8 text to it.
using System;
using System.IO;
using System.Net;
using System.Text;
namespace TcpListener
{
class Program
{
static void Main(string[] args)
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 / ParallelTaskRunnerExtension.cs
Created July 1, 2014 01:36
ParallelTaskRunnerExtension - Running tasks in parallel, but limiting the concurrency.
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)