【问题标题】:Powershell script which will detect installed software on clients and serversPowershell 脚本将检测客户端和服务器上安装的软件
【发布时间】:2020-02-07 18:39:42
【问题描述】:

我的 powershell 模块无法正常工作。我使用了script from 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 } 
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

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

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

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

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

    您需要实现invoke-command并管理结果,或者使用预先编写好的脚本,例如this one

    这是一个示例实现

    $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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-09
      • 2018-01-27
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多