简体   繁体   English

尝试使用Powershell在活动目录中的OU中查找计算机,以查看它们是否为特定软件

[英]trying to use Powershell to look in an OU in active directory for computers to see if they specific software

I'm still pretty new to PowerShell, and am still learning how to use it. 我对PowerShell还是很陌生,并且仍在学习如何使用它。 I saw I could use a cmdlet named Get-WmiObject, but I'm not sure that's the right one to use. 我看到我可以使用一个名为Get-WmiObject的cmdlet,但是我不确定这是正确的选择。 I've seen people saying when using it that it might throw up errors on the user-end, which would confuse users. 我见过有人说使用它时,它可能会在用户端引发错误,这会使用户感到困惑。

So digging around, people are saying I can query the registry of all the computers in an OU, which would search here "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" and possibly here "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall". 因此,闲逛的人们说我可以在OU中查询所有计算机的注册表,该注册表将在此处搜索“ SOFTWARE \\ Microsoft \\ Windows \\ CurrentVersion \\ Uninstall”,并可能在此处搜索“ SOFTWARE \\ Wow6432Node \\ Microsoft \\ Windows \\ CurrentVersion \\ Uninstall” ”。 The problem I'm running into is I'm not sure which cmdlet to start out using (if a cmdlet at all), how to tell it to search in that specific registry, while also telling it to go through each computer in an OU checking. 我遇到的问题是我不确定要开始使用哪个cmdlet(如果根本没有cmdlet),如何告诉它在特定注册表中进行搜索,同时还告诉它要通过OU中的每台计算机?检查。

I need to search for all computers that have "Microsoft Office" installed. 我需要搜索所有安装了“ Microsoft Office”的计算机。 Can anyone point me in the right direction? 谁能指出我正确的方向? How do I go about doing this? 我该怎么做呢?

First of all, you need to get a list of computer names in the OU and for that you need to get the DistinghuishedName property of that OU. 首先,您需要在OU中获取计算机名称的列表,为此,您需要获取该OU的DistinghuishedName属性。 (look in ADUC -> OU Properties -> Attributes -> DistinghuishedName) (在ADUC中-> OU属性->属性-> DistinghuishedName中查找)

Using that, you can use the function below to test for installed software: 使用该功能,您可以使用下面的功能测试已安装的软件:

function Get-InstalledSoftware {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
        [string[]]$ComputerName = $env:COMPUTERNAME,

        [string]$NamePattern = '*',

        [switch]$ExcludeUpdates
    )
    begin {
        $UninstallPaths = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\',
                          'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\'
    }
    process {
        foreach ($computer in $ComputerName) {
            if ([string]::IsNullOrEmpty($computer) -or $computer -eq '.') { $computer = $env:COMPUTERNAME }

            # if the computername is its SamAccountName, it ends in a dollar sign.
            $computer = $computer -replace '\$$', ''

            if (!(Test-Connection -ComputerName $computer -Count 1 -Quiet)) {
                Write-Warning "Computer '$computer' cannot be reached."
                continue
            }

            $system  = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer
            $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$computer)
            foreach ($regPath in $UninstallPaths) {
                $key = $baseKey.OpenSubKey($regPath)
                # if the key exists
                if ($key) {
                    $key.GetSubKeyNames() | ForEach-Object {
                        $subKey      = $baseKey.OpenSubKey("$regPath$_")
                        $application = $subKey.GetValue('DisplayName')
                        if (($application) -and ($application -like $NamePattern)) {
                            if (!$ExcludeUpdates -or ($application -notlike "*update*")) {
                                [PSCustomObject]@{
                                    'Computer'        = $system.Name
                                    'Application'     = $application
                                    'Version'         = $subKey.GetValue('DisplayVersion')
                                    'InstallLocation' = $subKey.GetValue('InstallLocation')
                                    'UninstallString' = $subKey.GetValue('UninstallString')
                                    'Publisher'       = $subKey.GetValue('Publisher')
                                    'LoggedOnUser'    = $system.UserName
                                }
                            }
                        }
                        # close $subKey
                        if ($subKey)  { $subKey.Close() }
                    }
                    # close $key
                    if ($key)  { $key.Close() }
                }
            }
            # close $baseKey
            if ($baseKey)  { $baseKey.Close() }
        }
    }
}

Note: if you have PowerShell 3.0 or better, you can change the line $system = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer into $system = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer -Verbose:$false which will perform faster 注意:如果您具有PowerShell 3.0或更高版本,则可以将行$system = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer $system = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer -Verbose:$false$system = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer -Verbose:$false这将执行快点

Use it like this: 像这样使用它:

Import-Module ActiveDirectory

$OuDn = 'DistinghuishedName of the OU you are interested in'

# get a list of computer names in the given OU
$computers = Get-ADComputer -Filter * -SearchBase $OuDn | Select-Object -ExpandProperty Name

# use the function to get a list of installed applicationa
$software = Get-InstalledSoftware -ComputerName $computers -NamePattern "Microsoft Office*" -ExcludeUpdates

# output to console or export to a CSV file
$software | Export-Csv -Path 'D:\software.csv' -NoTypeInformation -Encoding UTF8

Note: this function does all the work on your pc, so you must make sure you are running it as a user that has permission to read the registry keys on all of the machines. 注意:此功能可以在您的PC上完成所有工作,因此必须确保以具有读取所有计算机上注册表项权限的用户身份运行它。

Also, in order for a key to be opened remotely, both the server and client machines must be running the remote registry service , and have remote administration enabled. 另外,为了远程打开密钥,服务器和客户端计算机都必须正在运行远程注册表服务 ,并且启用了远程管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM