REmoving keys with empty value deletes key with $TRUE as value

Welcome Forums General PowerShell Q&A REmoving keys with empty value deletes key with $TRUE as value

This topic contains 6 replies, has 4 voices, and was last updated by

js
 
Participant
2 weeks, 2 days ago.

  • Author
    Posts
  • #180908

    Participant
    Topics: 2
    Replies: 4
    Points: 14
    Rank: Member

    I have a piece of code where I create a hash with all parameters which run with a command. BEfore doing so I check for empty parameters and delete those. This also deletes where parameter has a True value while it leaves False. I am unable to understand why so

    Code

    $Volume_Template_Params1 = @{
    ............
    EnableDeduplication = $deduplication;
    EnableEncryption = $encryption;
    EnableIOPSLimit = $enable_IOLimit;
    EnableDataTransferLimit = $Senable_DataLimit;
    EnableCompression = $Compress;

    ..........
    }

    $Volume_Template_Params = $Volume_Template_Params1.Clone()
    foreach ($item in $Volume_Template_Params1.GetEnumerator()) {
    if ([string]::IsNullOrWhiteSpace($item.Value) -or ($item.Value -ieq "null"))
    {
    $SCID_Volume_Template_Params.Remove($item.Key)
    }
    }

    When I loop through this hashtable all parameters which has value remains but with true and empty value are deleted

  • #180917

    Participant
    Topics: 1
    Replies: 96
    Points: 514
    Helping Hand
    Rank: Major Contributor
    You need to compare with $null object in your if statement not with "null" string object.
    if ([string]::IsNullOrWhiteSpace($item.Value) -or ($item.Value -ieq $null))
    Thank you.
  • #180920

    Participant
    Topics: 2
    Replies: 4
    Points: 14
    Rank: Member

    It works, thank you, but why "null" would remove True. Suppose if it is a keyword how should it be handled

  • #180926

    Participant
    Topics: 1
    Replies: 96
    Points: 514
    Helping Hand
    Rank: Major Contributor

    Because null as a string is different from $null as a null. In fact, you don't need the 2nd condition...

    if ([string]::IsNullOrWhiteSpace($item.Value)) would be fine.

    Thank you.

  • #180938

    Participant
    Topics: 2
    Replies: 497
    Points: 1,230
    Helping Hand
    Rank: Community Hero

    If you want it to remove $false values as well, that condition won't work.

    In fact, I'm pretty sure the original if statement is making some assumptions that won't pan out.

    # original: if ([string]::IsNullOrWhiteSpace($item.Value) -or ($item.Value -ieq "null"))
    
    # stated condition from OP: 
    # - remove parameters with $null value
    # - remove parameters with $false value
    
    # I'd recommend:
    if (-not $item.Value -or [string]::IsNullOrWhitespace($item.Value)) { }
  • #181031

    Participant
    Topics: 2
    Replies: 4
    Points: 14
    Rank: Member

    For now I have opted if ([string]::IsNullOrWhiteSpace($item.Value)) but still not clear what null has to do with "true"

    • #181052
      js

      Participant
      Topics: 25
      Replies: 692
      Points: 1,721
      Helping Hand
      Rank: Community Hero

      Any string evaluates to true when casted to boolean.

      $true -eq 'hi there'
      True
      

You must be logged in to reply to this topic.