powershell - 使用变量名 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)
}
解决方案
在您的上下文中 $val.$propertyName 并不意味着您可以尝试任何事情:
$result = New-Object -TypeName 'System.Collections.ArrayList';
foreach ($user in $users) {
$val = Get-LocalUser $user
$result.Add($val)
}
$result 将是一个“Localuser”数组。
推荐阅读
- html - 输入组中的按钮掉到一边?
- javascript - 如何清除地图上的所有标记?
- javascript - 文本中的值正确,但 .attr() 或 .html() 中的值不正确
- spring - 我可以使用最新的 Spring Batch 使用 Quartz Scheduler 1.8.6 吗?
- c# - GeckoWebBrowser 在任何 CPU 中崩溃
- c# - IIS 无法重新启动并出现超时错误
- java - .Jar 更新转换为 .exe(或 .dmg/ .deb)
- c# - 当我使用 pop3 时,获取的电子邮件未排序
- c# - 如何将 SqlClientFactory 实例插入 Entity Framework 6.2?
- paypal - PAYPAL 身份 API 不发送 account_type 属性