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

Powershell脚本,用于检测客户端和服务器上已安装的软件,powershell,Powershell,我的powershell模块无法正常工作。我使用了一个我已经修改过的注册表,因为我们都知道在Windows上,您可以找到两个不同的注册表路径,其中包含已安装软件的列表 因此,这里是我的psm1脚本,当目标与我们域中的不同主机名相对时,它总是返回相同的结果,即我自己的PC软件列表: Function Get-OSCInstalledApplication { <# .SYNOPSIS Get-OSCInstalledApplication is an advanced

我的powershell模块无法正常工作。我使用了一个我已经修改过的注册表,因为我们都知道在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 } 
}
函数获取OSC安装应用程序
{
获取OSCSInstalledApplication-ComputerName“Server201201”、“Server201202”
此命令将列出“Server201201”和“Server201202”上安装的应用程序。
.举例
C:\PS>Get-OSCSInstalledApplication-ComputerFilePath C:\Script\ComputerList.csv
此命令指定包含多台计算机的项目的路径。然后“Get-OSCInstalledApplication”cmdlet将列出这些计算机上已安装的应用程序。
.举例
C:\PS>获取OSCSInstalledApplication-ComputerName“Server201201”|导出Csv-路径C:\installedApps.Csv
此命令将列出“Server201201”上已安装的应用程序,并将字符串保存在CSV文件中。
#>
[CmdletBinding(DefaultParameterSetName='SinglePoint')]
Param
(
[参数(必需=$true,位置=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,ParameterSetName=“SinglePoint”)]
[别名('CName')][字符串[]]$ComputerName,
[参数(必需=$true,位置=0,参数setName=“MultiplePoint”)]
[别名('CNPath')][String]$ComputerFilePath
)
如果($ComputerName)
{
Foreach($ComputerName中的CN)
{
#测试计算机连接性
$PingResult=测试连接-计算机名$CN-计数1-安静
如果($PingResult)
{
FindInstalledApplicationInfo-计算机名称$CN
}
其他的
{
写入警告“无法连接到计算机“$ComputerName.”
}
}
}
如果($ComputerFilePath)
{
$ComputerName=(导入Csv-路径$ComputerFilePath)。ComputerName
Foreach($ComputerName中的CN)
{
FindInstalledApplicationInfo-计算机名称$CN
}
}
如果($ComputerName)
{
Foreach($ComputerName中的CN)
{
#测试计算机连接性
$PingResult=测试连接-计算机名$CN-计数1-安静
如果($PingResult)
{
FindInstalledApplicationInfo 1-计算机名称$CN
}
其他的
{
写入警告“无法连接到计算机“$ComputerName.”
}
}
}
如果($ComputerFilePath)
{
$ComputerName=(导入Csv-路径$ComputerFilePath)。ComputerName
Foreach($ComputerName中的CN)
{
FindInstalledApplicationInfo 1-计算机名称$CN
}
}
}
函数FindInstalledApplicationInfo($ComputerName)
{
$Objs=@()
$RegKey=“HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*”
$InstalledAppsInfos=获取项目属性-路径$RegKey
Foreach($InstalledAppsInfo中的$InstalledAppsInfo)
{
$Obj=[PSCustomObject]@{Computer=$ComputerName;
DisplayName=$InstalledAppsInfo.DisplayName;
DisplayVersion=$InstalledAppsInfo.DisplayVersion;
Publisher=$InstalledAppsInfo.Publisher}
$Objs+=$Obj
}
$Objs |其中对象{$\.DisplayName}
}
函数findInstalledApplicationInfo($ComputerName)
{
$Objs1=@()
$RegKey1=“HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*”
$InstalledAppsInfos1=获取项目属性-路径$RegKey1
Foreach($InstalledAppInfo1,单位为$InstalledAppInfos1)
{
$Obj1=[PSCustomObject]@{Computer=$ComputerName;
DisplayName=$InstalledAppsInfo1.DisplayName;
DisplayVersion=$InstalledAppsInfo1.DisplayVersion;
Publisher=$InstalledAppsInfo1.Publisher}
$Objs1+=$Obj1
}
$Objs1 |其中对象{$\.DisplayName}
}

脚本按原样查询您的本地计算机:

 $Objs = @()
 $RegKey = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
 $InstalledAppsInfos = Get-ItemProperty -Path $RegKey
获取项目属性-路径$RegKey指本地计算机

调用该函数时作为参数传递的Computername仅用于生成报告,该报告始终引用本地计算机

您需要实现invoke命令并管理结果,或者使用诸如

下面是一个示例实现

$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