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

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

I have the following code:

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


        
4条回答
  •  醉话见心
    2020-11-21 07:51

    @Joey has a good answer. There is another way with a more .NET look with a String.Format equivalent, I prefer it when accessing properties on objects:

    Things about a car:

    $properties = @{ 'color'='red'; 'type'='sedan'; 'package'='fully loaded'; }
    

    Create an object:

    $car = New-Object -typename psobject -Property $properties
    

    Interpolate a string:

    "The {0} car is a nice {1} that is {2}" -f $car.color, $car.type, $car.package
    

    Outputs:

    # The red car is a nice sedan that is fully loaded
    

提交回复
热议问题