繁体   English   中英

将检测客户端和服务器上已安装软件的 Powershell 脚本

[英]Powershell script which will detect installed software on clients and servers

我的 powershell 模块无法正常工作。 我使用了Microsoft Gallery 中的一个脚本,我对其进行了修改,因为众所周知,在 Windows 上,您可以找到两个不同的注册表路径,其中已安装软件的列表所在的位置。

所以这是我的 psm1 脚本,当针对我们域中的不同主机名时,它总是返回相同的结果,我自己的 PC 软件列表:

Function Get-OSCInstalledApplication
{
<#
    .SYNOPSIS
        Get-OSCInstalledApplication is an advanced function which can be used to get installed application on local or remote computer.
    .DESCRIPTION
        Get-OSCInstalledApplication is an advanced function which can be used to get installed application on local or remote computer.
    .PARAMETER  ComputerName
        Gets the installed application on the specified computers. 
    .PARAMETER  ComputerFilePath
        Specifies the path to the CSV file. This file should contain one or more computers. 
    .EXAMPLE
        C:\PS> Get-OSCInstalledApplication -ComputerName "Server201201","Server201202"

        This command will list installed application on 'Server201201' and 'Server201202'.
    .EXAMPLE
        C:\PS> Get-OSCInstalledApplication -ComputerFilePath C:\Script\ComputerList.csv

        This command specifies the path to an item that contains several computers. Then 'Get-OSCInstalledApplication' cmdlet will list installed application from thoese computers.
    .EXAMPLE
        C:\PS> Get-OSCInstalledApplication -ComputerName "Server201201" | Export-Csv -Path C:\installedApps.csv

        This command will list installed application on 'Server201201' and saves the strings in a CSV file.
#>
    [CmdletBinding(DefaultParameterSetName='SinglePoint')]
    Param
    (
        [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName="SinglePoint")]
        [Alias('CName')][String[]]$ComputerName,
        [Parameter(Mandatory=$true, Position=0, ParameterSetName="MultiplePoint")]
        [Alias('CNPath')][String]$ComputerFilePath
    )

    If($ComputerName)
    {
        Foreach($CN in $ComputerName)
        {
            #test compter connectivity
            $PingResult = Test-Connection -ComputerName $CN -Count 1 -Quiet
            If($PingResult)
            {
                FindInstalledApplicationInfo -ComputerName $CN
            }
            Else
            {
                Write-Warning "Failed to connect to computer '$ComputerName'."
            }
        }
    }

    If($ComputerFilePath)
    {
        $ComputerName = (Import-Csv -Path $ComputerFilePath).ComputerName

        Foreach($CN in $ComputerName)
        {
            FindInstalledApplicationInfo -ComputerName $CN
        }
    }

    If($ComputerName)
    {
        Foreach($CN in $ComputerName)
        {
            #test compter connectivity
            $PingResult = Test-Connection -ComputerName $CN -Count 1 -Quiet
            If($PingResult)
            {
                FindInstalledApplicationInfo1 -ComputerName $CN
            }
            Else
            {
                Write-Warning "Failed to connect to computer '$ComputerName'."
            }
        }
    }

    If($ComputerFilePath)
    {
        $ComputerName = (Import-Csv -Path $ComputerFilePath).ComputerName

        Foreach($CN in $ComputerName)
        {
            FindInstalledApplicationInfo1 -ComputerName $CN
        }
    }
}

Function FindInstalledApplicationInfo($ComputerName)
{
    $Objs = @()
    $RegKey = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"

    $InstalledAppsInfos = Get-ItemProperty -Path $RegKey

    Foreach($InstalledAppsInfo in $InstalledAppsInfos)
    {
        $Obj = [PSCustomObject]@{Computer=$ComputerName;
                                 DisplayName = $InstalledAppsInfo.DisplayName;
                                 DisplayVersion = $InstalledAppsInfo.DisplayVersion;
                                 Publisher = $InstalledAppsInfo.Publisher}
        $Objs += $Obj
    }
    $Objs | Where-Object { $_.DisplayName } 
}

Function FindInstalledApplicationInfo1($ComputerName)
{
    $Objs1 = @()
    $RegKey1 = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"

    $InstalledAppsInfos1 = Get-ItemProperty -Path $RegKey1

    Foreach($InstalledAppsInfo1 in $InstalledAppsInfos1)
    {
        $Obj1 = [PSCustomObject]@{Computer=$ComputerName;
                                 DisplayName = $InstalledAppsInfo1.DisplayName;
                                 DisplayVersion = $InstalledAppsInfo1.DisplayVersion;
                                 Publisher = $InstalledAppsInfo1.Publisher}
        $Objs1 += $Obj1
    }
    $Objs1 | Where-Object { $_.DisplayName } 
}

该脚本确实会查询您的本地计算机:

 $Objs = @()
 $RegKey = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
 $InstalledAppsInfos = Get-ItemProperty -Path $RegKey

Get-ItemProperty -Path $RegKey是指本地计算机。

您在调用该函数时作为参数传递的 Computername 仅用于构建报告,该报告始终指的是您的本地计算机

您需要实现调用命令和管理的结果,或使用预煮脚本如这一个

这是一个示例实现

$outputfile = "$env:userprofile\Servers_$(get-date -f yyyy-MM-dd).log"

$computers = @('host1','host2','host3','host4')
# or
# $computers = get-content HostsListOneperLine.txt
$computers | % {
    write-output "processing $_" # remove if not needed
    if ( test-connection -computername $_ -count 1 -quiet ) {
        Invoke-Command -ComputerName $_ -ScriptBlock {
                Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate 
            }
    }
} | Out-File "$outputfile" -Append 

暂无
暂无

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

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