Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Expanding string

38 views
Skip to first unread message

Martin Zugec

unread,
Nov 25, 2009, 3:58:18 PM11/25/09
to

Hi gents,

my scripts are loading configuration settings from external XML files. My
problem is that I would like to support also variables in it, however it's
automatically saved as string.

Therefore $PWD is NOT folder, but '$PWD'.

As a workaround, I am using [string]$Var = Invoke-Expression, however it's
very easy to inject some code in it, therefore I would like to have just
something to expand string.

For regular environment variables (%var%), I can of course use
[System.Environment]::ExpandEnvironmentVariables, however I am curious if
someone knows about safe method how to expand strings in Posh??

Thanks in advance for any ideas.

Martin

Shay Levy [MVP]

unread,
Nov 25, 2009, 4:49:48 PM11/25/09
to
Try this:

$ExecutionContext.InvokeCommand.ExpandString($foo)


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar

MZ> Hi gents,
MZ>
MZ> my scripts are loading configuration settings from external XML
MZ> files. My problem is that I would like to support also variables in
MZ> it, however it's automatically saved as string.
MZ>
MZ> Therefore $PWD is NOT folder, but '$PWD'.
MZ>
MZ> As a workaround, I am using [string]$Var = Invoke-Expression,
MZ> however it's very easy to inject some code in it, therefore I would
MZ> like to have just something to expand string.
MZ>
MZ> For regular environment variables (%var%), I can of course use
MZ> [System.Environment]::ExpandEnvironmentVariables, however I am
MZ> curious if someone knows about safe method how to expand strings in
MZ> Posh??
MZ>
MZ> Thanks in advance for any ideas.
MZ>
MZ> Martin
MZ>


stej

unread,
Nov 26, 2009, 2:54:31 AM11/26/09
to
I needed some more - to specify names of properties of given object.
It should support nesting by '.' as well (it's a posh module for data
visualization http://www.leporelo.eu/blog.aspx?id=powershell-module-for-html-visualization
)

So consider this:
$x = [xml]@"
<root>
<a>
<b>
<c>42</c>
</b>
</a>
<a>
<b>
<c>really?</c>
</b>
</a>
</root>
"@

$property = "root.a[0].b.c"
$ExecutionContext.InvokeCommand.expandstring('$($x.'+$property +')')
#returns 42

I found it very interesting & powerfull.

On 25 lis, 22:49, Shay Levy [MVP] <n...@addre.ss> wrote:
> Try this:
>
> $ExecutionContext.InvokeCommand.ExpandString($foo)
>
> ---
> Shay Levy

> Windows PowerShell MVPhttp://blogs.microsoft.co.il/blogs/ScriptFanatic

Martin Zugec

unread,
Nov 26, 2009, 3:31:33 AM11/26/09
to
Perfect! That's exactly what I needed

Martin

"Shay Levy [MVP]" <n...@addre.ss> wrote in message
news:37162f6db12838...@news.microsoft.com...

Martin Zugec

unread,
Nov 26, 2009, 4:11:06 AM11/26/09
to
Posted on out-web.net - I combined both methods, so now my configuration
files supports not only regular powershell variables, but also %var% syntax
;)

Martin

"Martin Zugec" <martin...@gmail.com> wrote in message
news:C88647C3-4EEF-492D...@microsoft.com...

0 new messages