Error Passing Variable to Where-Object

Jack Ring 20 Reputation points
2024-04-18T15:55:12.9733333+00:00

I get no output when running the following command:

$Server = Read-Host -Prompt 'Input your Computer name'

$Appx1 = Read-Host -Prompt 'Enter Application Name'

If($Server)

    {

        Invoke-Command -ComputerName $Server -ScriptBlock { Get-AppxPackage -AllUsers | where { $_.Name -like "$Appx1" } | select Name,PackageUserInformation } | Format-List

    }

    

Else

    {

        Write-Host 'We are done'

    }

When I enter "Microsoft*" or "Microsoft" for $Appx1 variable I get nothing back. When I run it like this:

Invoke-Command -ComputerName $Server -ScriptBlock { Get-AppxPackage -AllUsers | where { $_.Name -like "Microsoft*" } | select Name,PackageUserInformation } | Format-List

Works fine.

Any idea? I am sure it is simple. Been several years working with PowerShell.

Thanks!

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,132 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,111 Reputation points
    2024-04-18T19:02:35.1566667+00:00

    There are two problems with your script. The first has been answered by @MotoX80 -- it's necessary to make the contents of the $Appx1 variable available within the script block which will be executed on a remote machine.

    Note, too, that the "-like" operator requires a wild-card in the right-hand operand unless you only wand an exact match. If an exact match is what you want it's usually better to use "-eq" instead of "-like" to avoid misinterpretation.

    The second is that you've used the Format-List within the script block. The formatting of the information by Format-List is intended for the host console.

    The Format-List (and Format-Table) send their output to Out-Default. It's Out-Default that uses a format file to figure out how to format the output and has to take the hosts abilities into account (e.g., line-wrapping longer lines).

    PowerShell determines what the "host" actually is, and the characteristics of the host.

    That output isn't sent to the "success" stream (i.e., STDOUT), it's sent to the host. But there is no host console when you run a command on a remote machine. So, if you want a string to be returned, pipe the Format-List into Out-String. But really, if you want to use Format-List, use it on the local machine and just let your script block return the PSCustomObject that the Select-Object creates. IOW, remove the "| Format-List" from the script block. Pipe the results of the Invoke-Command into the Format-List.

    Have a look at this: https://devblogs.microsoft.com/powershell/how-powershell-formatting-and-outputting-really-works/


1 additional answer

Sort by: Most helpful
  1. MotoX80 32,086 Reputation points
    2024-04-18T17:06:41.45+00:00

    The Appx1 variable is not visible in the scriptblock. Try this.

    where { $_.Name -like "$Using:Appx1" }
    
    

    See https://www.pdq.com/blog/invoke-command-and-remote-variables/