2015-08-19 34 views
2

我試圖從SQL Server執行Power Shell腳本。 通過參數的靜態值,每件事情都對我有用。 但是當我用動態參數嘗試它不工作。如何在從SQL調用的Powershell中傳遞參數存儲過程

以下是我試圖聲明:

declare @dteBatchDate as varchar(20) 
set @dteBatchDate='2014-06-23 15:49:00' 
EXEC xp_cmdshell 'powershell.exe -c "get-service | D:\Listing1.ps1 [email protected]"' 

我認爲這是一個語法問題。任何機構都可以提出解決方案。

在先進的感謝

回答

3

試試這個:

declare @dteBatchDate as varchar(20) 
declare @sql as varchar(255) 
set @dteBatchDate='2014-06-23 15:49:00' 
set @sql = 'powershell.exe -c "get-service | D:\Listing1.ps1 -' + @dteBatchDate + '"' 
EXEC xp_cmdshell @sql 
+0

謝謝它適用於我。在傳遞參數之前稍有改變,不需要有' - '符號。 –

+0

我剛剛修復了您提供的聲明。沒辦法檢查,因爲我在我的手機上;-) – Scoregraphic

+0

我試過這個,但它不接受帶有空格的參數。你怎麼做的? – Gsquare

4

要傳遞的變量xp_cmdshell的方式不允許它被渲染爲PowerShell來看看它。你基本上是這樣做從PowerShell提示符的觀點:

powershell.exe -c "get-service | D:\Listing1.ps1 [email protected]" 

以同樣的方式,當你需要之前在PowerShell中執行命令的變量渲染,你必須做同樣的事情xp_cmdshell

因此,在將它傳遞給xp_cmdshell之前構建您的命令。 enter image description here

+0

這是一個很好的答案,因爲它解釋了*問題導致的問題。 – vonPryz

相關問題