{"id":1112,"date":"2009-10-11T09:49:59","date_gmt":"2009-10-11T07:49:59","guid":{"rendered":"http:\/\/contoso.se\/blog\/?p=1112"},"modified":"2009-10-11T09:49:59","modified_gmt":"2009-10-11T07:49:59","slug":"query-a-database-from-a-task","status":"publish","type":"post","link":"https:\/\/contoso.se\/blog\/?p=1112","title":{"rendered":"Query a database from a task"},"content":{"rendered":"<p>I have seen a number of questions where Operations Manager operators would like to have more info about\u00c2\u00a0windows machines in the console. You could include info in the console with a new management pack, but if you already have it in another database, it would be easier to query that database direct. I have written a script that will take a machine name (prinicipal name) and query a database for more information it.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2009\/10\/querydb.JPG\"><img decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-1114  aligncenter\" title=\"querydb\" src=\"http:\/\/contoso.se\/blog\/wp-content\/uploads\/2009\/10\/querydb-300x298.jpg\" alt=\"querydb\" width=\"300\" height=\"298\" srcset=\"https:\/\/contoso.se\/blog\/wp-content\/uploads\/2009\/10\/querydb-300x298.jpg 300w, https:\/\/contoso.se\/blog\/wp-content\/uploads\/2009\/10\/querydb-150x150.jpg 150w, https:\/\/contoso.se\/blog\/wp-content\/uploads\/2009\/10\/querydb.JPG 541w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u00c2\u00a0<br \/>\nThe script<\/p>\n<blockquote><p>&#8216;## Get parameter (computer name) into the script<br \/>\nset oArgs=wscript.Arguments<br \/>\n&#8216;## Query the database for info<br \/>\nConst adOpenStatic = 3<br \/>\nConst adLockOptimistic = 3<\/p>\n<p>Set oAPI = CreateObject(&#8220;MOM.ScriptAPI&#8221;)<br \/>\nSet oBag = oAPI.CreatePropertyBag()<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)<br \/>\nSet objRecordSet = CreateObject(&#8220;ADODB.Recordset&#8221;)<\/p>\n<p>objConnection.Open _<br \/>\n&#8220;Provider=SQLOLEDB;Data Source=hq-opsmgr28;&#8221; &amp; _<br \/>\n&#8220;Trusted_Connection=Yes;Initial Catalog=databasename_here;&#8221; &amp; _<br \/>\n&#8220;User ID=domai\\username;Password=Password_here;&#8221;<\/p>\n<p>objRecordSet.Open &#8220;SELECT * FROM machines WHERE FQDN LIKE &#8216;%&#8221; &amp; oArgs(0) &amp; &#8220;%'&#8221;, _<br \/>\nobjConnection, adOpenStatic, adLockOptimistic<\/p>\n<p>varNo = objRecordSet.RecordCount<\/p>\n<p>Do Until objRecordSet.EOF<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;**********************************************&#8221;<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;*\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 *&#8221;<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;*\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Contoso Machine Database \u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 *&#8221;\u00c2\u00a0<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;*\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 *&#8221;<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;**********************************************&#8221;<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8221; &#8221;<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8221; &#8221;<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;Hostname: &#8221; &amp; objRecordSet.Fields.Item(&#8220;Hostname&#8221;)<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;FQDN: &#8221; &amp; objRecordSet.Fields.Item(&#8220;FQDN&#8221;)<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;SLA level: &#8221; &amp; objRecordSet.Fields.Item(&#8220;SLAlevel&#8221;)<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;Owner: &#8221; &amp; objRecordSet.Fields.Item(&#8220;Owner&#8221;)<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;Role: &#8221; &amp; objRecordSet.Fields.Item(&#8220;Role&#8221;)<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;Location: &#8221; &amp; objRecordSet.Fields.Item(&#8220;Location&#8221;)<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;Service: &#8221; &amp; objRecordSet.Fields.Item(&#8220;Service&#8221;)<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;Note: &#8221; &amp; objRecordSet.Fields.Item(&#8220;Note&#8221;)<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8221; &#8221;<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8221; &#8221;<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Wscript.Echo &#8220;**********************************************&#8221;<br \/>\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 objRecordSet.MoveNext<br \/>\nLoop<\/p><\/blockquote>\n<p>I store this script local (C:\\scripts\\querySQL.vbs) on the machine running the console, and call it from a task. The settings of the task are the following<\/p>\n<ul>\n<li>Task Name: Contoso &#8211; query db<\/li>\n<li>Task target: Windows Computer<\/li>\n<li>Application: C:\\windows\\system32\\cmd.exe<\/li>\n<li>Parameters: \/C cscript.exe C:\\scripts\\querySQL.vbs $Target\/Property[Type=&#8221;Windows!Microsoft.Windows.Computer&#8221;]\/PrincipalName$<\/li>\n<li>Working directory: C:\\Windows\\system32<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I have seen a number of questions where Operations Manager operators would like to have more info about\u00c2\u00a0windows machines in the console. You could include info in the console with a new management pack, but if you already have it in another database, it would be easier to query that database direct. I have written &hellip; <a href=\"https:\/\/contoso.se\/blog\/?p=1112\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[13,19],"tags":[],"_links":{"self":[{"href":"https:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1112"}],"collection":[{"href":"https:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1112"}],"version-history":[{"count":9,"href":"https:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1112\/revisions"}],"predecessor-version":[{"id":1122,"href":"https:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1112\/revisions\/1122"}],"wp:attachment":[{"href":"https:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}