Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Outputting variables & accepting keystrokes without pressing enter

Reply
 
Old 08-08-2008   #1 (permalink)
Kari


 
 

Outputting variables & accepting keystrokes without pressing enter

Hi!

no1:

Let's say that I have two variables:

$var1 = "variable1"
$var2 = "variable2"

I would need to print out the values of these variables so that the output
would be like:

variable1_variable2

If I try output it:
[PS] E:\>$var1 = "variable1"
[PS] E:\>$var2 = "variable2"
[PS] E:\>write-host "$var1_$var2"
variable2

.... I can't get both of the values out. I also need to use this to specify
an output file in cvs:

[querystuff] | export-csv -path "c:\variables_$var1_$var2"

I realize the difference between double quotes and single quotes. I just
don't understand how to combine two variables.

--------

no2

If I'm running some sort of loops and I need to query user for like
"Press y to continue or n to quit"

.... how can I do this so that user only needs to choose y and the query
starts (without pressing enter)?

My System SpecsSystem Spec
Old 08-08-2008   #2 (permalink)
Kiron


 
 

RE: Outputting variables & accepting keystrokes without pressing enter

# 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
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Why the "RunWorkerCompleted" never comes after pressing ENTER? .NET General
Can't open pics by pressing enter or double click Vista music pictures video
Pop-up to enter variables to pass to script PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46