Friday, 11 November 2016

Save images inside PS code

This is accomplished with Base64 Encoding. Base64 Encoding basically takes 3 binary bytes (24 bits) and converts them into 4 ASCII characters.

Use the following code to generate the utility-

######################################################
# Image2Base64.ps1
#
# Encodes an Image into Base64 ASCII text
######################################################
Add-Type -AssemblyName System.Windows.Forms

# WinForm Setup
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Font = "Comic Sans MS,9"
$mainForm.ForeColor = "Black"
$mainForm.BackColor = "PaleGoldenrod"
$mainForm.Text = " Image2Base64 Converter"
$mainForm.Width = 570
$mainForm.Height = 200

# Output Box
$textBoxOut = New-Object System.Windows.Forms.TextBox
$textBoxOut.Location = "35, 85"
$textBoxOut.Size = "415, 20"
$mainForm.Controls.Add($textBoxOut)

# Input Box
$textBoxIn = New-Object System.Windows.Forms.TextBox
$textBoxIn.Location = "35, 25"
$textBoxIn.Size = "415, 20"
$mainForm.Controls.Add($textBoxIn)

# Browse Input Button
$buttonBrowse = New-Object System.Windows.Forms.Button
$buttonBrowse.Location = "460, 25"
$buttonBrowse.Size = "75, 23"
$buttonBrowse.Text = "Browse"
$buttonBrowse.add_Click({selectFiles})
$mainForm.Controls.Add($buttonBrowse)

# Convert Button
$buttonConvert = New-Object System.Windows.Forms.Button
$buttonConvert.Location = "280, 130"
$buttonConvert.Size = "75, 23"
$buttonConvert.Text = "Convert"
$buttonConvert.add_Click({convertButton})
$mainForm.Controls.Add($buttonConvert)

# Exit Button
$buttonExit = New-Object System.Windows.Forms.Button
$buttonExit.Location = "375, 130"
$buttonExit.Size = "75, 23"
$buttonExit.Text = "Exit"
$buttonExit.add_Click({$mainForm.close()})
$mainForm.Controls.Add($buttonExit)

# File to Convert Label
$convertLabel = New-Object System.Windows.Forms.Label
$convertLabel.Location = "34, 48"
$convertLabel.Size = "130, 23"
$convertLabel.Text = "Input Image File"
$mainForm.Controls.Add($convertLabel)

# Out File Label
$outLabel = New-Object System.Windows.Forms.Label
$outLabel.Location = "34, 111"
$outLabel.Size = "130, 23"
$outLabel.Text = "Output Text File"
$mainForm.Controls.Add($outLabel)

# Completion Label
$completeLabel = New-Object System.Windows.Forms.Label
$completeLabel.Location = "460, 85"
$completeLabel.Size = "130, 23"
$completeLabel.Text = ""
$mainForm.Controls.Add($completeLabel)

Function convertButton {
    $error.clear()
    $inFileName = $textBoxIn.Text
    $outFileName = $textBoxOut.Text
    $encodedImage = [convert]::ToBase64String((get-content $inFileName -encoding byte))
    $encodedImage -replace ".{80}" , "$&`r`n" | set-content $outFileName
    If ($error.count -gt 0) {
        $completeLabel.Text = "Error!"
    }
    Else {
        $completeLabel.Text = "Done!"
    }
} # End convertButton

function selectFiles {
    $selectForm = New-Object System.Windows.Forms.OpenFileDialog
    $selectForm.Filter = "All Files (*.*)|*.*"
    $selectForm.InitialDirectory = ".\"
    $selectForm.Title = "Select a File to Process"
    $getKey = $selectForm.ShowDialog()
    If ($getKey -eq "OK") {
            $textBoxIn.Text  = $selectForm.FileName
            $work = $selectForm.FileName
            $work = $work.SubString(0, ($work.Length -3)) + "txt"
            $textBoxOut.Text = $work
            $completeLabel.Text = "      "
    }
} # End SelectFile

[void] $mainForm.ShowDialog()

Wednesday, 14 September 2016

ScreenSaver Matrix style #Powershell

Set-StrictMode -off


if ($host.ui.rawui.windowsize -eq $null) {
    write-warning "Sorry, I only work in a true console host like powershell.exe."
    throw
}

#
# Console Utility Functions
#

function New-Size {
    param([int]$width, [int]$height)
  
    new-object System.Management.Automation.Host.Size $width,$height
}

function New-Rectangle {
    param(
        [int]$left,
        [int]$top,
        [int]$right,
        [int]$bottom
    )
  
    $rect = new-object System.Management.Automation.Host.Rectangle
    $rect.left= $left
    $rect.top = $top
    $rect.right =$right
    $rect.bottom = $bottom
  
    $rect
}

