# Need help sorting Variables for remote installation

**URL:** <https://forums.powershell.org/t/need-help-sorting-variables-for-remote-installation/23287>\
**Category:** PowerShell Help\
**Created:** [December 13, 2023, 6:44pm UTC](https://forums.powershell.org/t/need-help-sorting-variables-for-remote-installation/23287 "2023-12-13T18:44:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![xml](https://avatars.discourse-cdn.com/v4/letter/x/96bed5/32.png) [@xml](https://forums.powershell.org/u/xml)\
**Post date:** [December 13, 2023, 6:44pm UTC](https://forums.powershell.org/t/need-help-sorting-variables-for-remote-installation/23287/1 "2023-12-13T18:44:57Z")

</div>

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:

```auto
#==============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.

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [December 13, 2023, 7:05pm UTC](https://forums.powershell.org/t/need-help-sorting-variables-for-remote-installation/23287/2 "2023-12-13T19:05:00Z")

</div>

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](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-7.3)

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [December 13, 2023, 7:08pm UTC](https://forums.powershell.org/t/need-help-sorting-variables-for-remote-installation/23287/3 "2023-12-13T19:08:32Z")

</div>

> [@xml](#):
>
> `"\\Sparky\Endpoint\\Mozilla Firefox\$($newFirefox.Name)"`

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

> **[The Infamous Double-Hop Problem in PowerShell](https://www.progress.com/blogs/the-infamous-double-hop-problem-in-powershell)**
>
> In this article, we will discuss how to get around the infamous double-hop problem in PowerShell.

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [December 13, 2023, 7:47pm UTC](https://forums.powershell.org/t/need-help-sorting-variables-for-remote-installation/23287/4 "2023-12-13T19:47:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xml](https://avatars.discourse-cdn.com/v4/letter/x/96bed5/32.png) [@xml](https://forums.powershell.org/u/xml)\
**Post date:** [December 13, 2023, 9:35pm UTC](https://forums.powershell.org/t/need-help-sorting-variables-for-remote-installation/23287/5 "2023-12-13T21:35:19Z")

</div>

> [@neemobeer](#):
>
> Best to copy the

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [December 13, 2023, 11:12pm UTC](https://forums.powershell.org/t/need-help-sorting-variables-for-remote-installation/23287/6 "2023-12-13T23:12:51Z")

</div>

Yea Neemo is exactly right here.

```auto
    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](https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/ps-remoting-second-hop?view=powershell-7.4)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [December 18, 2023, 3:13am UTC](https://forums.powershell.org/t/need-help-sorting-variables-for-remote-installation/23287/7 "2023-12-18T03:13:34Z")

</div>


