变量不通过?

Variable not passing through?

大家下午好!

我可以得到一些帮助,或者至少用下面的脚本指出正确的方向吗?

我正在使用 Dell BIOS,并且已经成功地让我们正在寻找的工作尽可能地让远程 PC 将某个类别设置为已禁用或已启用。现在的问题是,当尝试创建脚本时,选择的变量确实通过并循环到第一列选择。见下文:


$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"        

所以在 $FConfigs 的选择中,$selection 获得了适当的选择,但它没有传递给 $SConfigs。这是因为,它在 $Sconfigs 中显示了完全相同的 $FConfigs 选择。 请注意:我已经验证了我选择的任何选项都包含属性,因此 gci "dellsmbios:$selection" 应该有效。

希望这是有道理的,也许有人能告诉我我做错了什么。

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

在这一行中,将 $selection 更改为 $using:selection

在远程会话中使用局部变量时需要这样做。您可以在 'Using local variables':

下阅读更多相关信息

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