Skip to content

Instantly share code, notes, and snippets.

@royashbrook
royashbrook / RandomJS.js
Created May 16, 2020
Random Javascript Things
View RandomJS.js
//some utility functions.
const tagwrapper = (tag,txt,att='') => `<${tag}${att.length===0?'':' ' + att}>${txt}</${tag}>`;
const htmlhelpers = ['h4','table','thead','tbody','tr','td','th','div']; //etc
for(const s of htmlhelpers){eval(`var ${s} = (txt,att='')=>tagwrapper('${s}',txt,att);`)}
const groupByArr = (x, key) => x.reduce((rv, x)=> { (rv[x[key]] = rv[x[key]] || []).push(x); return rv; }, []);
const groupByObj = (x, key) => x.reduce((rv, x)=> { (rv[x[key]] = rv[x[key]] || []).push(x); return rv; }, {});
const setElementHTML = (id,html) => {
const ctx = $(id)
ctx.empty();
ctx.html(html)
View bcpCSV.ps1
function l($t){
"{0}`t-`t{1}" -f (Get-Date), $t
}
function bcpCsv($f,$t,$b,$cs)
{
l "Importing file $f to table $t using batchsize $b and connection $cs"
l "creating datatable using target table schema"
$dt = (getData "select top 0 * from $t" $cs)
$cols = $dt.columns.columnname
View filewatcher.ps1
# references:
# https://community.spiceworks.com/topic/380201-using-powershell-to-check-for-new-files-in-a-directory
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/register-objectevent
# https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher
# you can change $p to whatever path you want, you can also add file filters, and add more events
# i am monitoring a folder for all create/delete events and color coding output
$p = $pwd.path
$w = New-Object System.IO.FileSystemWatcher $p
$w.IncludeSubdirectories = $true
@royashbrook
royashbrook / sna.md
Last active May 3, 2018
service now helpers
View sna.md

service now helper scripts for automating some common tasks in the browser as bookmarklets

these are only tested and used with chrome currently

View dir.txt
/
--/assets
----/page1
------20180101-Some Document Title-t0.pdf
------20180102-Some Document Title-t0.pdf
------20180104-Some Document Title-t1.pdf
page1.md
View pizero pihole setup.sh
# note that this is not a shell script that is meant to be run, just a list of shell commands mostly with comments.
# I am setting this up on my mac so all commands will be relative to that
# 1. Setup the piholes as usual, download stretch lite, image sdcard with etcher
# start with the following link if you forgot:
# https://www.raspberrypi.org/downloads/raspbian/
# 2. enable ssh
touch /Volumes/boot/ssh
View perfmon-cli.ps1
$a = "server"
$b = "\Processor(_Total)\% Processor Time"
Get-Counter -com $a -cou $b -s 1 -con | %{
$cv = [math]::round($_[0].CounterSamples.CookedValue)
Write-Progress -Activity $a `
-PercentComplete $cv `
-CurrentOperation "$b - $cv%" `
-Status "Measuring..."
}
View Get-WindowsUpdates.ps1
$sb = {
(New-Object -ComObject Microsoft.Update.Session).CreateUpdateSearcher() | %{
$_.QueryHistory(0, $_.GetTotalHistoryCount()) |
? resultcode -in 2,3 |
? operation -eq 1 |
select title,date -unique |
%{ [pscustomobject]$_ }
}
}
$s = "s1,s2,s3,s4".split(',')
@royashbrook
royashbrook / config.xml
Created Mar 24, 2018
Send-MailMessage Bug
View config.xml
<?xml version="1.0"?>
<settings>
<mail>
<from>from@gmail.com</from>
<to>to@gmail.com</to>
<subject>Test</subject>
<smtp>smtp.gmail.com</smtp>
<port>587</port>
<user>from@gmail.com</user>
<pass>frompassword</pass>
View select-object-with-max-value.ps1
# all files
$a = Get-Item *.txt
# prefix of latest file
$b = $a |
sort -Property LastWriteTime -Descending |
select -First 1 @{n='dts';e={$_.name.split('_')[0]}}).dts
# files with that prefix
Get-Item "$b*.txt"
You can’t perform that action at this time.