Skip to content

Instantly share code, notes, and snippets.

View ctigeek's full-sized avatar

Steven Swenson ctigeek

View GitHub Profile
@ctigeek
ctigeek / InstallWindowsUpdates.ps1
Created November 2, 2015 14:06
Install all available windows updates on a computer. This requires PsExec to be installed locally (not on the remote computer.)
function Install-WindowsUpdates($computerName) {
$command = {
if (! (Test-Path c:\updates )) { New-Item c:\updates -ItemType Directory }
if (Test-Path c:\updates\installUpdates.ps1 ) { Remove-Item c:\updates\installUpdates.ps1 -Force -Confirm:$false }
"Get-WUInstall -AcceptAll -AutoReboot -Verbose " | Out-File c:\updates\installUpdates.ps1
}
Invoke-Command -ComputerName $computerName -ScriptBlock $command
##WSUS doesn't like remote execution... so we have to jump through several hoops to make it think it's executing locally....
if (Test-Path \\$computerName\c$\updates\prepareForReboot.ps1 ) {
## Prepare for reboot is an optional script you can install on the remote server to gracefully shutdown things before reboot.
@ctigeek
ctigeek / Get-DotNetVersion.ps1
Last active December 11, 2015 20:55
Get the versions and SPs on the current machine (default), or an array of machines piped in.
function Get-DotNetVersion {
[cmdletbinding()]
Param(
[Parameter(ValueFromPipeline=$True)] [object] $ComputerName
)
BEGIN {
}
PROCESS {
if (-not $ComputerName) {
@ctigeek
ctigeek / GetDirectoryHash.cs
Created December 13, 2015 13:22
Create a hash for all files in a directory, including file content, name, and path.
public static byte[] GetDirectoryHash(string path)
{
var fileCount = 0;
var fileHasher = SHA1Managed.Create();
var sumHasher = SHA1Managed.Create();
sumHasher.Initialize();
var sw = new Stopwatch();
sw.Start();
foreach (var file in Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories))
@ctigeek
ctigeek / ConcurrentImmutableDictionary.cs
Last active February 1, 2016 19:30
An add-only concurrent dictionary that's lightweight, doesn't require locks on reads. Although it implements IReadOnlyDictionary, you can change values in the dictionary.
//License:
// This code was authored by Steven Swenson (github.com/ctigeek, twitter.com/ctigeek)
// and is released under the MIT license.
// https://opensource.org/licenses/MIT
//
// Use cases for this code:
// You need to have an in-memory dictionary that's only added to, and is typically built when the process spins up.
// You can add items to the dictionary at any time, but it's expensive (i.e. it rebuilds the entire underlying dict) but reads are extrememly fast.
// You can easily augment this code to implement a remove function, but if you're doing lots of adds and removes you're probably better off using a regular ConcurrentDictionary.
// It *does* allow you to modify values. This is slightly misleading since it implements IReadOnlyDictionary.
@ctigeek
ctigeek / config.js
Last active February 2, 2016 20:33
Enable SSL in MongoExpress....
// **snip***
} else {
mongo = {
db: 'db',
host: 'localhost',
password: 'pass',
port: 27017,
url: '"mongodb://localhost:27017/db',
username: 'admin',
ssl: true // <<<--------------Add this....
@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 October 7, 2024 19:46
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 / ISessionExtensions.cs
Created May 18, 2016 19:10
Helper functions for storing data in session.
public static void SetInt64(this ISession session, string key, long lng)
{
var bytes = BitConverter.GetBytes(lng);
session.Set(key, bytes);
}
public static long GetInt64(this ISession session, string key, long defaultIfNull)
{
var bytes = session.Get(key);
if (bytes == null || bytes.Length == 0)
@ctigeek
ctigeek / Send-SlackNotification.ps1
Created May 20, 2016 20:21
Send-SlackNotification (Slack Web RPC API)
function Send-SlackNotification($message) {
$url = "https://slack.com/api/chat.postMessage"
$headers = @{"Content-Type"="application/x-www-form-urlencoded";"User-Agent"="SlackBot"}
$body = @{ token="PUT_YOUR_BOT_KEY_HERE"; channel="PUT_CHANNEL_HERE"; text=$message; as_user="true"; }
##If you are using powershell v5 you can change this to Invoke-RestMethod.
##The UsePasicParsing option is not supported in v4 for Invoke-RestMethod, and you really want that option.
$result = Invoke-WebRequest -Uri $url -Body $body -Headers $headers -Method Post -ErrorAction SilentlyContinue -UseBasicParsing
$result.Content | ConvertFrom-Json
}
@ctigeek
ctigeek / EncryptionAbstract.txt
Created August 31, 2016 22:33
Encryption for Developers
**Encryption doesn't have to be difficult!**
In this pragmatic talk we'll be discussing how to use cryptography in your application.
As an example, we'll walk through how to use various cryptographic technologies to design an end-to-end encrypted chat application.
What kind of encryption do you use?
What are the most common and secure configurations?
And most importantly how do you integrate it into your software?
We'll address all these questions and walk through code samples in c#.
We'll cover symmetrical (AES) and public-key cryptographic algorithms, along with multiple types of hashing,
and various use-cases for each.
Cryptography configuration can sometimes be tricky, but we'll look at the most secure and versatile configurations for each methodology.