# Sql Query script

**URL:** <https://community.squaredup.com/t/sql-query-script/907>\
**Category:** PowerShell\
**Tags:** sql, 2-state-monitor-aler, powershell-mp, sql-query, powershell\
**Created:** [January 30, 2019, 10:23am UTC](https://community.squaredup.com/t/sql-query-script/907 "2019-01-30T10:23:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![erenay-t](https://community.squaredup.com/letter_avatar_proxy/v4/letter/e/a3d4f5/32.png) [@erenay-t](https://community.squaredup.com/u/erenay-t)\
**Post date:** [January 30, 2019, 10:23am UTC](https://community.squaredup.com/t/sql-query-script/907/1 "2019-01-30T10:23:02Z")

</div>

Hey Guys,

I made a script for Scom monitoring with powershell.

```
$ScomAPI = New-Object -comObject "MOM.ScriptAPI"
                    $PropertyBag = $ScomAPI.CreatePropertyBag()

  
    
        $sqlConn = New-Object System.Data.SqlClient.SqlConnection
            $sqlConn.ConnectionString = “Server= *****\******* ;Integrated Security=true;Initial Catalog=n ***** ”
                $sqlConn.Open()

        $sqlcmd = New-Object System.Data.SqlClient.SqlCommand
            $sqlcmd.Connection = $sqlConn
                    $query = "
                               SELECT count(*)
                                 FROM HL7_Outbox
                             "  
                             
        $sqlcmd.CommandText = $query

        $adp = New-Object System.Data.SqlClient.SqlDataAdapter $sqlcmd
           
              $SqlQeuryOutput = $sqlcmd.ExecuteScalar()
  
        
             
                                
$Result = "bad"
       
      if ($SqlQeuryOutput -lt "1" ) {

        $Result = "good"
        
          
    } 

    
   Write-EventLog -LogName 'Operations Manager' -Source "MyScript" -EventId 1 -EntryType Warning -Message "This is a test 2.0, the state of this monitor is $Result, $SqlQeuryOutput, "
    
    

      $sqlConn.Close()

# fill pb
$PropertyBag.AddValue("Result",$Result)
             
# Send output to SCOM
$PropertyBag
```

The entire script works perfect in powershell.

after i made it the script work on powershell, i made a two-state monitor from it. but once the monitor was put in scom, the monitor is giving me the exact opposite awnser i am getting from powershell, so powershell and the two-state monitor have the exact same script, but different answers. how is this possible?
