Use try catch to find values in a variable which returned no value?

Welcome Forums General PowerShell Q&A Use try catch to find values in a variable which returned no value?

Viewing 5 reply threads
  • Author
    Posts
    • #184440
      Participant
      Topics: 20
      Replies: 53
      Points: 139
      Rank: Participant

      Greetings experts have the following script which is listing the output from the IF test only when there is output from that satisfies the IF test.  If the test is not satisfied the $SCOMGroup displayname does not display.  I'm looking for the values in the $SCOMGroup that are not displaying.  Wondering if a try catch would be helpful here, not sure to insert a try catch into this script?  Is there a way to get the try catch to write out to a file?

      Next question is getting the output on the last line into a .csv file.  I've been banging my head against the wall trying to get output to a spreadsheet but to no avail.

       

      $SCOMGroups = (Get-SCOMGroup).Displayname
      foreach ($SCOMGroup in $SCOMGroups) {
      If (Get-scomgroup -Displayname $SCOMGroup| Get-SCOMClassInstance | sort Displayname | FT DisplayName) {
      Write-Host “=================================================================”
      Write-Host $SCOMGroup
      Write-Host “=================================================================”
      Get-scomgroup -Displayname $SCOMGroup | Get-SCOMClassInstance | sort DisplayName | FT DisplayName
      }
      }
      

      Thank you for any and all input
      Norm
       

    • #184443
      Participant
      Topics: 20
      Replies: 53
      Points: 139
      Rank: Participant

      Greetings experts:
      The first sentence should read as follows:
      Greetings experts have the following script which is listing the output from the IF test only when there is output which is satisfied by the IF test.

      Sorry for the confustion

      Norm

    • #184455
      Participant
      Topics: 4
      Replies: 84
      Points: 368
      Helping Hand
      Rank: Contributor

      Regarding Try, Catch. It only catches terminating errors, if the error isn't terminating we need to make it so with "-ErrorAction Stop"

      Export-CSV exports objects to csv. since strings only have a single property (length) you have to make a custom object to get it to export correctly.

      The below code should get you going in the right direction.

      
      $SCOMGroups = (Get-SCOMGroup).Displayname
      foreach ($item in $SCOMGroups)
      {
      try
      {
      $SCOMGroupObj = Get-SCOMGroup -Displayname $item -ErrorAction Stop
      $SCOMCLASS = Get-SCOMClass -Instance $SCOMGroupObj | Sort-Object -Property DisplayName
      $SCOMCLASS | Format-Table -AutoSize
      }
      catch
      {
      Export-Csv -InputObject $([pscustomobject]@{'SCOM_Group' = $item}) -Path C:\temp\scom_groups.csv -Append -NoTypeInformation
      }
      }
      
      

       

       

       

    • #184464
      Participant
      Topics: 20
      Replies: 53
      Points: 139
      Rank: Participant

      Hello Logan,
      Want to first thank you for your input, it was helpful. Sorry to say still not getting the empty SCOM groups. I'm new to SCOM administration so it is a bit hard for me to explain, the problem as I see it is in the following line:

      If (Get-scomgroup -Displayname $SCOMGroup| Get-SCOMClassInstance | sort Displayname | FT DisplayName). 

      The key is the pipe to Get-SCOMClassInstance if there are group members assigned to the group then the line list the Group and the group members (vm's) assigned to the group. If there are no group members piping the output from Get-scomgroup to get-scomclassinstance produces nothing we might say a null value.

      Your script did produce output however only a half a dozen or so groups a few were empty most were not. And as mentioned some groups that I know for a fact of have no group members did not make it to the output file.

      Any thoughts?

      Norm

    • #184509
      Participant
      Topics: 4
      Replies: 84
      Points: 368
      Helping Hand
      Rank: Contributor

      I apologize if I wasn't quite clear on the end goal. If you want to return the name of groups with no members use the "-ErrorAction Stop" on the Get-SCOMClassInstance.

      Also, I would recommend dropping the format-table on the end. Formatting is for outputting to the console, if you are working with object in a ForEach, or a pipeline formatting them changes the type of object they are. I have updated the code I posted previously, this version is untested as I do not currently have a SCOM environment to test with, but I believe it should work as intended.

      
      $SCOMGroups = Get-SCOMGroup
      foreach ($item in $SCOMGroups)
      {
      try
      {
      $SCOMCLASS = Get-SCOMClass -Instance $item -ErrorAction Stop | Sort-Object -Property DisplayName
      $SCOMCLASS | Format-Table -AutoSize
      }
      catch
      {
      Export-Csv -InputObject $([pscustomobject]@{'SCOM_Group' = $item.DisplayName}) -Path C:\temp\scom_groups.csv -Append -NoTypeInformation
      }
      }
      
      
    • #184649
      Participant
      Topics: 20
      Replies: 53
      Points: 139
      Rank: Participant

      Hello Logan,
      I'm sure it's me still not getting the script to work. Microsoft support found this link: http://tetris38.blogspot.com/2012/06/opsmgr-2007r2-get-member-count-of-all.html we modified the script as follows:

      $mg = Get-SCOMManagementGroup 
      $groups = $mg.GetRootPartialMonitoringObjectGroups() | sort DisplayName 
      foreach($group in $groups) { 
             $groupMembers = $group.GetRelatedPartialMonitoringObjects([Microsoft.EnterpriseManagement.Common.TraversalDepth]::OneLevel)
             $groupMembersCount = $groupMembers.count.ToString()
             $Msg = $group.DisplayName + "|" + $groupMembersCount
             #$Msg
             $Msg >> C:\temp\SCOMGroupsMembersCountInProduction.txt
       }

      This script provides a count of the number of group members in each group. Going forward will this script in conjunction with the script in my post to clean up non-standard (default) groups in our SCOM installation.

      Thanks again for your help

      Norm

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