1

I am trying to parse the response from one of the functions in PowerShell Script and based on the response I have to make some decision.

The function is returning JSON object successfully but not able to parse the response from the function. I need to check if validnodes count is 0 or not.

      [String[]] $NodeList = 'a1572dev00e001','a1572dev00q001'


$Response = Get-Nodes
Write-Output "Response $Response"
        $JSRes = $Response | ConvertFrom-Json
        Write-Output "Parsing response $JSRes"
        #$Result = "success"

        if($JSRes.ValidNodes.Count -gt 0)
            {
                 Write-Output "$JSRes.ValidNodes"
                 $Result = "success"
                 

            }
            else
                {
                    Write-Output "All nodes are Invalid"
                    Write-Output "Invalid Nodes: $JSRes.Invalid"
                    $Result = "failed"
                    $ErrorMessage = "All nodes are Invalid"
    
            }
        Write-Output $Result


#Function
function Get-Nodes
{
    $ValidNodes=@()
    $InvalidNodes=@()

foreach($Node in $NodeList)
{
    if(Get-ADComputer -filter {Name -eq $Node})
    {
        $ValidNodes +=$Node
    }
    else
    {
        $InvalidNodes +=$Node
    }
}
    $JRes = @{"ValidNodes"=$ValidNodes;"Invalid"=$InvalidNodes} | ConvertTo-Json -Compress
    Write-Output $JRes 
    return $JRes
}

Output:

Response {"ValidNodes":["a1572dev00e001","a1572dev00q001"],"Invalid":[]} {"ValidNodes":["
a1572dev00e001","a1572dev00q001"],"Invalid":[]}
Parsing response 
All nodes are Invalid
Invalid Nodes: 
failed
1
  • 1
    Change $JSON = $Response | ConvertFrom-String -> $JSON = $Response | ConvertFrom-Json. I would suggest renaming $JSON to something else - it's no longer json :) Commented Sep 24, 2020 at 21:36

1 Answer 1

3

One issue is you are outputting the $Jres twice.

Write-Output $JRes 
return $JRes

These effectively do the exact same thing. Next, you're using ConvertFrom-String when it seems you should be using ConvertFrom-Json

$JSON = $Response | ConvertFrom-String

Finally, you're trying to output $ValidNodes and $InvalidNodes that only exist in your function. Change these to $JSON.ValidNodes and $JSON.InvalidNodes

One more suggestion is to parameterize the Nodelist so you can just pass the nodes to the function.

#Function
function Get-Nodes
{
    Param([string[]]$nodelist)
    $ValidNodes=@()
    $InvalidNodes=@()

foreach($Node in $NodeList)
{
    if(Get-ADComputer -filter {Name -eq $Node})
    {
        $ValidNodes +=$Node
    }
    else
    {
        $InvalidNodes +=$Node
    }
}
    $JRes = @{"ValidNodes"=$ValidNodes;"Invalid"=$InvalidNodes} | ConvertTo-Json -Compress
    $JRes 
}

$Response = Get-Nodes a1572dev00e001,a1572dev00q001

Write-Output "Response $Response"
$JSON = $Response | ConvertFrom-Json
Write-Output "Parsing response $JSON"

if($JSON.ValidNodes.Count -gt 0)
    {
            Write-Output "Valid Nodes: $($JSON.ValidNodes)"
            $Result = "success"
             
    }
    else
    {
            Write-Output "All nodes are Invalid"
            Write-Output "Invalid Nodes: $($JSON.InValidNodes)"
            $Result = "failed"
            $ErrorMessage = "All nodes are Invalid"

    }
Write-Output $Result
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.