gpt4 book ai didi

sql - 使用条件从 powershell 脚本运行脚本

转载 作者:行者123 更新时间:2023-12-01 23:42:32 25 4
gpt4 key购买 nike

所以,我有一个用于部署的脚本,其中一些命令在运行 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 服务器上安装 SQL 数据库。当我手动运行此代码时,我只需键入“sqlps”,然后导航到脚本目录。然后我用填充的参数运行它,它没有给我一个错误。我觉得这可能是使它正常工作的简单修复,但我不确定它是什么,我也不确定如何询问它。谢谢!

最佳答案

错误:

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

关于sql - 使用条件从 powershell 脚本运行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34272774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com