Hi Alexander,
It already works, but only when not compiling with a SAPIEN Host, something we need to do.
Compiling with a SAPIEN Host appears to cause issues with Get-PSCallStack, at least in the way I am using it.
Module - Scaled down for illustration only
function Add-LogEntry
{
[CmdletBinding(SupportsPaging = $false,
SupportsShouldProcess = $false,
PositionalBinding = $false,
ConfirmImpact = 'None')]
param
(
[Parameter(Mandatory = $true, HelpMessage = 'The Message To Add To The Log')]
[string]$message,
[Parameter(Mandatory = $true, HelpMessage = 'The Path To The Log File')]
[string]$logFilePath
)
#Retrieve call stack for use in logging message
$psCallStack = Get-PSCallStack
#Retrieve just the file in which the calling function exists, not the full path
$psCallStackScriptNameLeaf = Split-Path -Path $psCallStack[1].ScriptName -Leaf
#Generate output string
$logFileEntry = $psCallStackScriptNameLeaf.PadRight(23) + $psCallStack[1].ScriptLineNumber.ToString().PadRight(6) + $psCallStack[1].Command.ToString().PadRight(31) + $message
$logFileEntry | Out-File -FilePath $logFilePath -Append -Encoding ascii -ErrorAction Stop
Write-Verbose -Message $logFileEntry
} #End function Add-LogEntry
function MyFunc
{
add-logentry -message "Test From My Func" -logFilePath <You need to add a path to an existing log file>
}
Script - to demonstrate behaviour
import-module -name <you need to add the path to the module> -Verbose -force
<#
Padding
#>
add-logentry -message "Test From Script" -logFilePath <You need to add a path to an existing log file>
MyFunc
You will need to amend a few paths in the above examples to make it work.
Expected Output:
ExampleScript.ps1 14 ExampleScript.ps1 Test From Script
ExampleModule.psm1 33 MyFunc Test From My Func
This logging function is going to be used throughout the module which will contain a few dozen functions.
The module will be called by a number of scripts.
When executed from ISE - Recieve Expected Output
When executed from POSH Console - Recieve Expected Output
When executed as compiled with "Microsoft Windows PowerShell (Command Line)" - Receive Expected Output
When executed as compiled with "SAPIEN PowerShel V5 Host (Command Line)" - Recieve Errors related to path being null in split-path.
The error only occurs for the message from the script, the message from within the module reports correctly, i.e. all module actions are logged but all script actions log entries are omitted due to the error.
It apperas that Get-PSCallStack does not report back on the calling script when its compiled using the SAPIEN Hosts.
Interestingly though, If you use Get-PSCallStack directly in the script it will report its line number but is off by a few lines. So the function can be used in the compiled script but when used in the module it does not show information regarding the calling compiled script.
I am trying to find a way to maintain the expected output when using a SAPIEN Host compiled script.