0

Good afternoon all!

May I get some assistance, or at least pointed in the right direction with the below script?

Im working with the Dell BIOS and have successfully been able to get what we're looking for working as far as, getting the remote PC to set a certain category to Disabled, or Enabled. Now the issue is, when trying to create the script, the variable for the selection does pass through and just loops to the first colum of selections. See below:


$PSSession = New-PSSession -ComputerName $CName
Write-Host "Please wait will module is loaded"


Invoke-Command -Session $PSSession {Import-Module DellBIOSProvider} 
Start-Sleep 1

$FConfigs = Invoke-Command -Session $PSSession { Get-ChildItem -path "DellSMBios:\" | Select-Object -ExpandProperty Category}

for ($i=0; $i -lt $FConfigs.Count; $i++) {
  Write-Host "$($i): $($FConfigs[$i])"
  }

$selection1 = Read-Host "Enter the number you'd like to change"
$selection =  $Fconfigs[$selection1]

Start-Sleep 2
$SConfigs = Invoke-Command -Session $PSSession { Get-ChildItem -path "DellSMBios:\$selection" | Select-Object -ExpandProperty Category}
for ($i=0; $i -lt $SConfigs.Count; $i++) {
  Write-Host "$($i): $($SConfigs[$i])"
  }

$selection1 = Read-Host "Enter the number you'd like to change"
$selection =  $Sconfigs[$selection1]
"$selection"        

So in the selection of $FConfigs, $selection gets the appropriate selection but, it doesn't pass through to $SConfigs. That's because, it shows the exact same selection of $FConfigs in $Sconfigs. Please note: I have verified that whatever selection i choose, does have properties in it, so gci "dellsmbios:\$selection" should work.

Hopefully this makes sense, and someone may be able to tell me what i may be doing wrong.

Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24
  • In short: Because the script block executes _remotely_, it knows nothing about the _caller_'s variables. The simplest way to reference the caller's variable values is with the `$using:` scope - see [this answer](https://stackoverflow.com/a/35492616/45375). – mklement0 Jan 14 '21 at 23:17

1 Answers1

2
$SConfigs = Invoke-Command -Session $PSSession { Get-ChildItem -path "DellSMBios:\$selection" | Select-Object -ExpandProperty Category}

In this line, change $selection to $using:selection.

You need to do this when using local variables in a remote session. You can read more about it here under 'Using local variables':

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-7.1

mklement0
  • 382,024
  • 64
  • 607
  • 775
Ianteu
  • 104
  • 4