首页 > 解决方案 > 如何通过 PS 远程访问多台远程计算机以发现一个特定的应用程序并确定每个远程设备上的版本号?无输出

问题描述

仅供参考:我对 PS 非常陌生,我将其用作学习机会。同样,我试图在多个远程设备的列表中查找特定应用程序,并确定其相应主机系统上的应用程序版本号。我通过注册表查询尝试了这个(发现这很有挑战性),然后我使用了 Get-WMIObject。到目前为止,我正在使用它作为我的脚本。它没有产生任何输出;相反,它返回到命令提示符,没有错误或消息。

在多个远程设备中查找特定应用程序和版本的脚本:

    $Servers = Get-Content -Path C:\\files\Serverlist.txt

    $CIMSession = New-CIMSession -ComputerName $Servers Get-Credentials

    $Vendor = "App Name"


    foreach($Serv in $Servers) {

    If(Test-Connection -ComputerName $Serv -Count 1 -Quiet) {

    $Status = Get-Ciminstance Win32_Product -Computername $Serv | Where-object {$_.Version -contains 
    $Vendor}

    if($Status) {

    Out-file -Filepath C:\\files\AppVerResults.txt

    }

    }

    }

我还尝试调整脚本的以下部分,如下所示,但它向我显示错误“Get-CimInstance:访问被拒绝”。此错误消息是由于组策略引起的吗?我可以通过 RDP 远程访问与消息对应的设备。

    if($Status) {

     $Servers + " - "

    $Status | Out-file -Filepath C:\\files\AppVerResults.txt

    }

    }

    }

我应该通过调用命令还是注册表查询来解决它?我正在慢慢收拾东西,所以我会继续我的研究,但我希望在此期间得到一些建议。

标签: powershellscriptingpowershell-remoting

解决方案


我仍然相信搜索注册表是更简单的方法,除非您有.exe的特定文件路径。

使用此功能可在远程或本地 PC 上查找软件。通过指定(查找)有一个过滤器选项。-SoftwareName

  • Find-Software -ComputerName Remote_ComputerName -SoftwareName 'SQL'

还接受管道输入,以及要查询的多个计算机名称。

  • Find-Software -ComputerName ComputerOne, ComputerTwo, ComputerThree -SoftwareName 'SQL'
  • 'ComputerOne','ComputerTwo' | Find-Software -SoftwareName 'SQL'
  • Export-*通过管道连接到cmdlet也允许导出。

继承人的代码:

Function Find-Software {
[cmdletBinding()]
    Param(
        [Parameter(Mandatory=$false,
                   ValueFromPipeLine=$true,
                   ValueFromPipeLineByPropertyName=$true)]
        [Alias('cn','name')]
        [string[]]$ComputerName = $env:COMPUTERNAME,
        
        [Parameter(Mandatory=$false)]
        [String]$SoftwareName
         )
Begin{

    #Get Computer Names to check software version for
    $Server_List = Get-Content -Path "C:\files\Serverlist.txt"
    
    #Get Credentials for Script Scope once.
    $Credentials = Get-Credential

}

Process{
    if($PSBoundParameters.ContainsKey('SoftwareName')){
        foreach($Computer in $ComputerName){
            Try{

            $PSSession = New-PSSession -ComputerName $Computer -Credential $Credentials -EnableNetworkAccess -ErrorAction Stop

            $Software_List = Invoke-Command -ScriptBlock { 
                                Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",
                                                 "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" } -Session $PSSession

            $Software_List = $Software_List | Where-Object -FilterScript {$_.DisplayName -match $SoftwareName} | Sort-Object -Property DisplayName
                foreach($Software in $Software_List){
                    if($Software){
                    [PSCustomObject]@{
                            "Computer Name" = $Computer
                            "Software Name" = $Software.DisplayName
                            "  Version  "   = $Software.DisplayVersion
                            }
                        } else {
                    [PSCustomObject]@{
                            "Computer Name" = $Computer
                            "Software Name" = "Not found"
                            "  Version  "   = $null
                        }
                    }
                }

            } Catch {
                "Unable to connect to PC: $Computer"
                "Error: $($Error[0].Message.Split('.')[1].Trim())"
            }
        }
    } else {
        foreach($Computer in $ComputerName){
            Try{

            $PSSession = New-PSSession -ComputerName $Computer -Credential $Credentials -EnableNetworkAccess -ErrorAction Stop

            $Software_List = Invoke-Command -ScriptBlock { 
                                Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",
                                                 "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" } -Session $PSSession
                                                 
            $Software_List = $Software_List | Sort-Object -Property DisplayName
                foreach($Software in $Software_List){
                    [PSCustomObject]@{
                            "Computer Name" = $Computer
                            "Software Name" = $Software.DisplayName
                            "  Version  "   = $Software.DisplayVersion
                            }
                        }
                    } Catch {
                        "Unable to connect to PC: $Computer"
                        "Error: $($Error[0].Message.Split('.')[1].Trim())"
                    }
                }
            } #end ELSE statement
        } #end PROCESS block
    End {
        if(Get-PSSession){
            Get-PSSession | Remove-PSSession
        }
    } #end END block - Perform Session Clean Up
} #end FUNCTION

只需修改它以满足您的需求:)


推荐阅读