locked
Joining together strings into Powershell variable names RRS feed

  • Question

  • 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. 

    Wednesday, June 25, 2014 9:01 PM

Answers

  • Hi Nick,

    you can access a variable value by using the Get-Variable command. For example ...

    (Get-Variable "TreeNode_SG1_1").Value

    Will get you the value of the variable $TreeNode_SG1_1. Which then ought to have a Property named "Nodes" and so on.

    What is happening with your function, is that you declare $refreshNode as a string. Strings don't have a property "Nodes" however. Powershell will forgive you for calling a nonexistant property (It'll simply be $null), but try to call a non-existant method will yield you an error. And since $null does not have a method called RemoveAt that's the error you get: "You cannot call a Method on a null-valued expression".

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by BigfootNick Wednesday, June 25, 2014 10:50 PM
    Wednesday, June 25, 2014 9:55 PM
  • Hey Fred, 

    Thanks for the reply! I tried your suggestion and here's what I found:

    I had the two variables $bldg1 and $floor which I joined into a string:

    $newNodeName = -join('TreeNode_',$bldg1,'_',$floor)

    and then I passed that using 'Get-Variable' as you suggested - but I had to put the variable name within brackets, like so:

    $refreshNode = (Get-Variable ($newNodeName)).Value

    Now, instead of returning a string type it returns my existing string! Thanks so much for your help!

    • Marked as answer by BigfootNick Wednesday, June 25, 2014 10:50 PM
    Wednesday, June 25, 2014 10:45 PM

All replies

  • Hi Nick,

    you can access a variable value by using the Get-Variable command. For example ...

    (Get-Variable "TreeNode_SG1_1").Value

    Will get you the value of the variable $TreeNode_SG1_1. Which then ought to have a Property named "Nodes" and so on.

    What is happening with your function, is that you declare $refreshNode as a string. Strings don't have a property "Nodes" however. Powershell will forgive you for calling a nonexistant property (It'll simply be $null), but try to call a non-existant method will yield you an error. And since $null does not have a method called RemoveAt that's the error you get: "You cannot call a Method on a null-valued expression".

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by BigfootNick Wednesday, June 25, 2014 10:50 PM
    Wednesday, June 25, 2014 9:55 PM
  • Hey Fred, 

    Thanks for the reply! I tried your suggestion and here's what I found:

    I had the two variables $bldg1 and $floor which I joined into a string:

    $newNodeName = -join('TreeNode_',$bldg1,'_',$floor)

    and then I passed that using 'Get-Variable' as you suggested - but I had to put the variable name within brackets, like so:

    $refreshNode = (Get-Variable ($newNodeName)).Value

    Now, instead of returning a string type it returns my existing string! Thanks so much for your help!

    • Marked as answer by BigfootNick Wednesday, June 25, 2014 10:50 PM
    Wednesday, June 25, 2014 10:45 PM