Created
September 16, 2021 04:25
-
-
Save illuzian/469764970bc54694cca5c4fa02983557 to your computer and use it in GitHub Desktop.
PowerShell profile with Linux aliases and Proxy Conf Functions
This file contains hidden or 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
$ExecutionPolicy = Get-ExecutionPolicy | |
$DesiredPolicies = @('Bypass', 'Unrestricted') | |
# Becase I always accidently type ip addr on Windows. | |
function ip { | |
if ($args[0] -eq 'addr') { | |
ipconfig /all | |
} if ($args[0] -eq 'route') { | |
route print | |
} | |
} | |
function systemctl { | |
if ($args[0] -eq 'list-units') { | |
Get-Service | |
} | |
} | |
function tail { | |
if ($args[0] -eq '-n') { | |
$TailCount = [int]$args[1] | |
$AdditionalArgs = $args[2..($args.Length)] -Join " " | |
Get-Content -tail $TailCount $AdditionalArgs | |
} else { | |
Get-Content -tail $args | |
} | |
} | |
function more { | |
$AdditionalArgs = $args -Join " " | |
Get-Content $AdditionalArgs | out-host -Paging | |
} | |
function less { | |
$AdditionalArgs = $args -Join " " | |
Get-Content $AdditionalArgs | out-host -Paging | |
} | |
# Because somtimes I ifconfig do this instead | |
Set-Alias -Name ifconfig -Value ipconfig | |
if ($ExecutionPolicy -notin $DesiredPolicies) { | |
try { | |
$(Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope CurrentUser) 2> $null | |
} | |
catch { | |
Write-Host -ForegroundColor 'Yellow' "Failed to set permissive execution policy. Current policy is $ExecutionPolicy." | |
Write-Host -ForegroundColor 'Yellow' "Desired policies were:" | |
$DesiredPolicies | ForEach-Object {Write-Host -ForegroundColor 'Yellow' " • $_"} | |
} | |
} | |
Function Enable-Proxy { | |
param ( | |
$Proxy = 'http://yourdefault' | |
) | |
[system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy($proxy) | |
[system.net.webrequest]::defaultwebproxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials | |
[system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true | |
} | |
Function Disable-Proxy { | |
[system.net.webrequest]::defaultwebproxy = $null | |
} | |
Function Enable-BasicAuth { | |
$RegEditCred = Get-Credential | |
set-itemproperty -Credential $RegEditCred -path HKLM:\SOFTWARE\Policies\Microsoft\Windows\WinRM\Client -name AllowBasic -Value 1 | |
} | |
$TestSaveHistory = { | |
param( | |
[string]$line | |
) | |
$sensitive = ".*password.*|.*asplaintext.*|.*token.*|.*key.*|.*secret.*|^ .*" | |
return ($line -notmatch $sensitive) | |
} | |
Set-PSReadLineOption -AddToHistoryHandler $TestSaveHistory -MaximumHistoryCount 10000 -BellStyle None -HistorySaveStyle SaveIncrementally -HistorySavePath $home\.ps_history | |
Enable-Proxy | |
Write-Host -ForegroundColor 'Red' ' | |
---' | |
Write-Host -ForegroundColor 'Cyan' 'Default proxy was set using "Enable-Proxy". | |
- Enable-Proxy | |
Enables the default proxy using DefaultNetworkCredentials. | |
- Disable-Proxy | |
Turns off the proxy. | |
- Enable-Proxy -Proxy "http://otherproxy" | |
Sets the specified proxy and uses DefaultNetworkCredentials. | |
- Enable-BasicAuth | |
Enables basic auth for cmddlets that require them. | |
To connect to modules that require basic auth (Connect-ExchangeOnline, Connect-Msol, etc.), use cmdlet: Enable-BasicAuth and provide machine admin credentials.' | |
Write-Host -ForegroundColor 'Red' '---' | |
Write-Host -ForegroundColor 'DarkMagenta' 'Reference: | |
- Enable basic auth | |
set-itemproperty -Credential $RegEditCred -path HKLM:\SOFTWARE\Policies\Microsoft\Windows\WinRM\Client -name AllowBasic -Value 1 | |
- Configure session proxy | |
$proxy = "http://yourdefault" | |
[system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy($proxy) | |
[system.net.webrequest]::defaultwebproxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials | |
[system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true' | |
Write-Host -ForegroundColor 'Red' '---' | |
Write-Host -ForegroundColor 'Yellow' 'If WSL gives a permission error, run "Restart-Service vmcompute"' | |
Write-Host -ForegroundColor 'Red' '--- | |
' | |
Set-Alias python3 python | |
Set-Alias pip3 pip | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment