Formatting variables

This topic contains 2 replies, has 2 voices, and was last updated by  Justin King 2 years, 10 months ago.

  • Author
  • #19363

    Justin King

    The title may be poor, so I'll try to explain.

    I've writing a DSC Resource and I'd like to do something seemingly simple: a if($var1 -eq $var2) sort of thing. What I'm running into, however is what I"m assuming is a formatting problem between the two variables.

    Quick example is I grab a on of those fun values that has child objects. Grabbing it is rather easy and the output looks like this:

    $var1 = $coll.Rules
    QueryExpression : samplehere
    QueryID         : 1
    RuleName        : x64 PCs

    So when I grab $rules i get the above. So I tried to make a hashtable that mimics that dump .... made that like this:

    $var2 = @(
            QueryExpression = "samplehere"
            QueryID = "1"
            RuleName = "x64 PCs"
    Name                           Value
    RuleName                    x64 PCs
    QueryExpression        samplehere
    QueryID                        1

    now if i do a simple if ($var1.RuleName -eq $var2.RuleName) everything works as expected. But if i try to match the entire value of (var1 -eq var2) I fail. I'm guessing the two expressions are not formatted exactly the same, but I have no idea (sorry for being so new) how to format my "hand crafted variable" to match the value I'm reading off the system.

    Anyone have anyplace they can point me on how to handle this?

    (context: trying to create a DSC resource to manage something where one of the variables can have 'x" rules defined in it. I want to be able to read the entirety of the variable and match against my DSC configuration, so I'm debating using a hashtable to store all variables then compare, but no matter what I can't match the format so my test will always be $false)

  • #19365


    I think (and notice, think, I'm not all that good at this stuff... :P) that your problem is that you're trying to compare different types of variable.

    variables always have a type in powershell. So even if you construct your own hashtable variable, it will not be the same as the object type of the original one, even if the properties and values are the same. You can probably check your original variable type by doing something like


    You can still compare values of single properties (as you noticed), but you cannot compare the variable holding the object returned from whatever you're doing, to the variable containing your hashtable.

    I don't have time to test it right now, but it's possible you can do some magic with select in order to turn your original object to a pscustomobject. I'm pretty sure select does that, again you could confirm with $var1a = $var1 | select *; $var1a.gettype().fullname

    But in your scenario, I'd probably just compare the values to your absolute.

    Also, if I'm wrong, I apologize and hope someone comes up with the right answer

  • #19368

    Justin King

    Actually I think you showed me exactly what I wanted. By proving they were different formats I simply needed to create a foreach loop and build a new hash table to match . Little bit round about but by building a matchign array i finally got things where i want them.


You must be logged in to reply to this topic.