从条件为

时间:2015-12-14 17:11:04

标签: sql sql-server powershell

所以,我有一个用于部署的脚本,其中一些命令直到运行sqlps之后才被识别,通常我手动执行。我想自动运行该脚本。这是脚本:

    $client = Read-Host "Enter Client name"
    $date = Get-Date -Format "yymmdd"
    $sqlsrvname = Read-Host "Please enter the sql server name"
    $deploytype = Read-Host "Is there a server instance? (1) Yes (2) No"


   switch($deploytype){

   1 {$Instance = Read-Host "Please Enter instance name" 
    cd -Path $ppath
    Invoke-Command .\sqlpatchremote.ps1 -DBServer $sqlsrvname –dbinstance $Instance –client $client –mainline HTFS –datefolder $date –targetenv $sqlsrvname } 

 2 {cd -Path $ppath
 Invoke-Command .\sqlpatchremote.ps1 –dbs $sqlsrvname –client $client –mainline HTFS –datefolder $date –targetenv $sqlsrvname }

 default {"Invalid Selection"}

}

当我尝试运行此脚本时,我收到此错误:

Invoke-Command : A parameter cannot be found that matches parameter name 'DBServer'.
At line:17 char:38
+  Invoke-Command .\sqlpatchremote.ps1 -DBServer $sqlsrvname –dbinstance $Instance 
...
+                                      ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindi 
   ngException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands. 
   InvokeCommandCommand

当我手动输入它时,它告诉我它无效的命令正常工作,我怎样才能使这个工作?此脚本假定在SQL Server上安装SQL数据库。当我手动运行此代码时,我只需输入" sqlps"然后导航到脚本目录。然后我用填充的参数运行它并没有给我一个错误。我觉得这可能是一个简单的解决方案,让这个工作,但我不知道它是什么,我不知道如何问它。谢谢!

1 个答案:

答案 0 :(得分:3)

错误:

Invoke-Command : A parameter cannot be found that matches parameter name 'DBServer'.
At line:17 char:38

Invoke-Command拒绝参数DBServer。这意味着您将参数传递给Invoke-Command而不是您的脚本。

要将参数传递给您正在调用的脚本,您必须使用-ArgumentList参数。

尝试:

Invoke-Command -ComputerName "targethost" .\sqlpatchremote.ps1 -ArgumentList "-DBServer $sqlsrvname –dbinstance $Instance –client $client –mainline HTFS –datefolder $date –targetenv $sqlsrvname"

编辑:真的不确定参数的上述语法:((如果有人可以确认?)

按照正确的顺序,我已经成功地测试了它:

Invoke-Command -ComputerName "targethost" "scriptpath" -ArgumentList $arg1,$arg2#,...

MSDN