[英]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?这让我感到困惑,想知道是否有人可以对此有所了解,也许还可以用这个例子回答 2 个部分?
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.我有一个 Powershell 脚本,我像批处理文件一样运行,我有它从一个单独的文件捕获到日志的 output 值。 This particular part is not outputting in the transcript where I get the DB version of a database.这个特定部分没有在我获得数据库的 DB 版本的成绩单中输出。 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. eg:我尝试了"
的不同位置,单独使用$DBVersion
,这是一种简单的方法来展示我遇到的问题。例如:
## 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:在我的日志文件中,我得到了 output:
Debug: 2.16.51443.5147调试: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?这告诉我该变量包含一个值作为写入主机输出它,但是当使用Write-Information
时它不会在日志中显示任何内容,我使用的所有其他变量都显示,为什么$DBVersion.value
或$DBVersion
不显示请显示什么?
Also the second part is, why do I have to use:第二部分也是,为什么我必须使用:
$DBVersion.value
Outside of the write-host ""
quotes?在write-host ""
引号之外?
Many thank in Advance非常感谢提前
As @abraham said in the comments.正如@abraham 在评论中所说。 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)"
.我所要做的就是将变量放在引号内(我的问题 2)是使用子表达式运算符$()
来扩展引号内的值: Write-Host "Debug: $($DBVersion.value)"
。 The same goes for your Write-Information
.您的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.单独这样做也解决了我最初的问题,即为什么Write-Information
没有 output 任何东西进入事务日志,我不需要更改 $InformationPreference。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.