New ‘Log’ function added to SBTools module logs input string to file and display it to screen in different colors


SBTools module can be downloaded from the Microsoft Script Center Repository.

To install it, extract the .rar file, and run install-SBTools.ps1 in the folder where you extracted the .rar file.

Log:

This is a function to log input string to file and display it to screen. Log entries in the log file are time stamped. Function allows for displaying text to screen in different colors.

The function accepts 3 parameters:

  1. String: Required. The string to be displayed to the screen and saved to the log file
  2. Color: The color in which to display the input string on the screen
    Default is White
    Valid options are
    Black
    Blue
    Cyan
    DarkBlue
    DarkCyan
    DarkGray
    DarkGreen
    DarkMagenta
    DarkRed
    DarkYellow
    Gray
    Green
    Magenta
    Red
    White
    Yellow
  3. LogFile
    Path to the file where the input string should be saved.
    Example: c:\log.txt
    If absent, the input string will be displayed to the screen only and not saved to log file

Examples

  • Log -String “Hello World” -Color Yellow -LogFile c:\log.txt
    This example displays the “Hello World” string to the console in yellow, and adds it as a new line to the file c:\log.txt
    If c:\log.txt does not exist it will be created.
    Log entries in the log file are time stamped. Sample output:
    2014.08.06 06:52:17 AM: Hello World
  • Example
    Log “$((Get-Location).Path)” Cyan
    This example displays current path in Cyan, and does not log the displayed text to log file.
  • Example
    “Java process ID is $((Get-Process -Name java).id )” | log -color Yellow
    Sample output of this example:
    Java process ID is 4492
  • Example (This example uses Get-SBVHD function)
    “Drive ‘d’ on VM ‘CM01’ is on VHDX file ‘$((Get-SBVHD CM01 d).VHDPath)'” | log -color Green -LogFile D:\Sandbox\Serverlog.txt
    Sample output of this example:
    Drive ‘d’ on VM ‘CM01’ is on VHDX file ‘D:\VMs\Virtual Hard Disks\CM01_D1.VHDX’
    and the same is logged to file D:\Sandbox\Serverlog.txt as in:
    2014.08.06 07:28:59 AM: Drive ‘d’ on VM ‘CM01’ is on VHDX file ‘D:\VMs\Virtual Hard Disks\CM01_D1.VHDX’

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s