6
\$\begingroup\$

This is a simple Powershell script to provide menu driven manuals and standard operating procedures.

Sops and mans with the extension .sopman.txt will be loaded.

The file motd is required, it is intended to prompt the user with a banner containing useful and or up to date information.

Use the markup +color+ to colorize a line, see colors.sopman.txt for complete list of supported colors.

Github Project

Bash Version Code Review

#.-----.-----.-----.--------.---.-.-----.
#|__ --|  _  |  _  |        |  _  |     |
#|_____|_____|   __|__|__|__|___._|__|__|
#            |__|   Matthew A. Brassey 

#Variables
$sops = Get-ChildItem -Path "sops/*.sopman.txt" -Recurse -Force 
$selection = $null
$stoploop = $false
$retryCount = 0
$exit = $false
$header="================================================================[sopman]===="
$footer="============================================================================"

#Functions
 function motd {
    Get-Content motd | ForEach {$textcolor = Get-Color $_; $outputtext = $_ -replace "\+[a-z]+\+\s?"; Write-Host -ForegroundColor $textcolor $outputtext }
    Write-Host -foreground Cyan $footer   
}

 function menu0 {
    Clear-Host
    Write-Host -foreground Cyan $header 
    motd
    Write-Host -foreground Green "0] Search"
    foreach ($sop in $sops) {
        $fcount++
    Write-Host -foreground Green "$fcount) $($sop.BaseName -replace ".{7}$")"
    }
    Write-Host -foreground Cyan $footer 
}

function Get-Color {
    Param([Parameter(Position=0)]
    [String]$Entry)

    process {
        if ($Entry.Contains("black")) {Return "Black"}
        elseif ($Entry.Contains("blue")) {Return "Blue"}
        elseif ($Entry.Contains("cyan")) {Return "Cyan"}
        elseif ($Entry.Contains("darkblue")) {Return "DarkBlue"}
        elseif ($Entry.Contains("darkcyan")) {Return "DarkCyan"}
        elseif ($Entry.Contains("darkgray")) {Return "DarkGray"}
        elseif ($Entry.Contains("darkgreen")) {Return "DarkGreen"}
        elseif ($Entry.Contains("darkred")) {Return "DarkRed"}
        elseif ($Entry.Contains("darkyellow")) {Return "DarkYellow"}
        elseif ($Entry.Contains("gray")) {Return "Gray"}
        elseif ($Entry.Contains("green")) {Return "Green"}
        elseif ($Entry.Contains("purple")) {Return "Magenta"}
        elseif ($Entry.Contains("red")) {Return "Red"}
        elseif ($Entry.Contains("white")) {Return "White"}
        elseif ($Entry.Contains("yellow")) {Return "Yellow"}
        else {Return "White"}
    }
}

 function search {
    Get-ChildItem -recurse -Path "sops/*.sopman.txt"| Select-String -pattern $searchString

 }

 #Code 
do {
    menu0
    #do loop to validate menu input
    do {
        [int]$input1 = Read-Host "Please select SOP to view ( ctrl-c to quit ) "
        if ($input1 -eq "0") { $searchString = Read-Host "Search for " }
        if (($input1 -ge "0") -and ($input1 -le $sops.length)) {$stoploop = $true}
        else {
            Write-Host -foreground Red "ERROR! Please select an option"
            $retryCount = $retryCount + 1
            if ($retryCount -ge 3) {
                Write-Host -foreground Red "EXIT!"
                $stoploop = $true
            }
        }
    } while ($stoploop -eq $false)

    do {
        Clear-Host
        Write-Host -foreground Cyan $header
        if ($input1 -eq "0") {Write-Host -foreground Green "Search results for ""$searchString"" :"
            search
            Write-Host -foreground Cyan $footer
            $input3 = Read-Host "[ 'm' to return to menu, anything else to quit ] " 
                if ($input3 -eq "m") { Break }
                else {Write-Host -foreground Green "Bye-Bye" }
                    exit
        }       
        Get-Content $sops[$input1-1] | ForEach {$textcolor = Get-Color $_; $outputtext = $_ -replace "\+[a-z]+\+\s?"; Write-Host -ForegroundColor $textcolor $outputtext }
        Write-Host -foreground Cyan $footer
        $input2 = Read-Host "[ 'm' to return to menu, anything else to quit ] " 
        if ($input2 -eq "m") { Break }
        else {Write-Host -foreground Green "Bye-Bye" }
        exit
    } while ($stoploop -eq $false)
} while ($exit -eq $false)
<#
.SYNOPSIS
This simple Powershell script provides menu driven manuals and standard operating procedures. 
.DESCRIPTION
Sops and mans with extension '.sopman.txt' will be loaded. The file 'motd' is required, it is intended to prompt the user with a banner containing useful and/or up to date information. Use the markup '+color+' to colorize a line, see colors.sopman.txt for complete list of supported colors.  
.EXAMPLE
./sopman.ps1
Get-Help .\sopman.ps1 -full
.NOTES
Sop Man?  
.LINK
https://github.com/mattinclude/powershell/tree/master/sopman
#>
\$\endgroup\$
  • \$\begingroup\$ Do you also have particular concerns about improvement? \$\endgroup\$ – πάντα ῥεῖ Mar 18 '17 at 18:17
  • \$\begingroup\$ The bash version allows the passing of five command line arguments version, motd, search, help and license. It would be nice to add the same functionality to this powershell version. Other than that, I'm looking for corrections, optimizations & general improvements or suggestions. \$\endgroup\$ – Matt Mar 20 '17 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.