PowerShell和copy paste中的Switch语句接受最后一个数组输出,而不是第一个数组输出

PowerShell和copy paste中的Switch语句接受最后一个数组输出,而不是第一个数组输出,powershell,powershell-4.0,Powershell,Powershell 4.0,PowerShell中的switch语句出现问题,不知道为什么会发生这种情况 我想复制的内容取决于开关的某种原因,我只有得到最后的输出像“D”时,我粘贴开关A 知道如何在PowerShell 4.0中继续工作吗?我被限制为4.0,因为我的学校不会在服务器上升级到PowerShell 5 [array]$a = "A", "B", "C", "D" $login = read-host login $switch = 'switch($login) {' for($i = 1; $i -le

PowerShell中的switch语句出现问题,不知道为什么会发生这种情况

我想复制的内容取决于开关的某种原因,我只有得到最后的输出像“D”时,我粘贴开关A

知道如何在PowerShell 4.0中继续工作吗?我被限制为4.0,因为我的学校不会在服务器上升级到PowerShell 5

[array]$a = "A", "B", "C", "D"

$login = read-host login

$switch = 'switch($login) {'

for($i = 1; $i -le $a.length; $i++)
{
    $switch += "`n`t$i { '$($test = $a[$i-1])  $([System.Windows.Clipboard]::SetText($test))'; break }" 
}

$switch += "`n}"

Invoke-Expression $switch
。听起来您实际上想要的是一个哈希表或类似的:

# added to reference the System.Windows namespace
Add-Type -AssemblyName PresentationFramework

$options = @{
    A = 'this thing'
    B = 'That thing'
    C = 'Another thing'
    D = 'Oh look over here'
}

$login = Read-Host -Prompt login

[Windows.Clipboard]::SetText($options[$login])
更进一步,我建议验证输入:

do {
    $login = Read-Host -Prompt login
} until ($options.Keys -contains $login)
。听起来您实际上想要的是一个哈希表或类似的:

# added to reference the System.Windows namespace
Add-Type -AssemblyName PresentationFramework

$options = @{
    A = 'this thing'
    B = 'That thing'
    C = 'Another thing'
    D = 'Oh look over here'
}

$login = Read-Host -Prompt login

[Windows.Clipboard]::SetText($options[$login])
更进一步,我建议验证输入:

do {
    $login = Read-Host -Prompt login
} until ($options.Keys -contains $login)

为什么要使用
Invoke Expression
?将此代码段与带有batfile的服务器连接中的Invoke Expressioin一起使用,但是使用我们的不带Invoke Expression的表达式,我得到的输出与数组的最后一个输出相同,而不是示例a
SetText()
for
循环期间执行4次为什么要使用
调用表达式
?将此代码段与带有batfile的服务器连接时的调用表达式一起使用,但是使用我们的未调用表达式,我得到的输出与它获取数组的最后一次输出相同,而不是示例a
SetText()
for
循环期间执行4次