Skip to content

Instantly share code, notes, and snippets.

@JFFail
JFFail / NumberClass.ps1
Created April 30, 2016 13:55
Example of how to overload operators with classes in PowerShell.
class Number {
[int] $value = 0
Number() {
#Does nothing.
}
Number([int]$somethingElse) {
$this.value = $somethingElse
}
@JFFail
JFFail / count_days.sh
Last active July 31, 2020 22:08
Solution to Reddit Daily Programmer 201
#!/usr/pkg/bin/zsh
dateMath() {
todayMonth=$(date "+%m/%d/%y" | awk -F\/ '{print $1}' | sed "s/^0//")
todayDay=$(date "+%m/%d/%y" | awk -F\/ '{print $2}' | sed "s/^0//")
todayYear=$(date "+%m/%d/%y" | awk -F\/ '{print $3}' | sed "s/^0//")
today="$todayMonth/$todayDay/$todayYear"
current=$(date -d $today +%s)
submitted=$(date -d $1 +%s)
diffDays=$((($submitted - $current) / 86400))
echo "You must wait $diffDays days from $today to $1"
@JFFail
JFFail / port-percent.sh
Last active July 31, 2020 22:08
Script to repeatedly check port availability on an endpoint. Takes server name and a port number as parameters.
#!/bin/bash
if [[ $# -ne 2 ]]; then
echo "Please submit 2 arguments: server name and port number"
else
total=0
success=0
while true; do
total=$((total+1))
result=$(nmap -Pn -p $2 $1 | grep "/tcp")
echo -n $result
@JFFail
JFFail / hack-fallout.ps1
Last active July 31, 2020 22:08
Intermediate Reddit Daily Programmer 238 - Fallout Hacking Minigame
#https://www.reddit.com/r/dailyprogrammer/comments/3qjnil/20151028_challenge_238_intermediate_fallout/
#Reddit Daily Programmer 238 - Intermediate
#Get the desired difficulty from the user.
$needDifficulty = $true
while($needDifficulty) {
Write-Host "How difficult would you like the game to be?"
$difficulty = Read-Host "Enter a value (1 - 5)"
#Validate the input by making sure it's a number.
@JFFail
JFFail / Export-ADContacts.ps1
Created June 1, 2019 21:03
Sample of getting required properties from an AD object and storing a multi-value attribute properly in a .csv file.
# Get the objects with the required attributes and storing proxyAddresses properly in the .csv file.
Get-ADObject -Filter { objectClass -eq "contact" } -Server myserver.mydomain.net -SearchBase "OU=Contacts,DC=mydomain,DC=net" -SearchScope OneLevel -Properties name,givenName,sn,displayName,telephoneNumber,proxyAddresses,targetAddress,mail,mailNickname,company,department,l,physicalDeliveryOfficeName,postalCode,st,streetAddress,title | Select-Object name,givenName,sn,displayName,telephoneNumber,targetAddress,mail,mailNickname,company,department,l,physicalDeliveryOfficeName,postalCode,st,streetAddress,title,@{name="proxyAddresses"; expression={$_.proxyAddresses -join ";"}} | Export-Csv -Path .\sample.csv -Encoding ASCII -Append -NoClobber -NoTypeInformation
# Shows how to then use the proxyAddresses attribute in the future.
$allContacts = Import-Csv -Path .\sample.csv
foreach($singleContact in $allContacts) {
$proxyAddresses = $singleContact.proxyAddresses.Split(";")
}
@JFFail
JFFail / parse_pstn_usage.py
Created April 28, 2020 22:34
Parses a monthly PSTN usage report from Office 365 to report on the minutes consumed per destination phone number for dial-out.
import csv
usage_tracker = {}
report_file = "./pstn_report.csv"
with open(report_file, "r", newline="") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
@JFFail
JFFail / quarantine_days.sh
Last active July 30, 2020 21:12
Simple Bash script to calculate the number of days in quarantine.
#!/bin/bash
# Print help if it was requested.
if [[ $1 == "-h" || $1 == "-?" ]]; then
printf "quarantine_days - Provide a date in the format of YYYY/MM/DD\n"
printf "\tIf the date is valid, the number of days in quarantine will be printed.\n"
printf "\tNon-valid dates will cause the script to exit.\n"
exit
fi
# Make sure something was passed.
@JFFail
JFFail / monthStartUTC.groovy
Created June 5, 2020 20:39
Get a Date object at a specified Date and Time as an ISO formatted string in UTC
TimeZone.setDefault(TimeZone.getTimeZone('UTC'))
Integer year = Calendar.getInstance().get(Calendar.YEAR)
Integer month = Calendar.getInstance().get(Calendar.MONTH)
def then = new GregorianCalendar(year, month, 1, 0, 0, 0).time
// Desired format: 2020-06-01T00:00:00.000000+00:00
thenISO = then.format("yyyy-MM-dd'T'HH:mm:ss.SSSZ", TimeZone.getTimeZone("UTC"))
thenISOFixed = thenISO[0..thenISO.length() - 3] + ":00"
println thenISOFixed
@JFFail
JFFail / chessboard.js
Created May 11, 2020 23:59
Simple chessboard-generating script from Eloquent JavaScript Chapter 2.
var size = 15, base = "";
for(var i = 1; i <= size; i++) {
if(i % 2 === 1) {
for(var k = 1; k <= size; k++) {
if(k % 2 === 1) {
base += "#";
} else {
base += " ";
}
@JFFail
JFFail / GarageDoor.cs
Created April 26, 2016 00:18
Reddit Daily Programmer #260 - Garage Door Opener
//Reddit Daily Programmer #260 - Easy
//https://www.reddit.com/r/dailyprogrammer/comments/4cb7eh/20160328_challenge_260_easy_garage_door_opener/
using System;
namespace DoorOpener
{
class DoorOpener
{
//Create an enumerated set for my sanity in figuring the state of the door.
enum DoorState { open, closed, opening, closing, stoppedOpening, stoppedClosing, emergencyOpening, openBlocked };