Skip to content

Instantly share code, notes, and snippets.

@belotn
belotn / Set-DirShortcut.ps1
Created July 25, 2022 14:10
Set alt+j alt+k alt+l alt+h to move in directory, the both first one in directory structure, the both last in the directory history
Set-PSReadLineKeyHandler -Chord "Alt+k" -ViMode Insert `
-ScriptBlock {
param($Key, $Arg)
$Level = 1
[Microsoft.PowerShell.PSConsoleReadLine]::TryGetArgAsInt($Arg, `
[ref]$Level,1)
Set-Location -Path (( 1..$Level |% { '..'}) -join '\') } `
-Description "Move to Parent Directory"
Set-PSReadLineKeyHandler -Chord "Alt+k" -ViMode Command `
-ScriptBlock {
@belotn
belotn / VIMBinding.ps1
Last active April 18, 2023 04:51
KeyHandler and function for some VI text manipulation ci, ca, di and da in #Powershell #PSReadLine
Set-PSReadLineKeyHandler -Chord "c,i" -ViMode Command -ScriptBlock { VIChangeInnerBlock }
Set-PSReadLineKeyHandler -Chord "c,a" -ViMode Command -ScriptBlock { VIChangeOuterBlock }
Set-PSReadLineKeyHandler -Chord "d,i" -ViMode Command -ScriptBlock { VIDeleteInnerBlock }
Set-PSReadLineKeyHandler -Chord "d,a" -ViMode Command -ScriptBlock { VIDeleteOuterBlock }
Set-PSReadLineKeyHandler -Chord "c,s" -ViMode Command -ScriptBlock { VIChangeSurround }
Set-PSReadLineKeyHandler -Chord "d,s" -ViMode Command -ScriptBlock { VIDeleteSurround }
Set-PSReadLineKeyHandler -Chord "Ctrl+a" -ViMode Command `
-ScriptBlock { VIIncrement $args[0] $args[1] }
Set-PSReadLineKeyHandler -Chord "Ctrl+x" -ViMode Command `
-ScriptBlock { VIDecrement $args[0] $args[1] }
@belotn
belotn / Set-CommandLineEdit.ps1
Last active October 5, 2021 11:12
Add Key Handler in VIMode Command set to $EditKey or Space by default to open commandline with console editor set in $EDITOR
Set-PSReadLineKeyHandler -VIMode Command -Key $(@{$False="\";$True=$EditKey}[$(Test-Path Variable:EditKey)]) -ScriptBlock {
if( Get-Command $EDITOR){
# Grab BufferLine
$Line = $Null
$Cursor = $Null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$Line, [ref]$Cursor)
# Launch Editor with temporary file
$File = New-TemporaryFile
Set-Content -Path $File -Value $Line
if($EDITOR -match '[ng]?vim?'){
@belotn
belotn / Karrakis.psm1
Created December 3, 2020 12:41
Oh-My-Posh 2 (Pure Powershell) Themes
#requires -Version 2 -Modules posh-git
$Global:ThemeSettings.PromptSymbols.PromptIndicator = '%'
$Global:ThemeSettings.PromptSymbols.ElevatedSymbol = "#"
$Global:ThemeSettings.PromptSymbols.SegmentForwardSymbol = [char]::ConvertFromUtf32(0xE0B0)
$Global:ThemeSettings.Colors.PromptForegroundColor = [ConsoleColor]::White
$Global:ThemeSettings.Colors.PromptSymbolColor = [ConsoleColor]::White
$Global:ThemeSettings.Colors.PromptHighlightColor = [ConsoleColor]::DarkBlue
$Global:ThemeSettings.Colors.GitForegroundColor = [ConsoleColor]::Black
$Global:ThemeSettings.Colors.WithForegroundColor = [ConsoleColor]::DarkRed
@belotn
belotn / Test-QualityCode.ps1
Created October 26, 2020 17:05
Script to check formatting and best practice before committing
######################################################################
# SCRIPT: Test-CodeQuality.ps1 #
######################################################################
# Purpose #
# Script to check formatting and best practice before committing #
######################################################################
# Author : Nicolas #
######################################################################
# ChangeLog #
# 20201026 - Initial Release #
@belotn
belotn / prompt.ps1
Last active November 24, 2020 16:30
Custom Prompt Function for powershell
############################################################
# FUNCTION: Prompt #
############################################################
# Redefine Prompt setting new color for new sessions #
# and setting username, servername, PID and WS as title #
# TODO: Customisable prompt FIXED/ #
# TODO: Add Domain Information #D DONE/ #
############################################################
function Prompt {
<#
pkg info | grep php | grep 5.4 | awk '{print $1}' | awk -F '-5.4' '{a = $1 ; sub("5","56",a) ; print "portmaster -y -D -o ` find /usr/ports -name "a" -print | sed \"s/\\/usr\\/ports\\///\"` "$1}' | sh
@belotn
belotn / graylog_kms_extractor.json
Created January 22, 2015 17:00
Graylog2 KMS activation extrator
{
"extractors": [
{
"condition_type": "string",
"condition_value": "activation request",
"converters": [],
"cursor_strategy": "copy",
"extractor_config": {
"regex_value": ".*0x[0-9a-f]+,\\d+,([^,]+),[0-9a-f\\-]+,[^,]+,\\d,\\d,\\d+,[0-9a-f\\-]+.*"
},
@belotn
belotn / bulk_add.php
Created November 28, 2014 14:29
Cacti Bulk Add Script
<html><head><title>Ajouter device par lot</title></head><body>
<?php
if(!isset($_POST['add']) && !isset($_FILES['csv']) && !isset($_POST['ds_add'])){
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<label>Fichier CSV</label> <input type="hidden" name="MAX_FILE_SIZE" value="204800" /><input type="file" name="csv"/><br/>
<input type="submit" name="ok" value="Envoyer"/>
</form>
<?php
}elseif(isset($_POST['add'])){
@belotn
belotn / Chage-ServiceOnFailure.ps1
Created March 26, 2014 16:07
Change failure settings for unconfigured service
get-xaserver |% {
gwmi Win32_Service -ComputerNAme $_.ServerName -Filter 'Name="cpsvc"' |% {
if(sc.exe \\$($_.__SERVER) qfailure $($_.name) | select-string 'FAILURE'){
"$($_.__SERVER) OK"
}else{
sc.Exe \\$($_.__SERVER) failure $($_.name) reset= 86400 actions= restart/60000/restart/60000
"$($_.SERVER) Set"
}
}