powershell - Format-Table 忘记了一些属性,但 Format-List 显示了所有属性。为什么?

标签 powershell registry powershell-3.0

鉴于以下PowerShell 3脚本,Format-Table不会将所有属性列为列(它会跳过 NoRemove),但是 Format-List可以,您可以使用 Select-Object 强制属性存在.

Out-GridView行为与 Format-Table 相同,也会跳过 NoRemove

这是为什么?

注意:这是来自限制较少的 Where-Object 子句,它看起来像 Format-Table 检查的不仅仅是数组中的第一个对象猜列。

例子来自Channel 9 how-to: Print/List installed ​programs/ap​plications sorted by date忘记将第一个 Get-ItemProperty (gp) 初始化为一个数组,所以你会得到这样的错误:

Method invocation failed because [Microsoft.Win32.RegistryKey] doesn't contain a method named 'op_Addition'.

示例代码:

$nonUninstallableSoftwareRegistryKeys = (@(Get-Item HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*)) + 
(Get-Item HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*) + 
(Get-Item HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*) | 
  Where-Object { $_.ValueCount -eq 1 }

$nonUninstallableSoftwareRegistryKeys.GetType().FullName
$nonUninstallableSoftwareRegistryKeys | Get-Member

$nonUninstallableSoftwareRegistryNameValues = $nonUninstallableSoftwareRegistryKeys | 
  Get-ItemProperty

$nonUninstallableSoftwareRegistryNameValues.GetType().FullName
$nonUninstallableSoftwareRegistryNameValues | Get-Member

$nonUninstallableSoftwareRegistryNameValues |
  Format-Table

$nonUninstallableSoftwareRegistryNameValues |
  Format-List

$nonUninstallableSoftwareRegistryNameValues |
  Select-Object SystemComponent, NoRemove, PSPath, PSParentPath, PSChildName, PSProvider |
  Format-Table

我用了GetType() . FullNameGet-Member检查底层类型。

$nonUninstallableSoftwareRegistryKeys 从所有已安装的软件(用户、系统 x64 和系统 x86)开始,这些软件由只有 1 个值的注册表项过滤(根据经验,这些是您无法卸载的)。

输出的第一部分显示 $nonUninstallableSoftwareRegistryKeysMicrosoft.Win32.RegistryKey 类型的 System.Object[]与所有合适的成员。因此能够执行 Where-ObjectValueCount 上过滤属性,即使代码完成没有显示。

$nonUninstallableSoftwareRegistryKeys 也公开了一些 PowerShell "Extended Type System" NoteProperty属性包括 Property,其中包含键下的注册表名称/值对和来自注册表提供程序的一堆 PS*

$nonUninstallableSoftwareRegistryNameValues 也是一个 System.Object[] 但现在是类型 System.Management.Automation.PSCustomObject因为 Get-ItemProperty它将每个 $nonUninstallableSoftwareRegistryKeys 项的 Property 中的名称/值对扩展到属性中。对于我输出中的第一项,它添加了 SystemComponent属性(property)。对于第二项,它添加了 NoRemove .它添加了一堆来自注册表提供商的 PS*

Format-Table 输出:

SystemComponent PSPath                                                                                                                                    PSParentPath                        
--------------- ------                                                                                                                                    ------------                        
              1 Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager             Microsoft.PowerShell.Core\Registr...
                Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\WIC                            Microsoft.PowerShell.Core\Registr...
              1 Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager Microsoft.PowerShell.Core\Registr...
                Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\WIC                Microsoft.PowerShell.Core\Registr...

Format-List 输出:

SystemComponent : 1
PSPath          : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager
PSParentPath    : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall
PSChildName     : Connection Manager
PSProvider      : Microsoft.PowerShell.Core\Registry

NoRemove     : 1
PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\WIC
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall
PSChildName  : WIC
PSProvider   : Microsoft.PowerShell.Core\Registry

SystemComponent : 1
PSPath          : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager
PSParentPath    : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
PSChildName     : Connection Manager
PSProvider      : Microsoft.PowerShell.Core\Registry

NoRemove     : 1
PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\WIC
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
PSChildName  : WIC
PSProvider   : Microsoft.PowerShell.Core\Registry

选择对象输出:

SystemComponent NoRemove PSPath                                                                                                                                    PSParentPath               
--------------- -------- ------                                                                                                                                    ------------               
              1          Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager             Microsoft.PowerShell.Cor...
                1        Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\WIC                            Microsoft.PowerShell.Cor...
              1          Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager Microsoft.PowerShell.Cor...
                1        Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\WIC                Microsoft.PowerShell.Cor...

编辑:我的环境

PS C:\Users\Developer> Get-CimInstance Win32_OperatingSystem | Select-Object Version, Caption | Format-List
$PSVersionTable

Version : 6.2.9200
Caption : Microsoft Windows 8 Pro

Name                           Value                                                                                                                                                          
----                           -----                                                                                                                                                          
PSVersion                      3.0                                                                                                                                                            
WSManStackVersion              3.0                                                                                                                                                            
SerializationVersion           1.1.0.1                                                                                                                                                        
CLRVersion                     4.0.30319.18051                                                                                                                                                
BuildVersion                   6.2.9200.16628                                                                                                                                                 
PSCompatibleVersions           {1.0, 2.0, 3.0}                                                                                                                                                
PSRemotingProtocolVersion      2.2   

这两个返回同一个表:

$nonUninstallableSoftwareRegistryNameValues |
  Format-Table

$nonUninstallableSoftwareRegistryNameValues |
  Format-Table *                                                                                                                                                         

最佳答案

如果您没有为所有属性名称或“*”指定,Format-Table 将默认只打印前 4 个($FormatEnumerationLimit< 的默认值), 不确定为什么在 ft 输出中只得到三个。 Format-List 仅当对象类型没有 Format-List 的格式 View 时才会显示所有内容。

关于powershell - Format-Table 忘记了一些属性,但 Format-List 显示了所有属性。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18019633/

相关文章:

csv - 使用Powershell将集合保存到CSV时出现问题

powershell-3.0 - 如何使用默认浏览器打开两个html文件

powershell - 在ps1中查找函数并调用

azure - 您的 Azure 凭据尚未设置或已过期,请运行 Connect-AzAccount 来设置您的 Azure 凭据

c# - 即使使用 Microsoft.Win32 也找不到RegistryKey-Class

c++ - 如何将 REG_SZ 类型的注册表值解析为变量?

docker - 从/插入 Nexus 3 Docker-Registry 时出现 MissingBlobException

提供数组属性的数组的 Powershell 导出为 CSV

powershell - 使用 Powershell 安装 Sitecore 更新包

arrays - 将数组作为单独的参数传递给 PowerShell 脚本