已安装 PowerShell 搜索软件 [英] PowerShell Search software installed

查看:40
本文介绍了已安装 PowerShell 搜索软件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让这个 ForEach 循环使用注册表搜索来搜索计算机上安装的特定软件.出于某种原因,即使我知道并且可以看到它们已安装,它也只能找到一个而不是其他两个.有什么错过.

I am attempting to get this ForEach loop to search for specific software install on a computer using a registry search. For some reason it is only find one and not the other two even though I know and can see they are installed. What have a missed.

Clear-Host
$Computers = hostname

$array = @()

foreach($pc in $computers){

    $computername=$pc.computername

    #Define the variable to hold the location of Currently Installed Programs

    $UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" 

    #Create an instance of the Registry Object and open the HKLM base key

    $reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername) 

    #Drill down into the Uninstall key using the OpenSubKey Method

    $regkey=$reg.OpenSubKey($UninstallKey) 

    #Retrieve an array of string that contain all the subkey names

    $subkeys=$regkey.GetSubKeyNames() 

    #Open each Subkey and use GetValue Method to return the required values for each


    ForEach($Key in $subkeys) {
        $thisKey=$UninstallKey+"\\"+$key 

        $thisSubKey=$reg.OpenSubKey($thisKey) 
        #If found set variable to True for used in report

            if (($thisSubKey.GetValue("DisplayName") -like "CCleaner")) {Write-Host "CCleaner = True"}
            Else {Write-Host = " False"}

        if (($thisSubKey.GetValue("DisplayName") -like "*7-Zip*")) {Write-Host "7Zip = True"}
            Else {Write-Host = " False"}

        if (($thisSubKey.GetValue("DisplayName") -like "*.NET Framework*")) {Write-Host ".NET  = True"}
            Else {Write-Host = " False"}


    } 

}

它找到了 DotNet 并且它等于 True,但是 7-Zip 和 CCleaner 也安装在我正在寻找的地方.我看了这段代码有一段时间了,但不明白为什么.

It finds DotNet and its equals True, but the 7-Zip and CCleaner are also installed and in the place I am looking. I have a looked at this code for a while now and can not see why.

我知道我已将计算机设置为主机名,我将更改为包含计算机列表的文件.目前仅用于测试.

I know I have set the computers to hostname, I will change to a file with a list of computers. This is just for test at the moment.

先谢谢你.

推荐答案

我认为您所有的 If 语句都可以替换为 Switch 语句,这样可以更快更更好(一次调用从远程注册表中获取值而不是 3),而且它可以正则表达式匹配.考虑删除所有 3 个 If 语句并将它们替换为:

I think all of your If statements could be replaced with a Switch statement that would work faster and better (one call to get the value from the remote registry instead of 3), plus it can regex match. Consider removing all 3 of your If statements and replacing them with:

Switch -Regex ($thisSubKey.GetValue("DisplayName")){
    "CCleaner" {Write-Host "CCleaner = True";continue}
    "7-Zip" {Write-Host "7-Zip = True";continue}
    "\.Net Framework" {Write-Host ".Net Framework = True"}
}

这应该和你所有的 If 语句有效地做同样的事情

That should effectively do the same thing as all of your If statements

如何获取 32 位密钥......好吧,你已经有了从注册表中获取东西的代码,只需在切换后添加第二部分,基本上使用修改后的路径.更好的是,让我们制作一个从 DisplayName 值安装的所有应用程序的列表,将来自 Wow6432Node 部分的所有应用程序添加到该列表中,然后通过 Switch 运行整个列表.现在我假设,由于您在代码中引用了 computername 属性,因此您正在导入 CSV 并循环遍历它?我希望如此,我有点基于此.因此,这将在 CSV 格式的计算机中循环,其中计算机名称存储在 computername 属性中.它将为每台计算机添加 3 个新属性:CClean7-Zip.Net Framework.默认情况下,它将它们全部设置为 $false.然后它会拉取软件列表,如果找到其中的任何一个,它就会将该属性更改为 $true.

How to get the 32 bit keys... Well, you already have the code to get things from the registry, just tack on a second part after the switch basically with a modified path. Better yet, let's make a single list of all the applications installed from the DisplayName values, add all the ones from the Wow6432Node section to that list, and then run the whole list through the Switch. Now I assume that, since you are referencing a computername property in your code, you are importing a CSV and looping through that? I hope so, I kind of based this off that. So this will loop through computers in a CSV where the computer name is stored in a computername property. It will add 3 new properties to each computer: CClean, 7-Zip, and .Net Framework. It sets them all as $false by default. Then it pulls the software listings, and if it finds any of those it changes that property to $true.

Clear-Host
$Computers = @($(new-object PSObject -prop @{'ComputerName' = $env:COMPUTERNAME}))
##Computers = Import-CSV 'C:\Path\To\SCCMVerify.csv'

$array = @()

for($i = 0; $i -lt $computers.count;$i++){

    $computername=$computers[$i].computername
    Add-Member -InputObject $computers[$i] -NotePropertyName 'CCleaner' -NotePropertyValue $false
    Add-Member -InputObject $computers[$i] -NotePropertyName '7-Zip' -NotePropertyValue $false
    Add-Member -InputObject $computers[$i] -NotePropertyName '.Net Framework' -NotePropertyValue $false



    #Define the variable to hold the location of Currently Installed Programs

    $UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" 
    $Uninstall32Key="SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall" 

    #Create an instance of the Registry Object and open the HKLM base key

    $reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername) 

    #Drill down into the Uninstall key using the OpenSubKey Method

    $regkey=$reg.OpenSubKey($UninstallKey) 
    $reg32key=$reg.OpenSubKey($Uninstall32Key) 

    #Retrieve an array of string that contain all the subkey names

    $subkeys=$regkey.GetSubKeyNames() 
    $sub32keys=$reg32key.GetSubKeyNames() 

    #Open each Subkey and use GetValue Method to return the required values for each, compile that in a list

    $applications = $subkeys|ForEach{$reg.OpenSubKey("$UninstallKey\\$_").GetValue('DisplayName')}
    $applications += $sub32keys|ForEach{$reg.OpenSubKey("$Uninstall32Key\\$_").GetValue('DisplayName')}

    #Search all applications for matching software
    Switch -Regex ($applications) {
        "CCleaner" {Write-Host "CCleaner = True";$Computers[$i].CCleaner = $true ;continue}
        "7-Zip" {Write-Host "7-Zip = True";$computers[$i].'7-Zip' = $true;continue}
        "\.Net Framework" {Write-Host ".Net Framework = True";$Computers[$i].'.Net Framework' = $true}        
    } 

}

这篇关于已安装 PowerShell 搜索软件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