0

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

2
  • 2
    To put it simply, it's read as 2 separate commands and not just one. So, it first runs Write-Host "....", then it runs $DBVersion.Value. if you want it in the same line, you need to place the variable inside the quotes, then using the sub-expression operator $() to expand the value inside the quotes: Write-Host "Debug: $($DBVersion.value)". Same goes for your Write-Information. Commented Aug 19, 2021 at 13:34
  • 1
    In addition to Abraham's helpful comment, you don't need either of those cmdlet but in any case, Write-Information doesn't produce any output by itself, you would need to set $InformationPreference = 'Continue' at the top of your script or use the -InformationAction Continue parameter. Commented Aug 19, 2021 at 13:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.