# 1.-
$var1 = "variable1"
$var2 = "variable2"
# the first variable's name is altered and PowerShell tries to expand
# the variable $var1_, if it does not exist $null is expanded...
"c:\variables_$var1_$var2"
# ...if it exists, $var1_ is expanded
$var1_ = 'oops!'
"c:\variables_$var1_$var2"
# to avoid this enclose the variable's name in '{}'
"c:\variables_${var1}_$var2"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# 2.-
You can use the [Management.Automation.Host.KeyInfo]'s VirtualKeyCode
Property to check which key was pressed. It also has a Character Property.
Label the outer loop, then prompt the user and either continue or exit the
outer loop.
:myLoop foreach ($num in 1..10) {
"`$num = $num"
do {
write-host Press y to continue or n to quit
$action = $host.ui.rawUI.readkey('NoEcho, IncludeKeyDown')
switch -r ($action.virtualKeyCode) {
89 { # <-- 'y' or 'Y'
"`$num * 2 = $($num * 2)`n$('-' * 25)"
break
}
78 { # <-- 'n' or 'N'
write-host Exiting myLoop
sleep -m 200
break myLoop # <-- break out of the Foreach loop, labeled 'myLoop'
}
}
# continue prompting if other keys were pressed
} while ($action.virtualKeyCode -notMatch '89|78')
}
--
Kiron