Can anyone tell me why this else statement is not working?…

Welcome Forums General PowerShell Q&A Can anyone tell me why this else statement is not working?…

Viewing 3 reply threads
  • Author
    Posts
    • #265949
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member

      Hi,

      I have the following code to retrieve tags from various vcenter servers, I am banging my head against the wall as I just cannot see where the structure of my code is going wrong where the else statement is not recognised,

      Many thanks all!

    • #265976
      Participant
      Topics: 2
      Replies: 382
      Points: 523
      Helping Hand
      Rank: Major Contributor

      I can’t test it but as an educated guess, Get-TagAssignment does not return NULL if there are no tag assignments.

      Try piping $Tags to Get-Member to figure out what’s assigned to $Tags when there are no tag assignments returned.

    • #266000
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member

      Yeah I must admit, if I pulled the correct objects from Get-TagAssignment then I should ‘hopefully’ not receive any empty values

      The reason I used the ‘else’ statement in my code was to evade my export-csv failing due to these

      I have become a bit fixated on why the else statement will not work as the structure of the code seems fine to me and it bugs me that I cant get it to work as I am sure that I will need to use ‘else’ again some time in the future, if not in this code then something else

      Anyway, many thanks for your reply Matt

    • #266183
      Participant
      Topics: 9
      Replies: 677
      Points: 2,680
      Helping Hand
      Rank: Community Hero

      The issue is you have the else portion in the wrong spot. It currently reads

      Code formatting and indentation can go a long way. Also, you open the same log file to export over and over appending each time as well as create a (what should be) identical list. Instead of doing those it would be better to just capture all output and then export once. I’ve restructured your code to show the recommendations.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.