Groups > Microsoft > Windows PowerShell > Re: Help with variable substitution




Help with variable substitution

Help with variable substitution
Sun, 13 Apr 2008 14:18:34 -070
I have a XML-file from which I select a SQL command which contains
variables for database, owner and table.
Then I want to use variable substitution to create "real" commands.

XML-file ("c:\commands.xml"):

<?xml version="1.0" encoding="windows-1250"?>
<commands>
	<command description="select count(*)"
alias="|-c*|">
		<database engine="sqlserver" command="select count(*) from
$
($database).$($owner).$($table)"/>
		<database engine="db2" command="select count(*) from
$database.
$owner.$table"/>
	</command>
	<command description="select *" alias="|-*|">
		<database engine="sqlserver" command="select * from
$database.$owner.
$table"/>
	</command>
</commands>


Script:
	$database = "abc"
	$owner = "dbo"
	$table = "xyz"
	$a = [xml] (gc "c:\commands.xml")
	$command=$($a.selectnodes("//database[@engine='sqlserver' and
contains(../@alias,'|-*|')]")).command

I would like output like
select * from abc.dbo.xyz

Post Reply
Re: Help with variable substitution
Sun, 13 Apr 2008 17:48:46 -070
Just add a line at the bottom ...
right now, your $command is set to:
  "select * from $database.$owner.$table"

You want to do this (note the double double-quotes -- that's one way
of escaping them, and the triple double-quotes at the end):

$command = invoke-expression "Write-Output
""$command"""

--
Joel "Jaykul" Bennett

On Apr 13, 5:18 pm, gurbao <aud...@gmail.com> wrote:
> Script:
>         $database = "abc"
>         $owner = "dbo"
>         $table = "xyz"
>         $a = [xml] (gc "c:\commands.xml")
>         $command=$($a.selectnodes("//database[@engine='sqlserver' and
contains(../@alias,'|-*|')]")).command
>
> I would like output like
> select * from abc.dbo.xyz
>
> but I haven't figured out how to do this. What am I missing?
Post Reply
Re: Help with variable substitution
Mon, 14 Apr 2008 02:03:53 -070
Thanks,
I tried different approaches but invoke-expression was not one of
them :-(
I guess I just use powershell too seldom to remember all the
possibilities I have to choose from  :-)




On Apr 14, 2:48 am, "Joel (Jaykul) Bennett"
<Jay...@huddledmasses.org>
wrote:
> Just add a line at the bottom ...
> right now, your $command is set to:
>   "select * from $database.$owner.$table"
>
> You want to do this (note the double double-quotes -- that's one way
> of escaping them, and the triple double-quotes at the end):
>
> $command = invoke-expression "Write-Output
""$command"""
>
> --
> Joel "Jaykul" Bennett
>
> On Apr 13, 5:18 pm, gurbao <aud...@gmail.com> wrote:
>
> > Script:
> >         $database = "abc"
> >         $owner = "dbo"
> >         $table = "xyz"
> >         $a = [xml] (gc "c:\commands.xml")
> >         $command=$($a.selectnodes("//database[@engine='sqlserver'
and contains(../@alias,'|-*|')]")).command
>
> > I would like output like
> > select * from abc.dbo.xyz
>
> > but I haven't figured out how to do this. What am I missing?
Post Reply
about | contact