Something like this:
DECLARE @fileName varchar(128);
DECLARE @sql varchar(max);
SET @sql = 'powershell.exe -c "get-service | C:\myPowershell.ps1 -' + @fileName + '"';
EXEC xp_cmdshell @sql;
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello all,
Please tell , how can i execute the Powershell script using Stored procedure.
i need to execute it .. with one parameter as file name
please tell..
Something like this:
DECLARE @fileName varchar(128);
DECLARE @sql varchar(max);
SET @sql = 'powershell.exe -c "get-service | C:\myPowershell.ps1 -' + @fileName + '"';
EXEC xp_cmdshell @sql;
Hi @Rakesh Dhar ,
Welcome to the Microsoft TSQL Q&A Forum!
If you must execute the Powershel script through a stored procedure, then you can refer to Guoxiong's method, but I also recommend that you use Powershell to extract sql server data instead of using sql to execute Powershell.
If you have any question, please feel free to let me know.
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
I highly recommend you use PowerShell to pull data from SQL Server, rather than trying to execute PowerShell from xp_cmdshell.