Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

How to Control Output Formatting in Start-Transcript/Stop-Transcript

$
0
0

Environment: Windows 7, PowerShell 2.0 (I know its old, but I am on DoD computers).

I used a script from Windows PowerShell Cookbook (O'Reilly) by Lee Holmes as a starting point to generate a hash (SHA256 specifically but supports other options).  The script works great and I have extended it to allow me to specify whether I want the output to go into individual files or a single file.  This is where I need some help.  The output looks like the following:

**********************
Windows PowerShell Transcript Start
Start time: 20151018181445
Username  : XXXXX\myusername
Machine   : YYYY-QQQQ (Microsoft Windows NT 6.1.7601 Service Pack 1)
**********************
Transcript started, output file is C:\PowerShellScripts\SingleOutput-SHA256.txt

HashAlgorithm    : SHA256
Path             : Get-FileHash-SHA256-SHA256.txt
HashingValueFile : C:\PowerShellScripts\SingleOutput-SHA256.txt
HashValue        : 54E0E03EEEC385143CC05EE583A62C2BDF4662601054FA7B114EF5B7DE971969

**********************
Windows PowerShell Transcript End
End time: 20151018181445
**********************
**********************
Windows PowerShell Transcript Start
Start time: 20151018181445
Username  : XXXXX\myusername 
Machine   : YYYY-QQQQ  (Microsoft Windows NT 6.1.7601 Service Pack 1)
**********************
Transcript started, output file is C:\PowerShellScripts\SingleOutput-SHA256.txt

HashAlgorithm    : SHA256
Path             : Get-FileHash-SHA256.txt
HashingValueFile : C:\PowerShellScripts\SingleOutput-SHA256.txt
HashValue        : 0D0E2A7DB9E55D2A19877B0277C1D505365547228CB29E14DDCED965C91F6590

**********************
Windows PowerShell Transcript End
End time: 20151018181445
**********************

This continues for as many files as I piped into the command.  What I would like to see is something like this:

**********************
Windows PowerShell Transcript Start
Start time: 20151018181445
Username  : XXXXX\myusername 
Machine   : YYYY-QQQQ  (Microsoft Windows NT 6.1.7601 Service Pack 1)
**********************
Transcript started, output file is C:\PowerShellScripts\SingleOutput-SHA256.txt

HashAlgorithm    : SHA256
Path             : Get-FileHash-SHA256-SHA256.txt
HashingValueFile : C:\PowerShellScripts\SingleOutput-SHA256.txt
HashValue        : 54E0E03EEEC385143CC05EE583A62C2BDF4662601054FA7B114EF5B7DE971969

HashAlgorithm    : SHA256
Path             : Get-FileHash-SHA256.txt
HashingValueFile : C:\PowerShellScripts\SingleOutput-SHA256.txt
HashValue        : 0D0E2A7DB9E55D2A19877B0277C1D505365547228CB29E14DDCED965C91F6590

**********************
Windows PowerShell Transcript End
End time: 20151018181445
**********************

As you can see I would like each file within a single Start Transcript/End Transcript Block.  I have pasted the code for the command below (hopefully that is ok).  Any assistance would be greatly appreciated.  We are required to produce SHA256 Hash values for all our development files that we want pushed to Production so the Sys Admins can verify that the code hasn't been changed (accidentally or on purpose).

Thanks

Rick Anderson

##############################################################################
##
## Get-FileHash-SingleFileOutput
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
## Modified By: Richard Anderson - 2/27/2015
## Modified the default HashAlgorithm parameter
## Added the OutputFile and changed the output type to Format-List
##
## Richard Anderson - 10/18/2015
## Modified to add third (optional) parameter of -SingleFileOutput
## This parameter toggles between outputting the Hash into a single
## file or multiple files. This parameter is useful when using
## retrieving the hash of multiple files by piping with
## the DIR command
##############################################################################

<#

.SYNOPSIS

Get the hash of an input file. If the parameter is named, its order is not important. The two named parameters are:
-Hash : This specifies what Hashing Algorithm to use
The options are MD5, SHA1, SHA256, SHA384, and SHA512. SHA256 is the default

-SingleFileOutput : If this parameter is present, the output of the script will go to a single file
called SingleOutput-<chosen Hash Algorithm>.txt. If this parameter is missing
then each files hash will be output to a file called <filename>-<chosen Hash Algorithm>.txt
(For example: myFile.cs with a MD5 hash would be myFile-MD5.txt)

.EXAMPLE 1
DESCRIPTION
Gets the hash of a specific file with a default Hash of SHA256 and into an output file of myFile-SHA256.txt
COMMAND
.\Get-FileHash-SingleFileOutputmyFile.txt

.EXAMPLE 2
DESCRIPTION
Get the hash of a single file using a specific Hashing Algorithm and into an output file of myFile-<chosen Hash Algorithm>.txt
COMMAND
.\Get-FileHash-SingleFileOutput-Hash MD5 myFile.txt

.EXAMPLE 3
DESCRIPTION
Get the hash of a list of files from the pipeline with a default Hash of SHA256 and into an output file of <filename>-SHA256.txt
COMMAND
dir | .\Get-FileHash

.EXAMPLE 4
DESCRIPTION
Get the hash of a list of files from the pipeline with a default Hash of SHA256 and into a single output file SingleOutput-SHA256.txt
COMMAND
DIR | .\Get-FileHash-SingleFileOutput-SingleFileOutput

.EXAMPLE 5
DESCRIPTION
Get the hash of a list of files from the pipeline using a specific Hashing Algorithm and into a single output file SingleOutput-<chosen Hash Algorithm>.txt
COMMAND
DIR | .\Get-FileHash-SingleFileOutput-SingleFileOutput-Hash MD5
#>

param(
## The path of the file to check
$Path,

## The algorithm to use for hash computation
[ValidateSet("MD5", "SHA1", "SHA256", "SHA384", "SHA512")]
$HashAlgorithm="SHA256",

[switch]$SingleFileOutput

)

Set-StrictMode-Version Latest

## Create the hash object that calculates the hash of our file.
$hashType= [Type] "System.Security.Cryptography.$HashAlgorithm"
$hasher=$hashType::Create()

## Create an array to hold the list of files
$files= @()

## If they specified the file name as a parameter, add that to the list
## of files to process
if($path)
{
$files+=$path
}
## Otherwise, take the files that they piped in to the script.
## For each input file, put its full name into the file list
else
{
$files+= @($input | Foreach-Object { $_.FullName })
}

## Go through each of the items in the list of input files
foreach($filein$files)
{
## Skip the item if it is not a file
if(-not (Test-Path$file-Type Leaf)) { continue }

## Convert it to a fully-qualified path
$filename= (Resolve-Path$file).Path

## Use the ComputeHash method from the hash object to calculate
## the hash
$inputStream=New-ObjectIO.StreamReader$filename
$hashBytes=$hasher.ComputeHash($inputStream.BaseStream)
$inputStream.Close()

## Convert the result to hexadecimal
$builder=New-ObjectSystem.Text.StringBuilder
$hashBytes | Foreach-Object { [void] $builder.Append($_.ToString("X2")) }

## Prep for Output
if ($SingleFileOutput)
{
$Filename="SingleOutput"
}
else
{
$FileName= [io.path]::GetFileNameWithoutExtension($filename)
}
##$FileName = [io.path]::GetFileNameWithoutExtension($filename)
$ExecutionPath=split-path$SCRIPT:MyInvocation.MyCommand.Path
$OutputFile=$ExecutionPath+"\"+$FileName+"-"+$HashAlgorithm+".txt"

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference="Continue"
Start-Transcript-path$OutputFile-append

## Return a custom object with the important details from the
## hashing
$output=New-Object PsObject -Property @{
Path = ([IO.Path]::GetFileName($file));
HashAlgorithm =$hashAlgorithm;
HashValue =$builder.ToString();
HashingValueFile =$OutputFile
}

$output | Format-List
Stop-Transcript
}

Viewing all articles
Browse latest Browse all 6937

Trending Articles