How can you use an object's property in a double-quoted string?

前端 未结 4 2051
耶瑟儿~
耶瑟儿~ 2020-11-21 07:18

I have the following code:

$DatabaseSettings = @();
$NewDatabaseSetting = \"\" | select DatabaseName, DataFile, LogFile, LiveBackupPath;
$NewDatabaseSetting.         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 07:39

    @Joey has the correct answer, but just to add a bit more as to why you need to force the evaluation with $():

    Your example code contains an ambiguity that points to why the makers of PowerShell may have chosen to limit expansion to mere variable references and not support access to properties as well (as an aside: string expansion is done by calling the ToString() method on the object, which can explain some "odd" results).

    Your example contained at the very end of the command line:

    ...\$LogFileName.ldf
    

    If properties of objects were expanded by default, the above would resolve to

    ...\
    

    since the object referenced by $LogFileName would not have a property called ldf, $null (or an empty string) would be substituted for the variable.

提交回复
热议问题