Created
November 6, 2018 01:47
-
-
Save WyrdNexus/826590a8ef2632664923f7516b66d6c0 to your computer and use it in GitHub Desktop.
Powershell CLI customizer
This file contains 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
# customize window | |
$shell = $Host.UI.RawUI | |
$shell.WindowTitle=”My Custom CLI” | |
$shell.BackgroundColor = “Black” | |
$shell.ForegroundColor = “Magenta” | |
# allow local unsigned scripts | |
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned | |
$ProfileRoot = (Split-Path -Parent $MyInvocation.MyCommand.Path) | |
## Add ProfileRoot to PATH (makes all profile scripts callable from CLI | |
# $env:path += ";$ProfileRoot" | |
Import-Module $ProfileRoot\Modules\commandsWrapper.psm1 | |
## Add ssh-keys | |
# ssh-agent | |
# ssh-add %UserProfile%\.ssh\my_private_key | |
i #call commandWrapper info function |
This file contains 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
## Get the directory of this wrapper script | |
$ModuleRoot = (Split-Path -Parent $MyInvocation.MyCommand.Path) | |
## Keep each specific command in it's own file, and import it | |
Import-Module $ModuleRoot\common.psm1 | |
Import-Module $ModuleRoot\customCommand.psm1 | |
## define any additional functions to be available in the CLI | |
function testme { | |
info "HELLO!" | |
} | |
function i { | |
# basic info | |
info "AVAILABLE COMMANDS" | |
info " i: info (this)" | |
info " testme: say hello" | |
info " customCommand: advanced behaviors" | |
info " > customCommand 'Test' 'Arguments' " | |
info " > customCommand 'Somebody' 'Me' " | |
info " " | |
} | |
## Only exported functions will be available in the CLI | |
Export-ModuleMember -Function testme, i |
This file contains 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
# Convenience methods for formatted/colored output | |
function info | |
{ | |
Write-Host $args -ForegroundColor Green | |
} | |
function warn | |
{ | |
Write-Host $args -ForegroundColor Yellow | |
} | |
function dang | |
{ | |
Write-Host $args -ForegroundColor Red | |
} | |
Export-ModuleMember -Function info, warn, dang |
This file contains 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
# Use the common module | |
$ModuleRoot = (Split-Path -Parent $MyInvocation.MyCommand.Path) | |
Import-Module $ModuleRoot\common.psm1 | |
# CLI INFO | |
class MyObject | |
{ | |
[string] $someProperty | |
## Constructor, called whenever creating a new instance of this object | |
MyObject([string] $name) | |
{ | |
$this.someProperty = $name | |
} | |
[string] action([string] $value) | |
{ | |
if ($this.getNameType() -eq 2) { | |
warn $this.someProperty " once told " $value | |
return 'gonna roll me' | |
} else { | |
info " Called action on " $this.someProperty " for " $value | |
return 'newValue' | |
} | |
} | |
[int] getNameType() | |
{ | |
if ($this.someProperty -eq "Test") { | |
return 1 | |
} | |
switch ($this.someProperty) { | |
"Somebody" { | |
return 2 | |
} | |
"Nobody" { | |
return 3 | |
} | |
} | |
return 0 | |
} | |
} | |
function customCommand { | |
Param( | |
[ parameter(Position=0, Mandatory=$true) ] | |
[String[]] | |
$nameArg, | |
[ parameter(Position=1, Mandatory=$false) ] | |
[String[]] | |
$valueArg | |
) | |
if ($nameArg) { | |
$Name = $nameArg.GetValue(0) | |
} | |
$TargetValue = $valueArg.GetValue(0) | |
$myObj = [MyObject]::new($Name) | |
$type = $myObj.getNameType() | |
if ($type -lte 0) { | |
warn "Unexpected MyObject Type: " $Name | |
} elseif ($type -eq 2) { | |
dang "Dang! Somebody Once..." | |
} else { | |
info "Something to do!" | |
$result = $myObj.action($TargetValue) | |
info " --- the world is " $result " --- " | |
} | |
} | |
Export-ModuleMember -Function customCommand |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment