Need help sorting Variables for remote installation

I am trying to create a script that does the following things:

  1. Connect to File share that contains versions of Firefox. My organization keeps up to 4 Firefox versions in folders details their Version number (i.e “120.0.1”, “120.0”, “119.0.1”, “119.0”)
  2. Sort the files from greatest number to smallest number
  3. Select the greatest number
  4. Run the .msi program in the selected folder to the remote host.

For Further Clarification:
File Share = \Sparky\Endpoint\Mozilla Firefox*
.msi Folder = \Sparky\Endpoint\Mozilla Firefox\120.0.1\Firefox Setup 120.0.1

My Current Script:

#==============Universal Variable==============#
# $ToNatural is used to sort the selected folder
$ToNatural = { $_.Name -replace '\D', '0' }
$Computer = Read-Host "Please Enter Computer Name"

#==============Firefox==============#

# Selects whatever directory that has the greatest number
$newFirefox = Get-ChildItem "\\Sparky\Endpoint\\Mozilla Firefox" | Where-Object { $_.Name -match '\d' } | Sort-Object $ToNatural -Descending | Select-Object -First 1

# Check if a folder was found
if ($newFirefox) {
    # Construct the absolute path to the Firefox folder
    $firefoxFolderPath = "\\Sparky\Endpoint\\Mozilla Firefox\$($newFirefox.Name)"
}
    # New variable that represents content of the selected folder
$Firefox = Get-ChildItem $firefoxFolderPath

# Run the firefox.msi file
if ($Firefox -and $Firefox.Count -gt 0) {
    Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process msiexec -ArgumentList "/i", "$Firefox", "/qn" -NoNewWindow} 
}

I edited some details for this post, so there may be slight errors. But essentially whenever I run this code on a remote computer, Firefox does not install. My assumption is that it because it is not running the .msi from the selected folder.

That’s because the local variable does not exist on the remote system. Check out the section about the ‘$Using’ varaible about Remote Variables - PowerShell | Microsoft Learn

You may also run into the infamous double hop issue using a UNC path on the remote system.

Good catch, that’s also likely a problem. Best to copy the installer to the remote machine first and then run it from there

That would probably be much better than what I was gonna do. I will try tomorrow and see what happens. Thanks guys! @tonyd

Yea Neemo is exactly right here.

    Invoke-Command -ComputerName $using:Computer -ScriptBlock {Start-Process msiexec -ArgumentList "/i", "$using:Firefox", "/qn" -NoNewWindow} 

Another way to think of is is, when you use Invoke-Command, you’re essentially starting a brand new powershell session on another system, so anything you’ve defined in your current session isn’t defined there. The ‘using’ scope modifier essentially gives you the ability to use a locally defined variable in the remote command.

Regarding the double hop problem, MS does have an official doc on it as well providing the options: Making the second hop in PowerShell Remoting - PowerShell | Microsoft Learn