Write-Output is showing System.Data.DataRow for a variable which get the data from a sql query column value and If statement uses that variable is not executing.

Reddy, T 25 Reputation points
2024-02-22T12:06:26.7066667+00:00
  1. We are Using PowerShell in Azure Runbooks to run the azure data factory pipelines automatically.
  2. I have written a PowerShell code to stop the execution if previous run failed.
  3. below is the code (SQL query result is FAILED in below code) $STATUS_CURRENTRUN = @(Invoke-Sqlcmd -Query "SELECT distinct STATUS_FLAG FROM LOGGING.DM_PROCESS_CONTROL_DETAIL WHERE PROJECT_FLAG='GEMS' ;" @params ) Write-Output "STATUS_CURRENTRUN is $STATUS_CURRENTRUN" if($STATUS_CURRENTRUN -eq 'FAILED') { stop the execution update the log table }
  4. Now the problem is Write - Output "STATUS_CURRENTRUN is $STATUS_CURRENTRUN" command displaying System.data.datarow instead of query result (FAILED).
  5. And also, if statement in above code is never executing.

Please help me to get it fixed.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,336 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,882 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sedat SALMAN 14,155 Reputation points MVP
    2024-02-22T14:45:13.06+00:00

    Invoke-Sqlcmd returns a collection of DataRow objects, even when there's a single result. You're assigning this entire collection to $STATUS_CURRENTRUN, which is why you're seeing System.Data.DataRow. the following approach can work better for you

    # Run the SQL query
    $results = Invoke-Sqlcmd -Query "SELECT distinct STATUS_FLAG FROM LOGGING.DM_PROCESS_CONTROL_DETAIL WHERE PROJECT_FLAG='GEMS' ;" @params
    
    # Extract the value (assuming STATUS_FLAG is the desired column)
    if ($results.Count -gt 0) {  # Ensure there's at least one result
        $STATUS_CURRENTRUN = $results[0].STATUS_FLAG  
    } else { 
        # Handle case where there are no results (e.g., log an error)
        Write-Warning "No results from the SQL query."
    }
    
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.