How to access a property from an object using a va

2020-04-09 03:33发布

问题:

This works:

$psISE.Options.DebugBackgroundColor = '#FFC86400'

This doesn't:

$attribute = 'DebugBackgroundColor' 
($psISE.Options)[$attribute] = '#FFC86400'

ERROR: Unable to index into an object of type Microsoft.PowerShell.Host.ISE.ISEOptions

I want to set option attributes in a foreach loop using the $attribute variable.

Is there a way to do this?

回答1:

Just use double quotes after the dot:

$attribute = 'DebugBackgroundColor'
$psISE.Options."$attribute"