【发布时间】: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