首页 > 解决方案 > 使用变量名 powershell 访问对象

问题描述

我无法使用.PropertyName.

我尝试过使用$val.Options.$propertyName,但没有产生任何结果。

$propertyName是从文件输入的值

`$val.$propertyName` results in "Cannot index into null array"

$result = New-Object -TypeName 'System.Collections.ArrayList';
        foreach ($user in $users) {
            $val = Get-LocalUser $user | Select *
            $val = $val.$propertyName
            $result.Add($val)
        }

标签: powershell

解决方案


在您的上下文中 $val.$propertyName 并不意味着您可以尝试任何事情:

$result = New-Object -TypeName 'System.Collections.ArrayList';
        foreach ($user in $users) {
            $val = Get-LocalUser $user
            $result.Add($val)
        }

$result 将是一个“Localuser”数组。


推荐阅读