Powershell 变量没有通过?
大家下午好 我可以得到一些帮助,或者至少用下面的脚本指出正确的方向吗 我正在使用Dell BIOS,并且已经成功地获得了我们想要的工作,使远程PC将某个类别设置为禁用或启用。现在的问题是,当尝试创建脚本时,选择的变量确实会通过并循环到选择的第一列。见下文:Powershell 变量没有通过?,powershell,powershell-5.0,Powershell,Powershell 5.0,大家下午好 我可以得到一些帮助,或者至少用下面的脚本指出正确的方向吗 我正在使用Dell BIOS,并且已经成功地获得了我们想要的工作,使远程PC将某个类别设置为禁用或启用。现在的问题是,当尝试创建脚本时,选择的变量确实会通过并循环到选择的第一列。见下文: $PSSession = New-PSSession -ComputerName $CName Write-Host "Please wait will module is loaded" Invoke-Comman
$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将$selection
更改为$use:selection
在远程会话中使用局部变量时,需要执行此操作。您可以在“使用局部变量”下阅读更多关于它的内容:
在此行中,使用:selection将$selection
更改为$use:selection
在远程会话中使用局部变量时,需要执行此操作。您可以在“使用局部变量”下阅读更多关于它的内容:
简而言之:因为脚本块是远程执行的,所以它对调用方的变量一无所知。引用调用方变量值的最简单方法是使用
$using:
作用域-请参阅。简而言之:因为脚本块是远程执行的,所以它对调用方变量一无所知。引用调用方变量值的最简单方法是使用$using:
作用域-请参见。嘿,对不起,我忘了将您的标记为答案。它确实有用!嘿,对不起,我忘了把你的答案记下来。它确实有用!