Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Last active August 16, 2017 16:43
Show Gist options
  • Save matthewjberger/04b24019248879dcafcddfb8c8063c53 to your computer and use it in GitHub Desktop.
Save matthewjberger/04b24019248879dcafcddfb8c8063c53 to your computer and use it in GitHub Desktop.
A collection of various powershell scripts
[Reflection.Assembly]::LoadWithPartialName("System.Security");cls;$salt="SaltCrypto";$init="IV_Password";$e='JsFzFEpihszpJTSypy5CDslN8NfDHe7ew+GFo3g7sUU=';$k='a243asrw';if($e -is[string]){$e=[Convert]::FromBase64String($e)};$r=new-Object System.Security.Cryptography.RijndaelManaged;$pass=[Text.Encoding]::UTF8.GetBytes($k);$salt=[Text.Encoding]::UTF8.GetBytes($salt);$r.Key=(new-Object Security.Cryptography.PasswordDeriveBytes $pass,$salt,"SHA1",5).GetBytes(32);$r.IV=(new-Object Security.Cryptography.SHA1Managed).ComputeHash([Text.Encoding]::UTF8.GetBytes($init))[0..15];$d=$r.CreateDecryptor();$ms=new-Object IO.MemoryStream @(,$e);$cs=new-Object Security.Cryptography.CryptoStream $ms,$d,"Read";$sr=new-Object IO.StreamReader $cs;Write-Output $sr.ReadToEnd();$sr.Close();$cs.Close();$ms.Close();$r.Clear();
# Load security dll
[Reflection.Assembly]::LoadWithPartialName("System.Security");
# Clear the screen
cls;
# Salt. Added to make a common password uncommon, even though ours is a jumbled mess of letters on purpose.
$salt="SaltCrypto";
# Password to use
$init="IV_Password";
# Base-64 Encrypted string
$e='JsFzFEpihszpJTSypy5CDslN8NfDHe7ew+GFo3g7sUU=';
$k='a243asrw'; # The key we used when encrypting our secret message
if($e -is[string]) { # Fluffy padding, we already know it's a string ;)
$e=[Convert]::FromBase64String($e)
};
# Use RijndaelManaged as the crypto type
# https://blogs.msdn.microsoft.com/shawnfa/2006/10/09/the-differences-between-rijndael-and-aes/
# This only works with 128-bit AES standard strings, anything higher (192, 256, etc) won't work. This is pretty common to use.
$r=new-Object System.Security.Cryptography.RijndaelManaged;
# Set the password using our key
$pass=[Text.Encoding]::UTF8.GetBytes($k);
# Set the salt to use
$salt=[Text.Encoding]::UTF8.GetBytes($salt);
# This 'derives a key from a password using an extension of the PBKDF1 algorithm'
# https://msdn.microsoft.com/en-us/library/system.security.cryptography.passwordderivebytes(v=vs.110).aspx
# http://crypto.stackexchange.com/questions/1842/how-does-pbkdf1-work
# Microsoft does the hard work here.
# We pass along our password, salt, use SHA1 hashing, and save the result as the Rijndael Managed key as 32 bytes.
#
# The PBKDF1 algorithm 'calculates a hash of the password, concatenated with salt, and then hashes the hash value returned by the previous step iteration count minus one times.'
$r.Key=(new-Object Security.Cryptography.PasswordDeriveBytes $pass,$salt,"SHA1",5).GetBytes(32);
# IV stands for initialization vector. Rijndael Managed only requires an IV and a Key.
$r.IV=(new-Object Security.Cryptography.SHA1Managed).ComputeHash([Text.Encoding]::UTF8.GetBytes($init))[0..15];
# Create a decryptor so we can decrypt our string
$d=$r.CreateDecryptor();
# Take our encrypted string and make a memory stream
$ms=new-Object IO.MemoryStream @(,$e);
# Make a crypto stream that reads in the memory stream containing our encrypted string
# This will decrypt it as well.
$cs=new-Object Security.Cryptography.CryptoStream $ms,$d,"Read";
# Make a stream reader to read our crypto stream
$sr=new-Object IO.StreamReader $cs;
# Write the output from
Write-Output $sr.ReadToEnd();
# Close all the streams
$sr.Close();
$cs.Close();
$ms.Close();
# Clear the Rijndael Managed object
$r.Clear();
# Displays all installed truetype font names in their corresponding font in a browser
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$objFonts = New-Object System.Drawing.Text.InstalledFontCollection
$colFonts = $objFonts.Families
foreach ($objFont in $colFonts)
{
$strHTML = $strHTML + "<font size='5' face='" + $objFont.Name + "'>" + $objFont.Name + "</font><br>"
}
$strHTML | Out-File output.html
Invoke-Expression ./output.html
# This will send the last command entered in the powershell terminal to the clipboard for pasting
(Get-History)[-1].CommandLine | clip
# long version
Get-ChildItem -Recurse *.* | Select-String -Pattern "foobar" | Select-Object -Unique Path
# short version using aliases
dir -recurse *.* | sls -pattern "foobar" | select -unique path
(New-Object System.Drawing.Text.InstalledFontCollection).Families
Function ToMeme($a){$b=""; foreach($c in $a.tochararray()){$b="$b $c"}; return $b.Trim(' ');}
# Fuzzy file search installed programs
# including programs installed through the scoop package manager.
# Usage:
# open (no arguments)
# open -a (call program and enter arguments)
param ([switch]$a = $false)
$scoopApplications = gci $env:USERPROFILE\scoop\shims\*.ps1 | %{$_.FullName}
$installedApplications = Get-Command -Type Application | select path
$files = @($scoopApplications) + $installedApplications
$path = $files | fzf
if(!$path) {Exit}
$args = if($a){Read-Host "Enter Arguments"}else{""}
& $path $args
function PigL ($a) {$a = $a -replace "[^\w\s]",""; $b = $a.split(" ") | %{"a" + $_.substring(1) + $_[0].ToString().tolower() + "ay" }; [string]::Join(" ", $b) | Out-String }
function DecryptPigL ($a) {$b = $a.split(" ") | %{$_[$_.Length-3] + $_.Substring(1, $_.Length-4) }; [string]::Join(" ", $b) | Out-String }
gci -Recurse -ErrorAction silentlyContinue | ? { $_.Name -like "*ComLinkLogFile*" } | %{ $_.Name }
$find = 'jquery-1\.4\.4'
$replace = 'jquery-1\.5\.1'
$match = '*.cshtml' , '*.vbhtml'
$preview = $true
foreach ($sc in dir -recurse -include $match | where { test-path $_.fullname -pathtype leaf} ) {
select-string -path $sc -pattern $find
if (!$preview) {
(get-content $sc) | foreach-object { $_ -replace $find, $replace } | set-content $sc
}
}
gci | %{
$_.Name -match '-(.*)-';
$c = [regex]::Match( $matches[1].trim(), '[sS](\d\d)[eE](\d\d)').captures;
$s = $c.groups[1].value;
$e = $c.groups[2].value;
$a="[$s x $e]" -replace '\s','';
$_.Name -replace '-(.*)-',
$a}
# Input: Corner Gas - s04e01 - Title
# Output: Corner Gas [04x01] - Title
<# types
"ok",
"error",
"warning",
"question",
"info",
"okcancel",
"abortretryignore",
"yesnocancel",
"yesno",
"retrycancel",
"canceltryagaincontinue"
#>
<# buttons
0: OK
1: OK Cancel
2: Abort Retry Ignore
3: Yes No Cancel
4: Yes No
5: Retry Cancel
#>
# Example Usage:
# ShowMessage "Test Message" "My Title" "yesnocancel"
function ShowMessage($msg, $caption="Notice", $type="info", $buttons=-1)
{
$wshell=New-Object -comObject Wscript.Shell
$typeInt = 0
switch($buttons)
{
0 { $type = "ok" }
1 { $type = "okcancel" }
2 { $type = "abortretryignore" }
3 { $type = "yesnocancel" }
4 { $type = "yesno" }
5 { $type = "retrycancel" }
}
switch($type)
{
"error" {$typeInt = 16} # 'Stop'
"warning" {$typeInt = 48} # 'Exclamation'
"question" {$typeInt = 32} # 'Question'
"info" {$typeInt = 64} # 'Information'
"ok" {$typeInt = 0} # 'Ok'
"okcancel" {$typeInt = 1} # 'Ok Cancel'
"abortretryignore" {$typeInt = 2} # 'Abort Retry Ignore'
"yesnocancel" {$typeInt = 3} # 'Yes No Cancel'
"yesno" {$typeInt = 4} # 'Yes No'
"retrycancel" {$typeInt = 5} # 'Retry Cancel'
"canceltryagaincontinue" {$typeInt = 6} # 'Cancel Try-Again Continue'
default {$icon = 0} # 'No icon'
}
# 0 for the second parameter (duration) here means to stay up until closed (i.e. 'infinite' duration)
$answer = $wshell.Popup($msg,0,$caption, $typeInt)
$returnValue = ""
switch($answer)
{
1 { $returnValue = "ok" }
2 { $returnValue = "cancel" }
3 { $returnValue = "abort" }
4 { $returnValue = "retry" }
5 { $returnValue = "ignore" }
6 { $returnValue = "yes" }
7 { $returnValue = "no" }
10 { $returnValue = "tryagain" }
}
return $returnValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment