Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic string parsing in Powershell
    primarykey
    data
    text
    <p>I'm trying to do something unusual with Powershell parsing. Basically, I have a literal string that contains the name of a variable. What I would like to do is tell powershell "Hey, I've got a string that (may) contain one or more variable names--dynamically parse it, replacing the variable names with their values"</p> <p>Here's the textbook way I understand parsing works:</p> <pre><code>PS C:\&gt; $foo = "Rock on" PS C:\&gt; $bar = "$foo" PS C:\&gt; $bar Rock on </code></pre> <p>And if I now change the value for $foo:</p> <pre><code>PS C:\&gt; $foo = "Rock off" PS C:\&gt; $bar Rock on </code></pre> <p>No surprises here. The value of $bar was parsed as it was assigned, and didn't change simply because the value of $foo changed.</p> <p>Ok, so What if we assign $bar with a single quote?</p> <pre><code>PS C:\&gt; $foo = "Rock on" PS C:\&gt; $bar = '$foo' PS C:\&gt; $bar $foo </code></pre> <p>That's great, but is there a way to get Powershell to parse it on demand? For example:</p> <pre><code>PS C:\&gt; $foo = "Rock on" PS C:\&gt; $bar = '$foo' PS C:\&gt; $bar $foo PS C:\&gt; Some-ParseFunction $bar Rock on PS C:\&gt; $foo = "Rock off" PS C:\&gt; Some-ParseFunction $bar Rock off </code></pre> <p>Why do I want to do this? I would like to be able to get content from a file (or some data source) and dynamically parse it:</p> <pre><code>PS C:\&gt; $foo = "Rock on" PS C:\&gt; '$foo with your bad self.' | out-file message.txt PS C:\&gt; $bar = (get-content message.txt) PS C:\&gt; $bar $foo with your bad self. PS C:\&gt; Some-ParseFunction $bar Rock on with your bad self. </code></pre> <p>Can this be done? I realize I could do some pattern matching for search/replace for known names, but I'd rather have Powershell reparse the string.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload