Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Joining together strings into Powershell variable names

$
0
0

I'm working with treeviews in Powershell - I have a different node for each of our buildings. In my code I'm grabbing variables, Joining them together, and using that as the variable name - but my code is seeing the variable as a string instead of the name of a node that already exists... so I'm getting

You cannot call a method on a null-valued expression.

How can I do this? It would save me from hard-coding in every floor in every building. Here's what my code looks like:

$bldg = "SG1-1" //for example
function refreshPrinterList ( $bldg )
{
$bldg1 = $bldg.substring(0,3)
$floor = $bldg.substring(4,1)
$refreshNode = -join('$TreeNode_',$bldg1,'_',$floor)
$refreshNode.gettype() //Returns System.String`
if($bldg1 -eq "SG1") {
    if($floor -eq "1") {
        $count = $refreshNode.Nodes.Count
        while($refreshNode.Nodes.Count -gt 0)
        {
            $refreshNode.Nodes.RemoveAt($count)
            $count--      
        }

I can create a new node with that string name - but the node already exists elsewhere in the code, I just want to reference it. 


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>