Fossil.psm1

<#
  .SYNOPSIS
  Creates and writes to a log file using either builtin, predefined sections (header, footer) or custom message output.
 
  .DESCRIPTION
  Creates and writes to a log file using either builtin, predefined sections (header, footer) or custom message output.
 
  .PARAMETER Path
  The path to the log file you want to create or write to.
 
  .PARAMETER Message
  The custom output you want to write to the specified log file
 
  .PARAMETER Header
  Writes the header section to the specified log file.
 
  .PARAMETER HeaderTitle
  Writes the provided title text into the header section.
 
  .PARAMETER Footer
  Writes the footer section to the specified log file
 
  .EXAMPLE
  # Initialize the log file and write the starting header section to the log
  Write-FossilLog -Path "$($env:TEMP)\filename.log)" -Header -HeaderTitle "Script Name - Log"
 
  .EXAMPLE
  # Write output to the log file from your script or application
  Write-FossilLog -Path "$($env:TEMP)\filename.log)" -Message "Test Successful"
 
  .EXAMPLE
  # Finalize the log file and write the ending footer section to the log
  Write-FossilLog -Path "$($env:TEMP)\filename.log)" -Footer
#>


function Write-FossilLog {
  [CmdletBinding(DefaultParameterSetName = "Default")]
  param (
    [Parameter(Mandatory = $true, Position = 0,
      HelpMessage = "Path to a log file for writing output.")]
    [Parameter(ParameterSetName = 'Default')]
    [Parameter(ParameterSetName = 'Header')]
    [Parameter(ParameterSetName = 'Footer')]
    [ValidateNotNullOrEmpty()]
    [string]$Path,

    [Parameter(Mandatory = $false, Position = 1,
      HelpMessage = "Writes the provided message to the log file.")]
    [Parameter(ParameterSetName = 'Default')]
    [Parameter(ParameterSetName = 'Header')]
    [Parameter(ParameterSetName = 'Footer')]
    [string]$Message,

    [Parameter(Mandatory = $true, Position = 2,
      HelpMessage = "Writes the log header section.",
      ParameterSetName = "Header")]
    [switch]$Header,
    [Parameter(Mandatory = $false, Position = 3,
      HelpMessage = "Writes the application name or other title text into the log header section.",
      ParameterSetName = "Header")]
    [string]$HeaderTitle = "Fossil | VividRock",

    [Parameter(Mandatory = $true, Position = 2,
      HelpMessage = "Writes the log footer section.",
      ParameterSetName = "Footer")]
    [switch]$Footer
  )

  begin {
    # Initialize File
    if ((Test-Path -Path $Path) -eq $false) {
      New-Item -Path $Path -ItemType Directory -Force | Out-Null
      Write-FossilLog -Header -HeaderTitle $HeaderTitle
    }

    # Setup Params
    $Params_OutFile = @{
      FilePath = $Path
      Append   = $true
    }
  }

  process {
    # Write Header
    if ($Header) {
      # Get Starting DateTime Object and Store in Env for Use with Footer Section
      $Temp_ScriptStarted = Get-Date
      if (Test-Path -Path env:Fossil_Meta_StartDate) {
        Set-Item -Path env:Fossil_Meta_StartDate -Value $Temp_ScriptStarted.ToString('o')
      }
      else {
        New-Item -Path env: -Name "Fossil_Meta_StartDate" -Value $Temp_ScriptStarted.ToString('o') | Out-Null
      }

      # Output Header Section
      Out-File -InputObject "------------------------------------------------------------------------------" @Params_OutFile
      Out-File -InputObject " $($HeaderTitle)" @Params_OutFile
      Out-File -InputObject "------------------------------------------------------------------------------" @Params_OutFile
      Out-File -InputObject " Started: $($Temp_ScriptStarted.ToUniversalTime().ToString())" @Params_OutFile
      Out-File -InputObject " Executed By: $(([System.Security.Principal.WindowsIdentity]::GetCurrent().Name))" @Params_OutFile
      Out-File -InputObject "------------------------------------------------------------------------------" @Params_OutFile
    }

    # Write Footer
    if ($Footer) {
      # Gather Data
      $Temp_ScriptStarted = [datetime]::Parse($env:Fossil_Meta_StartDate)
      $Temp_ScriptCompleted = Get-Date
      $Temp_CompleteTimeSpan = New-TimeSpan -Start $Temp_ScriptStarted -End $Temp_ScriptCompleted

      # Output
      Out-File -InputObject "------------------------------------------------------------------------------" @Params_OutFile
      Out-File -InputObject " Completed: $($Temp_ScriptCompleted.ToUniversalTime().ToString(`"yyyy-MM-dd HH:mm:ss`")) (UTC)" @Params_OutFile
      Out-File -InputObject " Total Time: $($Temp_CompleteTimeSpan.Days) days, $($Temp_CompleteTimeSpan.Hours) hours, $($Temp_CompleteTimeSpan.Minutes) minutes, $($Temp_CompleteTimeSpan.Seconds) seconds, $($Temp_CompleteTimeSpan.Milliseconds) milliseconds" @Params_OutFile
      Out-File -InputObject "------------------------------------------------------------------------------" @Params_OutFile
      Out-File -InputObject " End of Logging" @Params_OutFile
      Out-File -InputObject "------------------------------------------------------------------------------" @Params_OutFile

      # Cleanup Environment Variables
      if (Test-Path -Path env:Fossil_Meta_StartDate) {
        Remove-Item -Path env:Fossil_Meta_StartDate -Force
      }
    }

    # Write Message
    if ($Message) {
      Out-File -InputObject $Message @Params_OutFile
    }
  }

  end {

  }
}

<#
  .SYNOPSIS
  Launches a window using the specified application interface that will monitor the contents of the file provided so you can actively watch logging in a window separate from the one you are working within.
 
  .DESCRIPTION
  Launches a window using the specified application interface that will monitor the contents of the file provided so you can actively watch logging in a window separate from the one you are working within.
 
  .PARAMETER Path
  The path to the log file you want to monitor.
 
  .PARAMETER Identifier
  The identifier (name) given to the event subscription object.
 
  .PARAMETER Console
  The type of console, Terminal (recommended) or PowerShell, you want to launch to monitor the log file.
 
  .PARAMETER Title
  Set the title of the status window.
 
  .PARAMETER AlwaysOnTop
  Writes the footer section to the specified log file
 
  .EXAMPLE
  # Show the status window to monitor a log file
  Show-FossilStatusWindow -Path "$($env:TEMP)\filename.log" -
 
  .EXAMPLE
  # Write output to the log file from your script or application
  Write-FossilLog -Path "$($env:TEMP)\filename.log)" -Message "Test Successful"
 
  .EXAMPLE
  # Finalize the log file and write the ending footer section to the log
  Write-FossilLog -Path "$($env:TEMP)\filename.log)" -Footer
#>


function Show-FossilStatusWindow {
  [CmdletBinding(DefaultParameterSetName = "Default")]
  param (
    [Parameter(Mandatory = $true, Position = 0,
      HelpMessage = "The path to the log file you want to monitor.",
      ParameterSetName = "Default")]
    [string]$Path,

    [Parameter(Mandatory = $false, Position = 1,
      HelpMessage = "The type of console, Terminal (recommended) or PowerShell, you want to launch to monitor the log file.",
      ParameterSetName = "Default")]
    [ValidateSet("Terminal")]
    [string]$Console = "Terminal",

    [Parameter(Mandatory = $false, Position = 2,
      HelpMessage = "Set the title of the status window.",
      ParameterSetName = "Default")]
    [string]$Title = "Fossil | VividRock",

    [Parameter(Mandatory = $false, Position = 3,
      HelpMessage = "Set the status window to remain on top of all windows.",
      ParameterSetName = "Default")]
    [switch]$AlwaysOnTop
  )

  begin {
    # Validate
    if ((Test-Path -Path $Path) -ne $true) {
      Throw "The path provided for the log file is invalid."
    }
  }

  process {
    # Resolve Parameters
    $Path = Resolve-Path -Path $Path

    # Using Terminal
    if ($Console -eq "Terminal") {
      # Launch Status Window
      Start-Process -FilePath "wt.exe" -ArgumentList "--title `"$Title`"", "-p", "Windows PowerShell", "pwsh", "-NoExit", "-Command", "Get-Content -Path $Path -Wait" -PassThru

      # Always On Top
      if ($AlwaysOnTop) {
        # Terminal Doesn't Support this Via Command Line (Yet?)
      }
    }
  }

  end {
    # Return $Temp_EventObject
    # Return $Temp_Process
  }
}