none
variable is not work RRS feed

  • Question

  • I have a simple script to get remote PC' installed program

    $computername = read-host -Prompt 'Input computer name'

    $package = read-host -Prompt 'Input program name'

    invoke-Command -cn $computername -ScriptBlock {
    Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.displayname -match "$package"} | Select-Object DisplayName, Publisher, InstallDate

    The filter $package is not work. always get all program whatever input Java, Office

    Please help

    Thanks!

    Eric


    Thursday, May 25, 2017 4:21 PM

Answers

All replies

  • Your goal seems to be to examine what programs are installed on a remote computer.

    Use this instead:

    Windows IT Pro - Auditing 32-Bit and 64-Bit Applications with PowerShell


    -- Bill Stewart [Bill_Stewart]

    Thursday, May 25, 2017 5:03 PM
    Moderator
  • Don't use quotes.

    Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
          Where-Object { $_.displayname -match $package} |


    \_(ツ)_/

    Thursday, May 25, 2017 5:29 PM
  • tried "Don't use quotes." still not work
    Friday, May 26, 2017 2:54 PM
  • Your goal seems to be to examine what programs are installed on a remote computer.

    Use the script from this article:

    Windows IT Pro - Auditing 32-Bit and 64-Bit Applications with PowerShell


    -- Bill Stewart [Bill_Stewart]

    Friday, May 26, 2017 3:21 PM
    Moderator
  • The problem is you need to use -ArgumentList parameter to pass values into the scriptBlock and pass that value into the 'where' statement. Check the below code:

    $computername = read-host -Prompt 'Input computer name'
    $package = read-host -Prompt 'Input program name'

    invoke-Command -cn $computername -ScriptBlock { $Pkg = $Args[0]; Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.displayname -match $Pkg}  -ArgumentList $package | Select-Object DisplayName, Publisher, InstallDate

    Sunday, May 28, 2017 5:00 PM
  • I tried your script, but got error

    A parameter cannot be found that matches parameter name 'ArgumentList'.
        + CategoryInfo          : InvalidArgument: (:) [Where-Object], ParameterBindingException
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.WhereObjectCommand
        + PSComputerName        : Sel0917

    Tuesday, June 6, 2017 2:25 PM
  • if modify script to 

    $computername = read-host -Prompt 'Input computer name'
    $package = read-host -Prompt 'Input program name'

    invoke-Command -cn $computername -ScriptBlock {$Pkg = $Args[0];Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.displayname -match $pkg} | Select-Object DisplayName, Publisher, InstallDate} -ArgumentList $package

    It is work. 

    Thanks very much Phyoe!

    Tuesday, June 6, 2017 2:49 PM