哈希表Get_Item返回空行 - Powershell

时间:2012-02-20 01:53:18

标签: powershell hashtable

$searchterm = read-host “Enter search term for uninstallers”
$uninstallers = get-childitem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
$founditems = $uninstallers | ? {(Get-ItemProperty -path (“HKLM:\”+$_.name) -name Displayname -erroraction silentlycontinue) -match $searchterm}
write-host “Searched registry for uninstall information on $searchterm”
write-host “——————————————”
$x = 0
$uninstallcommandtable = @{}
$uninstalldisplaytable = @{}
if ($founditems -eq $null) {“None found”} else {
    write-host “Found “($founditems | measure-object).count” item(s):`n”
    $founditems | % {
        $x = $x + 1
        Write-host "Item: $x"
        Write-host “Displayname: “$_.getvalue(“Displayname”)
        Write-host “Displayversion: “$_.getvalue(“Displayversion”)
        Write-host “InstallDate: “$_.getvalue(“InstallDate”)
        Write-host “InstallSource: “$_.getvalue(“InstallSource”)
        Write-host “UninstallString: “$_.getvalue(“UninstallString”)
    $uninstallcommandtable.Add($x, $_.getvalue(“UninstallString”))
    $uninstalldisplaytable.Add($x, $_.getvalue(“Displayname”))
        Write-host “`n”
    }
}

Write-host ($uninstalldisplaytable | Out-String)
$whichprogram = read-host "Which program do you want to uninstall?"
Write-host ($uninstallcommandtable.Get_Item($whichprogram) | Out-String)

由于某种原因,最后一个写主机返回一个空行。我在最后一个读取主机之前验证了测试输出,所以我知道$ uninstallcommandtable是正确的。任何想法都会很棒。

2 个答案:

答案 0 :(得分:2)

因为您的哈希表名称是System.Int32类型。这将告诉你:

$uninstallcommandtable.Keys | % {$_.GetType().FullName}

Read-Host正在设置System.String类型的变量。因此,您需要将字符串转换为System.Int32,如下所示:

Write-host $uninstallcommandtable.Get_Item([Int32] $whichprogram)

您也可以使用:

Write-host $uninstallcommandtable.Item([Int32] $whichprogram)

答案 1 :(得分:1)

或者,您可以在创建哈希条目时将密钥设为字符串:

$uninstallcommandtable.Add("$x", $_.getvalue(“UninstallString”))