Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 对子项进行筛选和排序_Powershell_Sorting_Filtering_Get Childitem - Fatal编程技术网

Powershell 对子项进行筛选和排序

Powershell 对子项进行筛选和排序,powershell,sorting,filtering,get-childitem,Powershell,Sorting,Filtering,Get Childitem,我正在尝试选择注册表中具有DisplayName属性(按DisplayName排序)的所有卸载项。我本以为这会管用 $uninstall32 = 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall' $uninstall64 = 'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall' $uni

我正在尝试选择注册表中具有DisplayName属性(按DisplayName排序)的所有卸载项。我本以为这会管用

$uninstall32 = 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstall64 = 'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstallKeys = (Get-ChildItem "Registry::$uninstall32" | Where-Object {$_.DisplayName} | sort DisplayName) +
                 (Get-ChildItem "Registry::$uninstall64" | Where-Object {$_.DisplayName} | sort DisplayName)

foreach ($uninstallKey in $uninstallKeys) {
    $uninstallKey
}

但这是一无所获。如果删除Where对象,则会得到结果,但不会排序。我哪里出错了?

您可以将
Get ChildItem
命令导入
|Get ItemProperty
以获得所需的结果

也就是说,我遇到了一个问题,我的注册表中有一个无效的密钥。 为了避免这个可能的问题,我迭代了每个项目,并分别获得了属性

$uninstall32 = 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstall64 = 'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstallKeys = (Get-ChildItem "Registry::$uninstall32") +
(Get-ChildItem "Registry::$uninstall64")

$AllKeys = `
  Foreach ($key in $uninstallKeys) {
  try {
    $Value = $Key | Get-ItemProperty -ErrorAction Stop
    $Value
  }
  catch {
    Write-Warning $_
  }
}
$AllKeys  = $AllKeys  | WHere DisplayName -ne '' | sort displayname
参考


如果我正确理解了这个问题,您可能希望键路径作为字符串数组返回,或者希望键作为对象返回,包括所有属性:

$uninstall = 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*',
             'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'

$uninstallKeys = $uninstall | ForEach-Object {
    Get-ItemProperty "Registry::$_" | 
    Where-Object {$_.DisplayName} | 
    Sort-Object DisplayName | 
    # if you want the Keys Paths returned as string properties:
    Select-Object @{Name = 'RegistryKey'; Expression = {($_.PSPath -split '::')[1]}}

    # if you want the Keys returned with all properties as objects:
    # Select-Object $_.PSPath
}

$uninstallKeys

get childitem的输出是一种错觉。它实际上调用格式文件中的GetItemProperty。您需要使用GetItemProperty查看值和数据。您可能希望改用get package命令。请注意,Netbeans在安装时会生成一个无效的注册表dword项“NoModify”,这会在get-itemproperty中创建一个异常

下面是一种使用get-itemproperty的方法:

get-itemproperty hklm:\software\microsoft\windows\currentversion\uninstall\* | 
  where displayname | sort displayname

我甚至不认为
DisplayName
Microsoft.Win32.RegistryKey
对象的属性
$uinstallKeys[0]| Get Member
没有显示它。是的,看起来这应该是可能的,但感觉Microsoft从未实现过这样的事情,唯一的实际选择是获取所有内容,然后有条件地将其添加到以DisplayName为键的哈希表中,然后对哈希表进行排序。这是一项繁重的工作,似乎微软应该提供它。@管理员再检查一些,如
$uninstallKeys[0..9]
或诸如此类,您可能会遇到一个。这是我从未真正习惯的注册表的一个怪癖。在我看来,当你为一个文件系统提供商编写代码时,总是不得不捕捉到熊猫、天气预报和失望的概念中偶尔出现的异常——试图找到一个文件、文件夹或任何东西。这只是排序对象的一些奇怪行为<代码>排序对象如果没有为命令提供属性,则单独根据特定对象类型的默认属性集进行排序。但是
Sort Object DisplayName
Sort Object RandomString
都以相同的方式排序,这与根本不提供属性不同。这将导致三种不同的排序方案:默认、按实际属性排序,或假属性的不同视图。我认为实际查看
DisplayName
属性值需要额外的
Get-ItemProperty
Get-ItemProperty
是用于获取DisplayName属性和其他属性的cmdlet。。。这就是说,我遇到了一个类型为“Specified cast is invalid”的问题,因为有一个程序在那里用它不应该有的东西注册了他的密钥。为了避免这种情况,循环遍历每个项目,然后从try-catch中获取项目属性将非常有效。唯一的缺点是,当前方法将跳过格式无效的密钥(如果有)。我注意到排序中的一个小怪癖是,`Tools for.Net 3.5`的显示名确实以空白开头。如图所示。Netbeans在注册表中生成无效的dword值。