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
$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>
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
"Shay Levy [MVP]" <n...@addre.ss> wrote in message
news:37162f6db12838...@news.microsoft.com...
Martin
"Martin Zugec" <martin...@gmail.com> wrote in message
news:C88647C3-4EEF-492D...@microsoft.com...