none
invoke-command not holding variable data RRS feed

  • Question

  • I have this code:

    $pc = Read-Host "Enter machine name"
    $software = Read-Host "Enter software to search for"
    $software
    Invoke-Command -ComputerName $pc -ScriptBlock {
    Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -like "*$software*" } | Select-Object DisplayName, DisplayVersion, UninstallString 
    Write-host "your software is $software"
    }

    Right after the Read-Host my $software variable has my data.  But in the Write-Host command $software is empty.  Why?  How/why does it get wiped out and how do you make it retain the data?


    mqh7

    Monday, August 15, 2016 8:13 PM

Answers

  • Do not edit the script.

    1) Copy Get-InstalledApp.ps1 (the one you downloaded) to a directory in your path.

    2) At the PowerShell prompt, type the command you want to run and press Enter:


    PS C:\> Get-InstalledApp -ComputerName remotecomputer

    That's all you need to do.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by mqh7 Wednesday, August 17, 2016 2:13 PM
    Tuesday, August 16, 2016 6:17 PM
    Moderator

All replies

  • You don't need Invoke-Command. Here's a better script for getting installed software:

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


    -- Bill Stewart [Bill_Stewart]

    Monday, August 15, 2016 8:25 PM
    Moderator
  • Thank you for the script.  But I'm still curious about my script.  How come Invoke-Command does not retain the $software variable data?   


    mqh7

    Monday, August 15, 2016 8:31 PM
  • You're not passing that information along.

    http://ss64.com/ps/invoke-command.html

    (Hint: -ArgumentList)


    Monday, August 15, 2016 8:36 PM

  • PS C:\> help about_Remote_Variables


    -- Bill Stewart [Bill_Stewart]

    Monday, August 15, 2016 8:42 PM
    Moderator
  • ok, I had my $software variable in the wrong place.   Now, how do I output my data?

    $pc = Read-Host "Enter machine name"
    Invoke-Command -ComputerName $pc -ScriptBlock {
    $software = Read-Host "Enter software to search for"
    $targetPC = $env:computername
    $drop = "\\MGApp-01\psscripts\logs"
    Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -like "*$software*" } | Select-Object DisplayName, DisplayVersion, UninstallString | out-file"$drop\$targetPC.txt"
    }

    I want to display my results from the remote computer, how do I do that?  Out-Gridview won't work here so I tired Out-File which also does not seem to work.

    Thank you!!


    mqh7

    Tuesday, August 16, 2016 2:32 PM
  • Use script I mentioned before. You don't need Invoke-Command.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, August 16, 2016 3:40 PM
    Moderator
  • I tried your command it does not work.     It does not seem to know what get-installedapp2 is.    At the very bottom of your script I add that line of code     get-installedapp2     I get this error.

    At line:207 char:1
    + get-installedapp2
    + ~~~~~~~~~~~~~~~~~
    Unexpected token 'get-installedapp2' in expression or statement.

    Also what I'm trying to do is find 1 installed app, not all of them.   Can I pass parms to your function?  

    • Edited by mqh7 Tuesday, August 16, 2016 3:58 PM
    Tuesday, August 16, 2016 3:54 PM
  • Do not edit the script.

    1) Copy Get-InstalledApp.ps1 (the one you downloaded) to a directory in your path.

    2) At the PowerShell prompt, type the command you want to run and press Enter:


    PS C:\> Get-InstalledApp -ComputerName remotecomputer

    That's all you need to do.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by mqh7 Wednesday, August 17, 2016 2:13 PM
    Tuesday, August 16, 2016 6:17 PM
    Moderator
  • Thank you !!

    mqh7

    Wednesday, August 17, 2016 2:13 PM