implement wsh

also make other parts of profile.ps1 wsh compatible
master
lub 1 year ago
parent 8336f9bddd
commit b1015a15ef

@ -1,12 +1,16 @@
#region keybindings #region keybindings
if (Get-Module PSReadLine) {
Set-PSReadlineKeyHandler -Key Tab -Function Complete Set-PSReadlineKeyHandler -Key Tab -Function Complete
Set-PSReadlineKeyHandler -Key Ctrl+d -Function ViExit Set-PSReadlineKeyHandler -Key Ctrl+d -Function ViExit
}
#endregion keybindings #endregion keybindings
#region prompt dependencies #region prompt
# set part of the prompt beforehand to optimize the prompt function # set part of the prompt beforehand to optimize the prompt function
# depending on elevated permissions change the delimiter # depending on elevated permissions change the delimiter
# prefix variables with random strings to avaid collissions
function prompt {
$identity = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() $identity = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
if($identity.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { if($identity.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
$delimiter = '#' $delimiter = '#'
@ -14,16 +18,19 @@ if($identity.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
$delimiter = '$' $delimiter = '$'
} }
$username = $env:USERNAME.ToLower()
$computername = $env:COMPUTERNAME.ToLower()
#endregion prompt dependencies
function prompt {
# shorten path with tilde if possible # shorten path with tilde if possible
# uses .Replace() to prevent regex # uses .Replace() to prevent regex
$cwd = (Get-Location).Path.Replace($HOME,'~') $homePath = if ($HOME) {
$HOME
'[{0}@{1} {2}]{3} ' -f $username,$computername,$cwd,$delimiter } elseif ($env:USERPROFILE) {
$env:USERPROFILE
} }
$cwd = (Get-Location).Path.Replace($homePath,'~')
'[{0}@{1} {2}]{3} ' -f $env:USERNAME.ToLower(),$env:COMPUTERNAME.ToLower(),$cwd,$delimiter
}
#endregion prompt
function Get-BADUser { function Get-BADUser {
param ( param (
[Parameter(Mandatory=$true, [Parameter(Mandatory=$true,
@ -114,14 +121,30 @@ function Get-Feierabend {
$Begin.AddHours($Hours).AddMinutes($Minutes).Add($PauseEnd - $PauseStart) $Begin.AddHours($Hours).AddMinutes($Minutes).Add($PauseEnd - $PauseStart)
} }
function fsh { function wsh {
param ( param (
[string]$ComputerName [Parameter(Mandatory, Position = 0)]
[String]
$ComputerName
) )
$host.UI.RawUI.WindowTitle = $ComputerName $host.UI.RawUI.WindowTitle = $ComputerName
Enter-PSSession @PSBoundParameters @args $profileContent = (
(
$PROFILE | Get-Member | Where-Object MemberType -eq 'NoteProperty' | ForEach-Object {
Get-Content $PROFILE.($_.Name) -ErrorAction SilentlyContinue
}
) | Where-Object {$_ -notmatch '^(#|$)'}
) -join "`n"
$scriptBlock = [scriptblock]::Create($profileContent)
if (!($session = Get-PSSession -ComputerName $ComputerName -Name 'wsh*' -ErrorAction SilentlyContinue | Where-Object Availability -eq 'Available' | Select-Object -First 1)) {
$session = New-PSSession -ComputerName $ComputerName -Name ('wsh'+(Get-Random))
Invoke-Command -Session $session -Command $scriptBlock
Invoke-Command -Session $session -Command {Set-Location ~}
}
Enter-PSSession $session
} }
function watch { function watch {
param ( param (

Loading…
Cancel
Save