Friday, December 4, 2015

PowerShell - Run PowerShell Script from SQL

In sql script, use xp_cmdshell to execute your script like this:

DECLARE @SQL VARCHAR(8000), @ResultCode varchar(2000), @ErrorMessage varchar(5000)
SELECT @SQL='%SystemRoot%\syswow64\WindowsPowerShell\v1.0\PowerShell -File "C:\Temp\ArrowTest.ps1"'
CREATE TABLE #TempResult (SomeCol VARCHAR(500))
SELECT @ErrorMessage = ''
INSERT #TempResult EXEC @ResultCode = xp_cmdshell @SQL


You need to decipher the value in #TempResult because it is the returned code (error or success) from the script.

No comments:

Post a Comment