-2

I would like a try...catch block around the cmdlet to gather information for debugging purposes that grabs ANY kind of results from the call, but all the research i've done on the cmdlet have failed to find ANY kind of exceptions being thrown.

Need some assistance with powershell.

the GET-ClusterNode cmdlet is failing 'sometimes'

I would like a try...catch block around the cmdlet to gather information for debugging purposes that grabs ANY kind of results from the call, but all the research i've done on the cmdlet have failed to find ANY kind of exceptions being thrown.

I've tried several variations of try...catch but i am failing at the task due to me not being able to see if there is an error being thrown.

The script loads an array with the results of the cmdlet into an array, then check for a zero value of records, i can fire the cmdlet again, but that isn't an answer - just a hack work around that might work or not.

As i mentioned, the cmdlet is failing RANDOMLY, and on several different systems, using the exact same script.

The script execution depends on the results of this command to continue processing.

Suggestions would be greatly appreciated.

    Write-Output "$(Get-Date): Display ""Get-ClusterNode"" output before trying to capture it to an array object (for debugging purposes)"

Get-ClusterNode
if (!$?) {Write-Output "$(Get-Date): The ""Get-ClusterNode"" command failed"}

# Wait a few seconds before this script attempts to capture Get-ClusterNode into an object

Start-Sleep -Seconds 3

Write-Output ""
Write-Output "$(Get-Date): Capture ""Get-ClusterNode"" output to an array object (`$ClusterNodeCollection_OBJ) for further processing"
Write-Output ""

$PotentialFailureReason_DSC = "Error while executing ""Get-ClusterNode"""

[array]$ClusterNodeCollection_OBJ = Get-ClusterNode
if (!$?) {$ErrorMessage_STR = $error[0].ToString(); $ErrorPosition_STR = $error[0].InvocationInfo.PositionMessage.ToString(); job_failure $ErrorMessage_STR $ErrorPosition_STR}

Write-Output "`$ClusterNodeCollection_OBJ ="
Write-Output ""

$ClusterNodeCollection_OBJ

$ClusterNode_CNT = $ClusterNodeCollection_OBJ.Count

Write-Output ""
Write-Output "`$ClusterNode_CNT = $ClusterNode_CNT"
mklement0
  • 382,024
  • 64
  • 607
  • 775
pithhelmet
  • 2,222
  • 6
  • 35
  • 60
  • 2
    Please post the relevant snippet of script your having trouble with. You can also try adding: `-ErrorAction Stop` to the cmdlet within the Try catch, to see if that then catches it. However there are rare occasions when commands will fail but not error, most likely due to a flaw in how the cmdlet is coded. – KG-DROID Aug 16 '23 at 20:36
  • As for the specific question: While the question mentions `try` / `catch`, it isn't used in the code, and there seems to be no awareness that _extra effort_ is usually required to make `try` / `catch` statements effective, which the [previously linked to](https://stackoverflow.com/q/59627391/45375) post addresses. _Any_ error in the `try` block is then caught. Even if you had shown such code, mysterious quiet failures without a [mcve] make your question eligible for closure to "Needs details or clarity" / "Needs debugging details" (and the former was indeed one of the other close votes). – mklement0 Aug 17 '23 at 12:49

0 Answers0