Adam Lacey
Adam Lacey

Reputation: 103

Why does write-host output to transcript but write-information does not?

This one is confusing me and wondered if anyone could shed some light on this, maybe also answer 2 parts with this one example?

I have a Powershell script I run like a batch file and I have it output values it has captured from a separate file out to a log. This particular part is not outputting in the transcript where I get the DB version of a database. I have tried different placements of the ", using $DBVersion on it's own and this is a simple way to show what I have trouble with. e.g.:

## Read the DBs Extended properties
Function Get-DB_Version {
Param (
    $SQLInstance,
    $DBName
)
Invoke-Sqlcmd -ServerInstance $SQLInstance -Database $DBName -Query "SELECT value FROM fn_listextendedproperty(default, default, default, default, default, default, default) WHERE name = 'version'"
}
**Other Variables used are also pre-set above here **


## Get DB version
$DBVersion = Get-DB_Version -SQLInstance $SQLInstance -DBName $DBName



Start-Transcript -Path "$LogOutput\$LogName" -IncludeInvocationHeader -Append 

Write-Information "
**** DEBUG INFO ****
Write-Host "Debug:"$DBVersion.value
Write-Information "Debug:"$DBVersion.value
Read-Host -Prompt "PAUSE" # This is so I can visually see the console seeing only the write-host is there as expected.

Stop-Transcript

In my log file I get the output:

Debug: 2.16.51443.5147

INFO: Debug:

This shows me that the variable contains a value as the write-host outputs it, however when use Write-Information it does not show anything in the log, All other variables I use do show, why would $DBVersion.value or $DBVersion not show anything please?

Also the second part is, why do I have to use:

$DBVersion.value

Outside of the write-host "" quotes?

Many thank in Advance

Upvotes: 0

Views: 1379

Answers (1)

Adam Lacey
Adam Lacey

Reputation: 103

As @abraham said in the comments. All I had to do, to have the variable inside of the quotes (my question 2) was use the sub-expression operator $() to expand the value inside the quotes: Write-Host "Debug: $($DBVersion.value)". The same goes for your Write-Information.

Doing this alone also resolved my original question of why Write-Information didn't output anything into the transaction logs and I did NOT need to change the $InformationPreference.

Upvotes: 1

Related Questions