function New-Coordinate {
    param([int]$x, [int]$y)
  
    new-object System.Management.Automation.Host.Coordinates $x, $y
}

function Get-BufferCell {
    param([int]$x, [int]$y)
  
    $rect = new-rectangle $x $y $x $y
  
    [System.Management.Automation.Host.buffercell[,]]$cells = $host.ui.RawUI.GetBufferContents($rect)  
  
    $cells[0,0]
}

function Set-BufferCell {
    [outputtype([System.Management.Automation.Host.buffercell])]
    param(
        [int]$x,
        [int]$y,
        [System.Management.Automation.Host.buffercell]$cell
    )
  
    $rect = new-rectangle $x $y $x $y
      
    # return previous
    get-buffercell $x $y

    # use "fill" overload with single cell rect  
    $host.ui.rawui.SetBufferContents($rect, $cell)
}

function New-BufferCell {
    param(
        [string]$Character,
        [consolecolor]$ForeGroundColor = $(get-buffercell 0 0).foregroundcolor,
        [consolecolor]$BackGroundColor = $(get-buffercell 0 0).backgroundcolor,
        [System.Management.Automation.Host.BufferCellType]$BufferCellType = "Complete"
    )
  
    $cell = new-object System.Management.Automation.Host.BufferCell
    $cell.Character = $Character
    $cell.ForegroundColor = $foregroundcolor
    $cell.BackgroundColor = $backgroundcolor
    $cell.BufferCellType = $buffercelltype
  
    $cell
}

function log {
    param($message)
    [diagnostics.debug]::WriteLine($message, "PS ScreenSaver")
}

#
# Main entry point for starting the animation
#

function Start-CMatrix {
    param(
        [int]$maxcolumns = 10,
        [int]$frameWait = 100
    )

    $script:winsize = $host.ui.rawui.WindowSize
    $script:columns = @{} # key: xpos; value; column
    $script:framenum = 0
      
    $prevbg = $host.ui.rawui.BackgroundColor
    $host.ui.rawui.BackgroundColor = "black"
    cls
  
    $done = $false      
  
    while (-not $done) {

        Write-FrameBuffer -maxcolumns $maxcolumns

        Show-FrameBuffer
      
        sleep -milli $frameWait
      
        $done = $host.ui.rawui.KeyAvailable      
    }
  
    $host.ui.rawui.BackgroundColor = $prevbg
    cls
}

# TODO: actually write into buffercell[,] framebuffer
function Write-FrameBuffer {
    param($maxColumns)

    # do we need a new column?
    if ($columns.count -lt $maxcolumns) {
      
        # incur staggering of columns with get-random
        # by only adding a new one 50% of the time
        if ((get-random -min 0 -max 10) -lt 5) {
          
            # search for a column not current animating
            do {
                $x = get-random -min 0 -max ($winsize.width - 1)
            } while ($columns.containskey($x))
          
            $columns.add($x, (new-column $x))
          
        }
    }
  
    $script:framenum++
}

# TODO: setbuffercontent with buffercell[,] framebuffer
function Show-FrameBuffer {
    param($frame)
  
    $completed=@()
  
    # loop through each active column and animate a single step/frame
    foreach ($entry in $columns.getenumerator()) {
      
        $column = $entry.value
  
        # if column has finished animating, add to the "remove" pile
        if (-not $column.step()) {          
            $completed += $entry.key
        }
    }
  
    # cannot remove from collection while enumerating, so do it here
    foreach ($key in $completed) {
        $columns.remove($key)
    }  
}

