Better Powershell integration

This commit is contained in:
Jeremy Wall 2021-10-28 21:51:17 -04:00
parent 6c1dd033b6
commit e149d95a25
2 changed files with 72 additions and 24 deletions

View File

@ -14,22 +14,22 @@ function prompt {
Invoke a scriptblock with evelvated permissions.
#>
function Invoke-Elevated {
param (
[ScriptBlock]
$code
)
$outTmpFile = [System.IO.Path]::GetTempFileName()
$errTmpFile = [System.IO.Path]::GetTempFileName()
param (
[ScriptBlock]
$code
)
$outTmpFile = [System.IO.Path]::GetTempFileName()
$errTmpFile = [System.IO.Path]::GetTempFileName()
$code | Write-Output
$outTmpFile | Write-Output
$errTmpFile | Write-Output
# TODO(jwall): Work on figuring out how to properly redirect output here
Start-Process powershell.exe -ArgumentList { &$code 1>$outTmpFile 2>$errTmpFile } -WindowStyle hidden -Verb RunAs
Get-Content $outTmpFile | Write-Output
Get-Content $errTmpFile | Write-Output
Remove-Item $outTmpFile
Remove-Item $errTmpFile
$code | Write-Output
$outTmpFile | Write-Output
$errTmpFile | Write-Output
# TODO(jwall): Work on figuring out how to properly redirect output here
Start-Process powershell.exe -ArgumentList { &$code 1>$outTmpFile 2>$errTmpFile } -WindowStyle hidden -Verb RunAs
Get-Content $outTmpFile | Write-Output
Get-Content $errTmpFile | Write-Output
Remove-Item $outTmpFile
Remove-Item $errTmpFile
}
<#
@ -38,7 +38,7 @@ function Invoke-Elevated {
#>
function Get-Elevated {
([Security.Principal.WindowsPrincipal] `
[Security.Principal.WindowsIdentity]::GetCurrent() `
[Security.Principal.WindowsIdentity]::GetCurrent() `
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
@ -59,11 +59,11 @@ function Get-Elevated {
#>
function New-Link {
param (
[Parameter(Position=0, Mandatory=$true)]
[Parameter(Position = 0, Mandatory = $true)]
[string[]]
$Source,
[Parameter(Position=1, Mandatory=$true)]
[Parameter(Position = 1, Mandatory = $true)]
[string[]]
$Destination
)
@ -72,7 +72,8 @@ function New-Link {
# to do symbolic links without administrator privileges
if (Get-Elevated) {
New-Item -ItemType SymbolicLink -Path $Destination -Target $Source
} else {
}
else {
# Elevate our permissions for this first.
Invoke-Elevated {
New-Item -ItemType SymbolicLink -Path $Destination -Target $Source
@ -80,8 +81,54 @@ function New-Link {
}
}
<#
.Synopsis
Get Powershell Version and optionally the host version.
.Parameter Full
Show all the version information not just the version
number and edition.
.Parameter WithHost
Show the Host version as well.
#>
function Get-PSVersion {
param (
[Parameter(Mandatory = $false)]
[Switch] $Full,
[Parameter(Mandatory = $false)]
[Switch] $WithHost
)
if ($Full) {
$PSVersionTable |
if ($WithHost) {
Get-Host
}
}
else {
@{ PSVersion = $PSVersionTable.PSVersion; PSEdition = $PSVersionTable.PSEdition }
if ($WithHost) {
$HostInfo = Get-Host
@{HostName = $HostInfo.Name; HostVersion = $HostInfo.Version }
}
}
return $output
}
function Get-Base64-Decoded {
[CmdletBinding()]
param (
[Parameter()]
[string[]]
$encoded
)
[Text.Encoding]::Utf8.GetString([Convert]::FromBase64String($encoded))
}
function grep {
$input | out-string -stream | select-string $args
$input | out-string -stream | select-string $args
}
# It's way more useful to call this open than ii
@ -90,4 +137,5 @@ Set-Alias -Name ln -Value New-Link
Set-Alias -name which -Value Get-Command
# Add make to our powershell path
$env:PATH="$env:PATH;C:\Program Files (x86)\GnuWin32\bin;C:\Users\jerem\AppData\Local\Julia-1.3.0\bin"
$env:PATH = "$env:PATH;C:\Program Files (x86)\GnuWin32\bin;C:\Users\jerem\AppData\Local\Julia-1.3.0\bin;C:\Users\jerem\AppData\Roaming\nvm;C:\Users\jerem\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin"
$env:NVM_HOME = "C:\Users\jerem\AppData\Roaming\nvm"

View File

@ -1,15 +1,15 @@
#Powershell based windows 10 bootstrap script
# First we source in our powershell profile so we can have some useful utilities
source "./PowerShellProfile.ps1"
. "./PowerShellProfile.ps1"
# First step is to overwrite our current profile with a symlink to the correct one.
Remove-Item -Force -Path $profile
Make-Link -Source "./PowerShellProfile.ps1" -Destination $profile
New-Item -ItemType SymbolicLink -Target "./PowerShellProfile.ps1" -Path $profile
# Now ensure our dnsservers are properly set up
Invoke-Elevated {
Get-NetAdapter | ForEach-Object {
Set-DnsClientServerAddress -InterfaceIndex $_.ifIndex -ServerAddresses 1.1.1.1,1.0.0.1,2606:4700:4700::1111,2606:4700:4700::1001
Set-DnsClientServerAddress -InterfaceIndex $_.ifIndex -ServerAddresses 1.1.1.1, 1.0.0.1, 2606:4700:4700::1111, 2606:4700:4700::1001
}
}