PowerShell – Using Pipeline Functions

Background

In its design, Powershell offers a very elegant mechanism for pipeline handling.

For instance, often in software development, we will need to process sets.

Outline

  1. Function :- showFileContent
    • Parameters
      • ValueFromPipeline $filename
    • Declare Local Variables
      • [int] $rc = 0;
      • [string] $filenameFullName = $null;
      • [string] $filenameLeaf = $null;
    • Process
      • Increment script variable $fileNumber ( $script:fileNumber )
      • Get $filenameFullName from $pipeline variable
        • $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath
          ($filename)
      • Get $filenameLeaf from
        • $filenameLeaf = Split-Path -Path $filenameFullName -Leaf -Resolve
      • Get Contents
        • Get-Content $filename
  2. Main
    • Set Variables
      • $fileInclude
        • File with extensions txt and log
        • $filesInclude = “.*\.txt.*|.*\.log.*”
    • Get Files
      • Get-Childitem -File $folder -Recurse
    • Filter
      • Where-Object {$_.PSPath -match $filesInclude}
    • Iterate Files
      • Outline
        • For each file
          • Get full file’s path
          • Pass full file’s path to function showFileContent
      • Code
        • foreach { $_.PSPath | showFileContent }

 

Source Code

GitHub – Gist

param ( `
[Parameter(Mandatory)] [string] $folder `
)
Set-StrictMode -Version latest
<#
Declare Variables
#>
#Only include files with txt and log extension
[string] $filesInclude = ".*\.txt.*|.*\.log.*"
[string] $CHAR_LINE = "=";
[String] $log = "";
[int] $fileNumber=0;
[Boolean] $bFolderExists = $false;
<#
Function
Name:- showFileContents
Arguments
$psPath
#>
Function showFileContent
{
[CmdletBinding()]
Param
(
[Parameter(ValueFromPipeline)] $psPath
)
<#
Declare Variables
#>
Begin
{
[int] $rc = 0;
[string] $filenameFullName = $null;
[string] $filenameLeaf = $null;
}
<#
Process
#>
Process
{
#Increment file number
$script:fileNumber = $script:fileNumber + 1;
#Get FileName from PSPath
$filenameFullName = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($psPath)
#Get filename from full path
$filenameLeaf = Split-Path -Path $filenameFullName -Leaf -Resolve
#Display Space
Write-Host ""
Write-Host ""
#Display Filename
$log = "{0}a) Filename:- {1}" -f $script:fileNumber, $psPath;
Write-Host $log
#Display FilenameLeaf
$log = "{0}b) FilenameLeaf:- {1}" -f $script:fileNumber, $filenameLeaf;
Write-Host $log
#Get length of display log
$logLength = $log.length;
#Prepare Filename Divider
$log = $CHAR_LINE.PadRight($logLength, $CHAR_LINE)
#Display Filename Divider
Write-Host $log
#Get file contents
$fileContents = Get-Content -raw $psPath
#Display File Content
$fileContents
}
<#
Deallocate
#>
End
{
}
}
$bFolderExists = Test-Path -Path $folder;
<#
Get files in passed in folder
recurse sub-directory
only include files matching pattern
for each matching file
call function showFileContents
#>
if ($bFolderExists -eq $false)
{
$log = "Folder {0} does not exist" -f $folder;
Write-Error $log;
}
else
{
Get-Childitem -File $folder -Recurse |
Where-Object {$_.PSPath -match $filesInclude} |
foreach { $_.PSPath | showFileContent }
}
view raw showfiles.ps1 hosted with ❤ by GitHub

Invoke

Sample Invocation

Sample

Output

Explanation

  1. filename
    • Sample
      • Microsoft.PowerShell.Core\FileSystem::C:\tmp\datafile\briana.txt
        • Break down
          • Microsoft.PowerShell.Core\FileSystem
          • ::
          • C:\tmp\datafile\briana.txt
  2. filenameFullName
    • Outline
      • To get the actual file name from PSPath, please use $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath
    •  Syntax
      • $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(<filename>)
    •  Sample
      • $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($filename)

 

Source Code Control

Git

DanielAdeniji/showfiles.ps1
Link

 

References

  1. Microsoft
    • Docs
      • Docs / System.Management.Automation / PathIntrinsics / Methods / GetUnresolvedProviderPathFromPSPath
        Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s