I have a PowerShell script monitor that needs to read its component status to modify its action. It reads conditions from a SQL to a database and acts on the query's returns to restart a service on a different server. The issue is that I need to only restart the service if the monitor is in the UP state (status = 1). I have made this function:
# Function to get the component status to use for restart behavior modification
- function GetComponentStatus (){
- # initialize some variables
- [int]$componentStatus = 0
- [int]$componentID =123456
- # build the connection string
- $conPort = "[port_number]"
- $conDatabase = "SolarWindsDatabase"
- $conServer = "[ServerName]\[InstanceName]"
- $conString = "Server={0},{1};Database={2};Integrated Security=True" -f `
- ($conServer, $conPort, $conDatabase)
- # create a new connection
- $OrionConnection = New-Object system.Data.SqlClient.SqlConnection($conString)
- $OrionConnection.Open()
- # query returns the component status, 1,2,3,etc.
- $query2 = "select top 1 availability as 'Status' from APM_ComponentStatus_Detail
- where ComponentID = $componentID;"
- $cmd2 = $OrionConnection.CreateCommand()
- $cmd2.CommandText = $query2
- $reader2 = $cmd2.ExecuteReader()
- while ($reader2.Read())
- {
- $dataPoint2 = New-Object PSObject
- $dataPoint2 | Add-Member -MemberType NoteProperty -Name Status -Value $reader2.GetValue(0)
- $componentStatus = $dataPoint2.Status
- }
- return $componentStatus
- }
This works but I have to hard-code the value of $componentID (line 4). How do I pull the component ID out of the environment similar to pulling the IP Address using ${IP}, which works from the command line? Is there a SWIS formatted command line macro I can use? I have tried several ${???} macros but none were filled with the componentID.
Thanks
Michael