Last active
April 1, 2019 11:57
-
-
Save Halkcyon/3cf99643a26d9feb36ab718302256c66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires -Version 5 | |
<# Lasciate ogni speranza, voi ch'entrate #> | |
using namespace System.Diagnostics | |
using namespace System.DirectoryServices.AccountManagement | |
using namespace System.Management.Automation | |
using namespace System.Net | |
using namespace System.Security.Cryptography.X509Certificates | |
using namespace System.Security.Principal | |
using namespace System.Text | |
using namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatform | |
using namespace Microsoft.PowerShell | |
$VerbosePreference = [ActionPreference]::Continue | |
$ErrorActionPreference = [ActionPreference]::Inquire | |
# Set up HOME | |
Set-Variable -Name HOME -Value "$Env:HOMEDRIVE$Env:HOMEPATH" -Option AllScope -Scope global -Force | |
(Get-PSProvider -PSProvider FileSystem).Home = $HOME | |
function ~ { $HOME | Set-Location } | |
if (Test-Path -Path ~\proxy.txt) { | |
[WebRequest]::DefaultWebProxy = [WebProxy]::new((Get-Content -Path ~\proxy.txt)) | |
[WebRequest]::DefaultWebProxy.Credentials = [CredentialCache]::DefaultNetworkCredentials | |
} | |
<# this proved too slow to run every time | |
if (($n=Find-PackageProvider -Name NuGet).Version -gt (Get-PackageProvider -Name NuGet).Version) { | |
[void]($n | Install-PackageProvider -Scope CurrentUser -Force) | |
} | |
if (($p=Find-PackageProvider -Name PowerShellGet).Version -gt (Get-PackageProvider -Name PowerShellGet).Version) { | |
[void]($p | Install-PackageProvider -Scope CurrentUser -Force) | |
}#> | |
Import-Module -Force -ErrorAction Ignore -Name @( | |
'Microsoft.PowerShell.Archive' | |
'Microsoft.PowerShell.Diagnostics' | |
'Microsoft.PowerShell.Host' | |
'Microsoft.PowerShell.Management' | |
'Microsoft.PowerShell.Security' | |
'Microsoft.PowerShell.Utility' | |
'ActiveDirectory' | |
'PackageManagement' | |
'Pester' | |
'PowerShellGet' | |
'PSScriptAnalyzer' | |
'PSReadline' | |
) -Scope global -DisableNameChecking 4>$null #got tired of -vb output of Pester | |
<# also pretty slow, modules don't update that often | |
Update-Module -AcceptLicense#> | |
Add-Type -Path ~\roslyn\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll | |
Invoke-Expression -Command @' | |
class RoslynCompilerSettings : ICompilerSettings | |
{ | |
[string] get_CompilerFullPath() | |
{ | |
return (Get-Item -Path ~\roslyn\tools\RoslynLatest\csc.exe).FullName | |
} | |
[int] get_CompilerServerTimeToLive() | |
{ | |
return 10 | |
} | |
} | |
'@ | |
$PSDefaultParameterValues = @{ | |
'Disabled' = $false | |
'Add-Type:CodeDomProvider' = [CSharpCodeProvider]::new([RoslynCompilerSettings]::new()) | |
'Add-Type:ReferencedAssemblies' = [hashtable].Assembly.Location | |
} | |
# Accept any certs | |
class TrustAllCertsPolicy : ICertificatePolicy { | |
[bool] CheckValidationResult([ServicePoint] $a, [X509Certificate] $b, [WebRequest] $c, [int] $d) { | |
return $true | |
} | |
} | |
[ServicePointManager]::CertificatePolicy = [TrustAllCertsPolicy]::new() | |
# Support the latest SSL protocols | |
[ServicePointManager]::SecurityProtocol = 'Tls' | |
[SecurityProtocolType].GetEnumValues(). | |
Where({ $_ -gt [Math]::Max([ServicePointManager]::SecurityProtocol.value__, [SecurityProtocolType]::Tls.value__) }). | |
ForEach({ if (-not [ServicePointManager]::SecurityProtocol.HasFlag($_)) { [ServicePointManager]::SecurityProtocol += $_ } }) | |
Add-Type -AssemblyName System.DirectoryServices.AccountManagement | |
function Test-Credential { | |
[OutputType([bool])] | |
param( | |
[Parameter(Position = 0, Mandatory)] | |
[ValidateScript({ $_.UserName.Contains('\') -and ![string]::IsNullOrWhiteSpace($_.GetNetworkCredential().Password) })] | |
[Credential()] | |
[pscredential] | |
$Credential | |
) | |
$d, $u = $Credential.UserName -split '\\' | |
try { | |
$principalContext = [PrincipalContext]::new([ContextType]::Domain, $d) | |
return $principalContext.ValidateCredentials($u, $Credential.GetNetworkCredential().Password) | |
} | |
finally { | |
if ($null -ne $principalContext) { | |
$principalContext.Dispose() | |
} | |
} | |
} | |
function Invoke-Assignment { | |
[Alias('=')] | |
param( | |
[Parameter(ValueFromRemainingArguments)] | |
[psobject[]] $InputObject | |
) | |
function private:eval($item) { | |
if ($null -ne $item) { | |
if ($item -is [scriptblock]) { | |
return &$item | |
} | |
return $item | |
} | |
return $null | |
} | |
## Ternary | |
if ($InputObject.Contains('?') -and $InputObject.Contains(':') -and $InputObject.Count -eq 5) { | |
if (eval($InputObject[0])) { | |
return eval($InputObject[2]) | |
} | |
else { | |
return eval($InputObject[4]) | |
} | |
} | |
## Null-coalescing | |
elseif ($InputObject.Contains('??') -and $InputObject.Count -eq 3) { | |
if ($null -ne ($r = eval($InputObject[0]))) { | |
return $r | |
} | |
else { | |
return eval($InputObject[2]) | |
} | |
} | |
## Bad input | |
else { | |
return $null | |
} | |
} | |
function Send-Note { | |
[OutputType([string[]])] | |
param( | |
[Parameter(Position = 0, Mandatory, ValueFromPipeline)] | |
[string[]] $Note, | |
[int] $Tempo, | |
[switch] $PassThru | |
) | |
begin { | |
$SendNote = [string]::Empty | |
$NoteIndex = @{ | |
'C' = @{ '1' = 32.7; '2' = 65.41; '3' = 130.8; '4' = 261.6; '5' = 523.3; '6' = 1047; '7' = 2093; '8' = 4186 } | |
'C#' = @{ '1' = 34.65; '2' = 69.3; '3' = 138.6; '4' = 277.2; '5' = 554.4; '6' = 1109; '7' = 2217; '8' = 4435 } | |
'D' = @{ '1' = 36.71; '2' = 73.42; '3' = 146.8; '4' = 293.7; '5' = 587.3; '6' = 1175; '7' = 2349; '8' = 4699 } | |
'Eb' = @{ '1' = 38.89; '2' = 77.78; '3' = 155.6; '4' = 311.1; '5' = 622.3; '6' = 1245; '7' = 2489; '8' = 4978 } | |
'E' = @{ '1' = 41.2; '2' = 82.41; '3' = 164.8; '4' = 329.6; '5' = 659.3; '6' = 1319; '7' = 2637; '8' = 5274 } | |
'F' = @{ '1' = 43.65; '2' = 87.31; '3' = 174.6; '4' = 349.2; '5' = 698.5; '6' = 1397; '7' = 2794; '8' = 5588 } | |
'F#' = @{ '1' = 46.25; '2' = 92.5; '3' = 185; '4' = 370; '5' = 740; '6' = 1480; '7' = 2960; '8' = 5920 } | |
'G' = @{ '1' = 49; '2' = 98; '3' = 196; '4' = 392; '5' = 784; '6' = 1568; '7' = 3136; '8' = 6272 } | |
'G#' = @{ '1' = 51.91; '2' = 103.8; '3' = 207.7; '4' = 415.3; '5' = 830.6; '6' = 1661; '7' = 3322; '8' = 6645 } | |
'A' = @{ '1' = 55; '2' = 110; '3' = 220; '4' = 440; '5' = 880; '6' = 1760; '7' = 3520; '8' = 7040 } | |
'Bb' = @{ '1' = 58.27; '2' = 116.5; '3' = 233.1; '4' = 466.2; '5' = 932.3; '6' = 1865; '7' = 3729; '8' = 7459 } | |
'B' = @{ '1' = 61.74; '2' = 123.5; '3' = 246.9; '4' = 493.9; '5' = 987.8; '6' = 1976; '7' = 3951; '8' = 7902 } | |
'R' = '0' | |
} | |
$NoteTypes = @{ | |
'W' = 1600; 'W.' = 2000 | |
'H' = 1000; 'H.' = 1500 | |
'Q' = 400; 'Q.' = 600 | |
'E' = 200; 'E.' = 300 | |
'S' = 100; 'S.' = 150 | |
} | |
} | |
process { | |
if ($Note.Count -eq 1 -and $Note[0].Contains(',')) { | |
foreach ($n in $Note -split ',') { | |
$null = $n -match '(?<Pitch>[A-G][#|b]?|[R])(?<Octave>[0-8])?(?<NoteType>[w|h|q|e|s]\.?)?' | |
$Pitch, $Octave, $NoteType = $matches['Pitch'], $matches['Octave'], $matches['NoteType'] | |
$Frequency = $NoteIndex[$Pitch][$Octave] | |
$Duration = if ($NoteType) { | |
if ($Tempo) { | |
100 / $Tempo * $NoteTypes[$NoteType] | |
} | |
else { | |
$NoteTypes[$NoteType] | |
} | |
} | |
else { | |
if ($Tempo) { | |
100 / $Tempo * 400 | |
} | |
else { | |
400 | |
} | |
} | |
if ($PassThru.IsPresent) { | |
"$Pitch$Octave$NoteType - $Duration - $Frequency" | |
} | |
if ($Pitch -eq 'R' -or $Octave -notin '1', '2', '3', '4', '5', '6', '7', '8') { | |
$SendNote += "Start-Sleep -Milliseconds $Duration;" | |
} | |
else { | |
$SendNote += "[console]::Beep($Frequency, $Duration);" | |
} | |
} | |
} | |
else { | |
$null = $Note -match '(?<Pitch>[A-G][#|b]?|[R])(?<Octave>[0-8])?(?<NoteType>[w|h|q|e|s]\.?)?' | |
$Pitch, $Octave, $NoteType = $matches['Pitch'], $matches['Octave'], $matches['NoteType'] | |
$Frequency = $NoteIndex[$Pitch][$Octave] | |
$Duration = if ($NoteType) { | |
if ($Tempo) { | |
100 / $Tempo * $NoteTypes[$NoteType] | |
} | |
else { | |
$NoteTypes[$NoteType] | |
} | |
} | |
else { | |
if ($Tempo) { | |
100 / $Tempo * 400 | |
} | |
else { | |
400 | |
} | |
} | |
if ($PassThru.IsPresent) { | |
"$Pitch$Octave$NoteType - $Duration - $Frequency" | |
} | |
if ($Pitch -eq 'R' -or $Octave -notin '1', '2', '3', '4', '5', '6', '7', '8') { | |
$SendNote += "Start-Sleep -Milliseconds $Duration;" | |
} | |
else { | |
$SendNote += "[console]::Beep($Frequency, $Duration);" | |
} | |
} | |
} | |
end { | |
for ($i = 0; $i -lt $SendNote.Count; $i++) { | |
Invoke-Expression -Command $SendNote | |
} | |
} | |
} | |
# Set all our encoding to UTF8 | |
Set-Variable -Name OutputEncoding -Value ([UTF8Encoding]::new()) -Option AllScope -Scope global -Force | |
# note: breaks more.com | |
[Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding | |
# Add access to all defined type accelerators | |
[psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators'):: | |
Add('accelerators', [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')) | |
if ($Host.Name -ne 'Visual Studio Code Host') { | |
$Host.UI.RawUI.BackgroundColor = [ConsoleColor]::Black | |
[TokenClassification].GetEnumValues().ForEach({ Set-PSReadlineOption -BackgroundColor Black -TokenKind $_ }) | |
} | |
# cope with encoding; stop using Function:\help | |
Set-Alias -Name man -Value Get-Help -Option AllScope -Scope global -Force | |
~ | |
Clear-Host | |
Write-Information -infa 2 -Msg ('Windows PowerShell v{0}.{1}' -f $PSVersionTable.PSVersion.Major, $PSVersionTable.PSVersion.Minor) | |
function disapprove{-join[char[]](40,3232,95,3232,41)|Set-Clipboard} | |
function smile{[char]::ConvertFromUtf32(0x1F643)|Set-Clipboard} | |
function shrug{'¯\_(ツ)_/¯'} | |
function prompt { | |
$private:stamp = Get-Date -UFormat "`n%Y-%m-%d %H:%M:%S" | |
$private:path = ($PWD.ProviderPath -replace '^H', '~' -replace '\\', '/' -replace '(\w+)?:' -replace '/$') + '/' | |
$private:arrow = '>' * ($NestedPromptLevel + 1) | |
$private:count = ((Get-History).Count + 1).ToString('000') | |
# as of writing, conemu supports VTY, but does not update $Host | |
if ($Host.UI.SupportsVirtualTerminal -or (Get-ChildItem -Path Env:\ConEmu*)) { | |
$e = [char]27 #backwards compatibility | |
$r, $b, $u = "$e[m", "$e[1m", "$e[4m" | |
$yellow = "$e[38;2;148;152;0m" | |
$blue = "$e[38;5;21m" | |
$orange = "$e[38;5;208m" | |
$purple = "$e[38;5;92m" | |
@" | |
$yellow$private:stamp | |
$r/$($PWD.Drive.Name.ToUpper())/ | |
${blue}PS $r$b$u $private:count $r $orange$private:path$purple$private:arrow $r | |
"@ | |
} | |
else { | |
#sad | |
$private:stamp | Write-Host -ForegroundColor DarkYellow | |
"/$($PWD.Drive.Name.ToUpper())/" | Write-Host -ForegroundColor Cyan | |
'PS ' | Write-Host -ForegroundColor Blue -NoNewline | |
"[$private:count] " | Write-Host -NoNewline | |
$private:path | Write-Host -ForegroundColor Gray -NoNewline | |
$private:arrow | Write-Host -ForegroundColor DarkMagenta -NoNewline | |
' ' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment