You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.3 KiB
PowerShell

#region keybindings
if (Get-Module PSReadLine) {
Set-PSReadlineKeyHandler -Key Tab -Function Complete
Set-PSReadlineKeyHandler -Key Ctrl+d -Function ViExit
}
#endregion keybindings
#region prompt
# set part of the prompt beforehand to optimize the prompt function
# 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()
if($identity.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
$delimiter = '#'
} else {
$delimiter = '$'
}
# shorten path with tilde if possible
# uses .Replace() to prevent regex
$homePath = if ($HOME) {
$HOME
} 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 {
param (
[Parameter(Mandatory=$true,
Position=0)]
[string]
$SearchTerm,
[string]
$Server = $env:USERDNSDOMAIN+':3268'
)
$filter = 'UserPrincipalName -like "*{0}*" -or Name -like "*{0}*"' -f $SearchTerm
Get-ADUser -Filter $filter -Server $Server @args
}
function Get-BADGroup {
param (
[Parameter(Mandatory=$true,
Position=0)]
[string]
$SearchTerm,
[string[]]
$Properties = @('Description','ManagedBy'),
[string]
$Server = $env:USERDNSDOMAIN+':3268'
)
$filter = 'Name -like "*{0}*" -or Description -like "*{0}*"' -f $SearchTerm
Get-ADGroup -Filter $filter -Properties $Properties -Server $Server @args
}
function Get-BADComputer {
param (
[Parameter(Mandatory=$true,
Position=0)]
[string]
$SearchTerm,
[string[]]
$Properties = @('Description','ManagedBy'),
[string]
$Server = $env:USERDNSDOMAIN+':3268'
)
$filter = 'Name -like "*{0}*" -or Description -like "*{0}*"' -f $SearchTerm
Get-ADComputer -Filter $filter -Properties $Properties -Server $Server @args
}
function Get-BADPrincipalGroupMembership {
param (
[Parameter(ValueFromPipeline)]
[Microsoft.ActiveDirectory.Management.ADAccount]
$ADObject
)
process {
Write-Host $ADObject
$server = $ADObject.DistinguishedName.Split(',DC=')[-2,-1] -join '.'
$ADObject | Get-ADPrincipalGroupMembership -Server $server
}
}
function Get-Feierabend {
param (
[Parameter(Position = 0)]
[datetime]
$Begin,
[Parameter(Position = 1)]
[datetime]
$PauseStart,
[Parameter(Position = 2)]
[datetime]
$PauseEnd,
[Parameter(Position = 3)]
[single]
$Hours = 6.5,
[Parameter(Position = 4)]
[single]
$Minutes = 30
)
$Begin.AddHours($Hours).AddMinutes($Minutes).Add($PauseEnd - $PauseStart)
}
function wsh {
param (
[Parameter(Mandatory, Position = 0)]
[String]
$ComputerName
)
$host.UI.RawUI.WindowTitle = $ComputerName
$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 $env:USERPROFILE}
}
Enter-PSSession $session
}
function watch {
param (
[int]$n = 2,
[Parameter(Mandatory,
Position = 0)]
$ScriptBlock
)
while ($true) {
$o = Invoke-Command -ScriptBlock $ScriptBlock
Clear-Host
'Watching {'+$ScriptBlock+'} '+(Get-Date)
''
$o
Start-Sleep -Seconds $n
}
}