function New-Column {
    param($x)
  
    # return a new module that represents the column of letters and its state
    # we also pass in a reference to the main screensaver module to be able to
    # access our console framebuffer functions.
  
    new-module -ascustomobject -name "col_$x" -script {
        param(
            [int]$startx,
            [PSModuleInfo]$parentModule
         )
      
        $script:xpos = $startx
        $script:ylimit = $host.ui.rawui.WindowSize.Height

        [int]$script:head = 1
        [int]$script:fade = 0
        [int]$script:fadelen = [math]::Abs($ylimit / 3)
      
        $script:fadelen += (get-random -min 0 -max $fadelen)
      
        function Step {
          
            # reached the bottom yet?
            if ($head -lt $ylimit) {

                & $parentModule Set-BufferCell $xpos $head (
                    & $parentModule New-BufferCell -Character `
                        ([char](get-random -min 65 -max 122)) -Fore white) > $null
              
                & $parentModule Set-BufferCell $xpos ($head - 1) (
                    & $parentModule New-BufferCell -Character `
                        ([char](get-random -min 65 -max 122)) -Fore green) > $null
              
                $script:head++
            }
          
            # time to start rendering the darker green "tail?"
            if ($head -gt $fadelen) {

                & $parentModule Set-BufferCell $xpos $fade (
                    & $parentModule New-BufferCell -Character `
                        ([char](get-random -min 65 -max 122)) -Fore darkgreen) > $null
                  
                $script:fade++
            }
          
            # are we done animating?
            if ($fade -lt $ylimit) {
                return $true
            }
                      
            $false          
        }
              
        Export-ModuleMember -function Step
      
    } -args $x, $executioncontext.sessionstate.module
}

function Start-ScreenSaver {
  
    # feel free to tweak maxcolumns and frame delay
    # currently 8 columns with 50ms wait
  
    Start-CMatrix -max 14 -frame 50
}

function Register-Timer {

    # prevent prompt from reregistering if explicit disable
    if ($_ssdisabled) {
        return
    }
  
    if (-not (Test-Path variable:global:_ssjob)) {
      
        # register our counter job
        $global:_ssjob = Register-ObjectEvent $_sstimer elapsed -action {
          
            $global:_sscount++;
            $global:_sssrcid = $event.sourceidentifier
              
            # hit timeout yet?
            if ($_sscount -eq $_sstimeout) {
              
                # disable this event (prevent choppiness)
                Unregister-Event -sourceidentifier $_sssrcid
                Remove-Variable _ssjob -scope Global
                         
                sleep -seconds 1
                   
                # start ss
                Start-ScreenSaver
            }

        }
    }
}

function Enable-ScreenSaver {
  
    if (-not $_ssdisabled) {
        write-warning "Screensaver is not disabled."
        return
    }
  
    $global:_ssdisabled = $false  
}

function Disable-ScreenSaver {

    if ((Test-Path variable:global:_ssjob)) {

        $global:_ssdisabled = $true
        Unregister-Event -SourceIdentifier $_sssrcid      
        Remove-Variable _ssjob -Scope global      

    } else {
        write-warning "Screen saver is not enabled."
    }
}

function Get-ScreenSaverTimeout {
    new-timespan -seconds $global:_sstimeout
}

function Set-ScreenSaverTimeout {
    [cmdletbinding(defaultparametersetname="int")]
    param(
        [parameter(position=0, mandatory=$true, parametersetname="int")]
        [int]$Seconds,
      
        [parameter(position=0, mandatory=$true, parametersetname="timespan")]
        [Timespan]$Timespan
    )
  
    if ($pscmdlet.parametersetname -eq "int") {
        $timespan = new-timespan -seconds $Seconds
    }
  
    if ($timespan.totalseconds -lt 1) {
        throw "Timeout must be greater than 0 seconds."
    }
  
    $global:_sstimeout = $timespan.totalseconds
}

#
# Eventing / Timer Hooks, clean up and Prompt injection
#

# timeout
[int]$global:_sstimeout = 30 # default 3 minutes

# tick count
[int]$global:_sscount = 0

# modify current prompt function to reset ticks counter to 0 and
# to reregister timer, while saving for later on module onload

$self = $ExecutionContext.SessionState.Module
$function:global:prompt = $self.NewBoundScriptBlock(
    [scriptblock]::create(
        ("{0}`n`$global:_sscount = 0`nRegister-Timer" `
            -f ($global:_ssprompt = gc function:prompt))))

# configure our timer
$global:_sstimer = new-object system.timers.timer
$_sstimer.Interval = 1000 # tick once a second
$_sstimer.AutoReset = $true
$_sstimer.start()

# we start out disabled - use enable-screensaver
$global:_ssdisabled = $true

# arrange clean up on module remove
$ExecutionContext.SessionState.Module.OnRemove = {
  
    # restore prompt
    $function:global:prompt = [scriptblock]::Create($_ssprompt)
  
    # kill off eventing subscriber, if one exists
    if ($_sssrcid) {
        Unregister-Event -SourceIdentifier $_sssrcid
    }
  
    # clean up timer
    $_sstimer.Dispose()
  
    # clear out globals
    remove-variable _ss* -scope global
}

Export-ModuleMember -function Start-ScreenSaver, Get-ScreenSaverTimeout, `
    Set-ScreenSaverTimeout, Enable-ScreenSaver, Disable-ScreenSaver
The above code will give error if executed directly.
This is a MODULE.

To be able to execute this screensaver, copy the above code in a txt file, and save the file with ".psm1" extension in the directory- "..\Documents\windowspowershell\modules"



The only thing to keep in mind is that-
Name of the .psm1 file should be same as the Folder name you create under "modules" folder.

(See above FOO.psm1 file under FOO folder)


Now, open Powershell session-
Run-
Get-Module -ListAvailable



You will see the name of the .psm1 file in Module name. (FOO in this case)

Now you need to make this module available to the current session

Run-
Import-Module FOO -PassThru



THATS IT..

To see all imported commands in this module, type "Get-Command -module FOO"

now type "Start-ScreenSaver" and see the magic