【问题标题】:Retrieve a specific field from the result of Get-ItemProperty in Powershell从 Powershell 中 Get-ItemProperty 的结果中检索特定字段
【发布时间】:2019-05-10 05:44:45
【问题描述】:

我有以下 powershell 脚本:

$registrypath = "HKLM:\SYSTEM\CurrentControlSet\Control\DeviceGuard"
$Name = "EnableVirtualizationBasedSecurity"
$ExpectedValue = "1"
$value = Get-ItemProperty -Path $registrypath -Name $Name

Write-Host($value)

它的输出是:

    @{EnableVirtualizationBasedSecurity=1; PSPath=Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\DeviceGuard; PSParentPath=Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control; PSChildName=DeviceGuard; PSDrive=HKLM; 
PSProvider=Microsoft.PowerShell.Core\Registry}

我想将其中的 EnableVirtualizationBasedSecurity 字段的值获取到我的 powershell 脚本中的一个变量中。

点赞$SpecificFieldValue = $value.get(EnableVirtualizationBasedSecurity);

我如何在 powershell 中做到这一点?

【问题讨论】:

    标签: windows powershell powershell-4.0


    【解决方案1】:

    Get-ItemProperty 为您提供PSCustomObject 作为响应。

    这意味着您可以像这样直接获取属性的值:

    $value.EnableVirtualizationBasedSecurity
    

    或直接将值保存在Get-ItemProperty-call 中,如下所示:

    (Get-ItemProperty -Path $registrypath -Name $Name).EnableVirtualizationBasedSecurity
    

    或者像这样

    Get-ItemProperty -Path $registrypath -Name $Name | Select-Object -Expandproperty EnableVirtualizationBasedSecurity
    

    我认为问题在于,您希望响应是 hashtable 而不是 PSCustomObject

    您可以通过在调用周围添加() 并调用getType() 方法来获取有关响应的ObjectType 的信息:

    (Get-ItemProperty -Path $registrypath -Name $Name).GetType()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 2021-09-23
      • 2023-02-08
      • 1970-01-01
      相关资源
      最近更新 更多