Skip to content

Instantly share code, notes, and snippets.

@ZeekoZhu
Created July 18, 2018 18:15
Show Gist options
  • Save ZeekoZhu/a04641b437e49d2a247f65cb7d3da743 to your computer and use it in GitHub Desktop.
Save ZeekoZhu/a04641b437e49d2a247f65cb7d3da743 to your computer and use it in GitHub Desktop.
#http://jongurgul.com/blog/get-stringhash-get-filehash/
Function FixEncoding ([parameter(ValueFromPipeline)][String] $string) {
$utf8 = [System.Text.Encoding]::UTF8
$iso88591 = [System.Text.Encoding]::GetEncoding(28591)
$wrongBytes = $utf8.GetBytes($string)
$rightBytes = [System.Text.Encoding]::Convert($utf8, $iso88591, $wrongBytes)
$utf8.GetString($rightBytes)
}
Function TryConnectWifi($wlanProfile) {
$res = Invoke-Command -ScriptBlock {netsh wlan connect $wlanProfile}
-not ($res -match $wlanProfile)
}
Function HduLogin() {
# get cookie
$username = 'hdu_student_no'
$passwd = 'p@55w0rd'
$_ = Invoke-WebRequest "http://2.2.2.2/ac_portal/default/pc.html?tabs=pwd" -SessionVariable s
$loginPost = Invoke-WebRequest "http://2.2.2.2/ac_portal/login.php" `
-Method Post -WebSession $s `
-Headers @{'Content-Type' = 'application/x-www-form-urlencoded; charset=UTF-8'} `
-Body "opr=pwdLogin&userName=$username&pwd=$passwd&rememberPwd=0"
$utf8 = [System.Text.Encoding]::UTF8
$iso88591 = [System.Text.Encoding]::GetEncoding(28591)
$wrongBytes = $utf8.GetBytes($loginPost.Content)
$rightBytes = [System.Text.Encoding]::Convert($utf8, $iso88591, $wrongBytes)
$result = fixEncoding $loginPost.Content
Write-Host ($result | ConvertFrom-Json).msg
}
Function Get-StringHash([String] $String, $HashName = "MD5") {
$StringBuilder = New-Object System.Text.StringBuilder
[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))| % {
[Void]$StringBuilder.Append($_.ToString("x2"))
}
$StringBuilder.ToString()
}
function prompt {
$lastSuccess = $?
$lastCmd = Get-History -Count 1
$lastExecTime = $lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime
$path = Split-Path -leaf -path (Get-Location)
# $Date = Get-Date
$userName = (git config user.name)
if ($userName -eq $null) {
$userName = $env:USERNAME
}
Write-Host
# Write-Host ":) " -NoNewline -ForegroundColor Yellow
Write-Host $userName -NoNewline -ForegroundColor Cyan
Write-Host " in " -NoNewline
Write-Host $path -NoNewline -ForegroundColor Green
$realLASTEXITCODE = $LASTEXITCODE
Write-VcsStatus
$global:LASTEXITCODE = $realLASTEXITCODE
# Write last command exeution
$timingColor = [System.ConsoleColor]::Green
$execStatus = '✓'
if ( -not $lastSuccess) {
$execStatus = '✗'
$timingColor = [System.ConsoleColor]::Red
}
$originColor = $host.ui.RawUI.ForegroundColor
$execMsg = " $execStatus $($lastExecTime.TotalMilliseconds)ms"
$startposx = $Host.UI.RawUI.windowsize.width - $execMsg.length
$startposy = $Host.UI.RawUI.CursorPosition.Y
$Host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates $startposx, $startposy
$host.UI.RawUI.ForegroundColor = $timingColor
$Host.UI.Write($execMsg)
$host.UI.RawUI.ForegroundColor = $originColor
Write-Host ">" -NoNewline -ForegroundColor Yellow
return " "
}
if ($host.Name -eq 'ConsoleHost') {
Import-Module PSReadline
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
# save input to history without execute
Set-PSReadlineKeyHandler -Key Alt+w `
-BriefDescription SaveInHistory `
-LongDescription "Save current line in history but do not execute" `
-ScriptBlock {
param($key, $arg)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
[Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($line)
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
}
# wrap current line or selection with '('
Set-PSReadlineKeyHandler -Key 'Alt+(' `
-BriefDescription ParenthesizeSelection `
-LongDescription "Put parenthesis around the selection or entire line and move the cursor to after the closing parenthesis" `
-ScriptBlock {
param($key, $arg)
$selectionStart = $null
$selectionLength = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$selectionStart, [ref]$selectionLength)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
if ($selectionStart -ne -1) {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace($selectionStart, $selectionLength, '(' + $line.SubString($selectionStart, $selectionLength) + ')')
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($selectionStart + $selectionLength + 2)
}
else {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $line.Length, '(' + $line + ')')
[Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
}
}
}
Import-Module Pscx -Prefix Pscx
Set-Alias suse openSUSE-42.exe
Set-Alias z goto
Import-Module posh-docker
Import-Module posh-git
Set-Alias ssh-agent "C:\Program Files\Git\usr\bin\ssh-agent.exe"
Set-Alias ssh-add "C:\Program Files\Git\usr\bin\ssh-add.exe"
Start-SshAgent -Quiet
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment