ExpandString function...
i have data stored in external xml file... script automatically read , assign specific variables.
after while, figured out cannot use variables (%system% or $posh) in external file (which makes complete sense), decided whenever retrieve data, use 2 functions expand sting:
$executioncontext
.invokecommand.expandstring() expand posh $variables
and
[system.environment]::expandenvironmentvariables() expand system %variables%
it works fine, run problem today while handling sql queries. expandstring() automatically rid of quotes (single or double-quote), sql queries corrupted (where x='y').
i realized can use ''' (where x='''y'''), y cannot variable of course :(
i solved using $([char]39) instead of ', curious how handle such situations?
martin
gc file.xml |%{[regex]::replace($_,"%(\w+)%",'$env:$1')}
Windows Server > Windows PowerShell
Comments
Post a Comment