Since Write-Verbose doesn't support some of the features that Write-Host does (like the -NoNewLine option), I had to roll my own Write-VeboseEx function. I put it into a WriteUtils.psm1 file and I import that module into my .ps1 scripts, but for some reason, my top-level script won't print anything when I call Write-VerboseEx.
However, my top-level script creates multiple threads with Start-Job which imports WriteUtils.psm1 and when I call Write-VerboseEx from inside the job, it does print the message. It's only the top-level script that refuses to print. If I use the regular Write-Verbose in the top-level script, it does print.
Here's what I have in WriteUtils.psm1:
---------------------------------------------------------------------------------------------
Param(
[parameter(Mandatory=$false)]
[bool] $VerboseOn = $false
)
if ($VerboseOn)
{
Write-Host "*** Verbose mode enabled in $($MyInvocation.MyCommand.Name). *** i.e. CommonWriteUtils.psm1"
$VerbosePreference = "Continue"
}
# Writes a line of text to the screen and prepends a timestamp and hostname to the line.
#
# Syntax:
# Write-Line [[-Text] <text>] [[-Format] <date format>] [-NoNewLine] [[-ForegroundColor] <color>] [-NoTimestamp] [<CommonParameters>]
####################################################################
functionWrite-Line
{
[CmdletBinding()]
Param
(
[parameter(Mandatory=$false)]
[string] $Text,
[parameter(Mandatory=$false)]
[string] $Format='HH:mm:ss',
[parameter(Mandatory=$false)]
[switch] $NoNewLine=$false,
[parameter(Mandatory=$false)]
[string] $ForegroundColor,
[parameter(Mandatory=$false)]
[switch] $NoTimestamp=$false
)
if ($NoTimestamp) { $msg=$Text }
else { $msg="[$(Get-Date -Format $Format) $(hostname)] $Text" }
if (-not$NoNewLine) { $msg+="`r`n" }
if ($ForegroundColor) { Write-Host-NoNewline$msg-ForegroundColor$ForegroundColor }
else { Write-Host-NoNewline$msg }
}
####################################################################
# Write text to the screen only if Verbose mode is enabled and unlike the native Write-Verbose
# function, this function also supports the -NoNewLine option.
#
# Syntax:
# Write-VerboseEx [[-Text] <text>] [-NoNewLine] [-NoTimestamp] [<CommonParameters>]
####################################################################
functionWrite-VerboseEx
{
[CmdletBinding()]
Param
(
[parameter(Mandatory=$false)]
[string] $Text,
[parameter(Mandatory=$false)]
[switch] $NoNewLine=$false,
[parameter(Mandatory=$false)]
[switch] $NoTimestamp=$false
)
if ($PSBoundParameters['Verbose'] -eq$true) { Write-Line$Text-ForegroundColor Yellow -NoNewLine:$NoNewLine-NoTimestamp:$NoTimestamp }
}
---------------------------------------------------------------------------------------------
Here is my top-level script:
---------------------------------------------------------------------------------------
(
[parameter(Mandatory=$false)]
[string] $Workspace=${Env:WORKSPACE}
)
$ErrorActionPreference="Stop"
try
{
$isVerboseOn=$PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent
if ($isVerboseOn)
{
Write-Host"*** Verbose mode enabled in $($MyInvocation.MyCommand.Name)."
$VerbosePreference="Continue"
}
Import-Module"$Workspace\CommonWriteUtils.psm1"-DisableNameChecking-ArgumentList @($isVerboseOn) | Out-Null
Write-Host"**** VerbosePreference = $VerbosePreference"# This shows VerbosePreference = "Continue"
# Print all parameters for debugging.
Write-VerboseEx"*** $($MyInvocation.MyCommand.Name) Script parameters:"
Write-VerboseEx" Workspace = '$Workspace'"
Write-VerboseEx"************************************************************"
Write-Verbose"This Write-Verbose will print, but the Write-VerboseEx's above won't"
...
---------------------------------------------------------------------------------------