﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Cade Bryant / Article Discussions / Article Discussions by Author  / Execute VBScript commands or .vbs files via T-SQL. / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 22:27:44 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Execute VBScript commands or .vbs files via T-SQL.</title><link>http://www.sqlservercentral.com/Forums/Topic401871-138-1.aspx</link><description>This is a great idea but executing more than 1 line of vbscript code will fail, and you can't output special characters(command chars).You have to either slice up each line of the vbs, loop through and echo each line to append to the file or just use BCP utility to create your vbs file.[code]/***********************************************************************************original script by Cade Bryant: http://www.sqlservercentral.com/scripts/Miscellaneous/30963/updated to enable generating script that is more than 1 line.windows command shell "ECHO" is limited in what it can do. It can't echo more thana single line at a time and it can't echo command/special characters*/CREATE  PROCEDURE usp_ExecVBS@FileOrCommand VARCHAR(4) = NULL,@FileCmdText VARCHAR(8000) = NULLASSET NOCOUNT ONDECLARE @cmd VARCHAR(8000)IF @FileOrCommand = 'file'BEGIN  SET @Cmd = 'CScript ' + '"' + @FileCmdText + '"'  EXEC xp_cmdshell @cmdENDELSE IF @FileOrCommand = 'cmd'BEGINDECLARE   @TempFile SYSNAME     CREATE TABLE ##STAGE     (Command VARCHAR(8000));     INSERT INTO ##STAGE     SELECT @FileCmdText;     SET @TempFile = 'C:\' + CAST(NEWID() AS SYSNAME) +   '.vbs'     SET @Cmd = 'bcp "SELECT COMMAND FROM ##STAGE" queryout ' + @TempFile + ' -T -S .\POOP -c'     exec xp_cmdshell @Cmd;     SET @cmd = 'CScript ' +  '"' + @TempFile + '"'     EXEC xp_cmdshell @cmd;     SET @cmd = 'DEL ' + @TempFile     EXEC xp_cmdshell @cmd;DROP TABLE ##STAGEEND;GO/*******************************************************************************execute with "cmd" method:*******************************************************************************/EXEC usp_ExecVBS 'cmd','Set cdoConfig = CreateObject("CDO.Configuration")Set cdoMessage = CreateObject("CDO.Message")schema = "http://schemas.microsoft.com/cdo/configuration/"With cdoConfig.Fields.Item(schema &amp; "sendusing") = 2.Item(schema &amp; "smtpserver") = "smtp.gmail.com".Item(schema &amp; "smtpserverport") = 465.Item(schema &amp; "smtpusessl") = true.Item(schema &amp; "smtpauthenticate") = 1.Item(schema &amp; "sendusername") = "gary.luis.ellis@gmail.com".Item(schema &amp; "sendpassword") = "%%%%%%%".Item(schema &amp; "smptconnectiontimeout") = 40.updateEnd WithWith cdoMessageSet .Configuration = cdoConfig.From = "gary.luis.ellis@gmail.com".To = "gary.luis.ellis@gmail.com".Subject = "Problems".TextBody = "Problems".Fields.update.SendEnd With';/****************************************************************************** * Wrap this into another procedure to setup simple notification for catch * block of try catch, or when a job completes, etc. *******************************************************************************/CREATE PROC uspEXECVBSMAILASEXEC usp_ExecVBS 'cmd','Set cdoConfig = CreateObject("CDO.Configuration")Set cdoMessage = CreateObject("CDO.Message")schema = "http://schemas.microsoft.com/cdo/configuration/"With cdoConfig.Fields.Item(schema &amp; "sendusing") = 2.Item(schema &amp; "smtpserver") = "smtp.gmail.com".Item(schema &amp; "smtpserverport") = 465.Item(schema &amp; "smtpusessl") = true.Item(schema &amp; "smtpauthenticate") = 1.Item(schema &amp; "sendusername") = "gary.luis.ellis@gmail.com".Item(schema &amp; "sendpassword") = "%%%%%%".Item(schema &amp; "smptconnectiontimeout") = 40.updateEnd WithWith cdoMessageSet .Configuration = cdoConfig.From = "gary.luis.ellis@gmail.com".To = "turbogary2000@yahoo.com".Subject = "Problems".TextBody = "Problems".Fields.update.SendEnd With' EXEC uspEXECVBSMAIL[/code]</description><pubDate>Mon, 01 Jun 2009 09:22:30 GMT</pubDate><dc:creator>turbogary2000</dc:creator></item><item><title>Execute VBScript commands or .vbs files via T-SQL.</title><link>http://www.sqlservercentral.com/Forums/Topic401871-138-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/Miscellaneous/30963/"&gt;Execute VBScript commands or .vbs files via T-SQL.&lt;/A&gt;[/B]</description><pubDate>Mon, 24 Sep 2007 01:35:56 GMT</pubDate><dc:creator>cadebryant</dc:creator></item></channel></rss>