2011-01-06 83 views
4

我有一個如下的主PowerShell腳本。它定義了一個變量$ V1然後啓動使用這個變量的sqlps。由於SQLPS本身就是一個minishell它不承認$ V1如何從主窗口powershell腳本傳遞參數到sqlps

$V1 = "asdasd"  

sqlps -NoLogo -Command { 
      invoke-sqlcmd -Query $V1 -ServerInstance "SomeImstamce" -Database "SomeDatabase" -Username "SomeUsernName" -Password "SomePassword" 
     } 

因此,我更新了SQLPS -Command塊預期參數如下。但是我不知道如何將外$ V1變量的值傳遞給內部SQLPS -Command座

$V1 = "RBIQHSAPPD049v.b2b.regn.net" 

sqlps -NoLogo -Command { 
      param ($SqlPsParam) 
      invoke-sqlcmd -Query $SqlPsParam -ServerInstance "SomeImstamce" -Database "SomeDatabase" -Username "SomeUsernName" -Password "SomePassword" 
     } 

上有http://technet.microsoft.com/en-us/library/cc280450.aspx語法的幫助,我已經嘗試了許多組合,但它亙古不變的似乎工作。

有什麼想法?

回答

3

哦弄明白atlast

$V1 = "sdfsaf" 
$V2 = "safd" 

sqlps -NoLogo -Command { 
      param ($SqlPsParam1, $SqlPsParam2) 
      invoke-sqlcmd -Query $SqlPsParam -ServerInstance "SomeImstamce" -Database "SomeDatabase" -Username "SomeUsernName" -Password "SomePassword" 
     } -args $V1, $V2 
+0

非常有幫助的例子 - 我有同樣的需要使用sqlps.exe實用程序中的父腳本變量。謝謝 :-)。 – 2015-02-03 23:08:44

相關問題