PowerShell 2.0 and Write-Verbose strange behaviour
I was writting a script for work wich was invoked on Windows 2012 R2 with PowerShell 4.0 as well on Windows 2008 R2 with PowerShell 2.0. This script was calling some sg utils and comparing the results. There is option to call it and to get just the result or to call it with more messges to see what actually is doing. So Write-Verbose seemed as a perfect option. In the first case all works fine with PowerShell (PS) 4.0. For some reason with PS 2.0 in Verbose mode it is failing at some point – sg_vpd cannot be called so instead of string for the result I’m getting null.
Also when the script is called without Verbose works perfectly fine. So it seems the problem is in Write-Verbose. After research in internet I wasn’t able to find anything like this an solution to the problem. The only option I came into is to replace the Write-Verbose cmdlet with custom function that behves the same way as the cmdlet just it’s not using the verbose pipeline.
function global:Write-Verbose
{
[CmdletBinding()]
PARAM
(
[Parameter(
Mandatory = $true,
Position = 0,
ValueFromPipeline=$true)]
[string]$Message
)
process
{
# check $VerbosePreference variable
if ($VerbosePreference -ne 'SilentlyContinue')
{
# do this via Write-Host
Write-Host "VERBOSE: $Message" -ForegroundColor Yellow
}
}
}
Even though this is workarround and not a real solution to the problem so far works. I guess not so major problem since there is PS 4.0 which doesn’t have this problem.