The Kirkland Coder: Write-NoteStart

Tuesday, February 21, 2012

Write-NoteStart

Once in a while I have to output information from a script that will say what I am doing and then if it completed or failed. In most cases it would look horrible on the screen. Like so:

Checking network connection...completed.
Checking file...completed.
Writing network information to file...completed.

This bugged me to no end; so I wrote the “Write-NoteStart” function to make it easier for me to sleep at night. When using this function the output looks more like this:

Checking network connection.............completed.
Checking file...........................completed.
Writing network information to file.....completed.

Here is the function:

function Write-NoteStart
{
<#
.SYNOPSIS
This function will place periods after writing a string and not write a new line.

.DESCRIPTION
This script is used to produce dots/periods after a given string for aligning
results. You can adjust the position it should add dots up to.

.EXAMPLE
    Write-NoteStart "Checking status" -DotTo 40
    Write-Host "completed."
    Write-NoteStart "Checking your connection" -DotTo 40
    Write-Host "completed."
    -----------------------------------------------------------
    This would produce:
    
Checking status.........................completed.
Checking your connection................completed.

.INPUTS
A string and an int.


.OUTPUTS
Write-Host to the screen with your string followed by periods.

.NOTES
Original Function name: Write-NoteStart.ps1
Original Author: Norman Skinner
Original Created on: 02/01/2012
Version: 1.0.0.0

#>
    param (
        [string]
        # The string for the start of the line.
        $Note = "No data",
        [int]
        # What line position to run dots to.
        $DotTo = 60
    )
    if ($Note.Length -lt $DotTo)
    {
        Write-Host $Note -NoNewline
        for ($i = $Note.Length;$i -lt $DotTo; $i++)
        {
            Write-Host "." -NoNewline
        }
    }
    else
    {
        Write-Host $Note
    }
}


No comments:

Post a Comment