Executing command from variable, not parsing command results to output

by Jul 18, 2012

 Let me try to post two version of the script. Script1 works, Script2 dont work:

Script1:
$usr_descr = Read-Host -prompt "Description" # At this line, the script asking me to provide "Description", i provide for eg:1
$usr_descr # This shows that $usr_descr is: 1,

# if $usr_descr is 1 then replace $usr_descr variable with "DE;C;;Deutsche;;User Account;" 
switch ($usr_descr)
{
"1" { $usr_descr = "DE;C;;Deutsche;;User Account;" }
"0" { $usr_descr = "DE;C;;Deutsche;;User Account;extern" }
default
{
$usr_descr = ""
}
}
# $usr_descr is "DE;C;;Deutsche;;User Account;", script works !

Script2:
$usr_descr_command = {$usr_descr = Read-Host -prompt "Description"}
& $usr_descr_command # At this line, the script asking me to provide "Description", i provide for eg:1
$usr_descr # This shows that $usr_descr is EMPTY !! Why ?

# if $usr_descr is 1 then replace $usr_descr variable with "DE;C;;Deutsche;;User Account;" 
switch ($usr_descr)
{
"1" { $usr_descr = "DE;C;;Deutsche;;User Account;" }
"0" { $usr_descr = "DE;C;;Deutsche;;User Account;extern" }
default
{
$usr_descr = ""
}
}
# $usr_descr is still empty ! 

Why executing command from variable, in case where a variable contain "$something = Read-Host" , $something being empty after i provide it ?