在PowerShell中执行动态命令

时间:2016-08-25 14:32:56

标签: powershell

我的脚本如下

New-Alias -Name sqlpackage -Value "C:\Program Files (x86)\Microsoft SQL 
Server\130\DAC\bin\sqlpackage.exe"
$command = "sqlpackage /Action:Publish /SourceFile:$sourceFile /tcs:$TargetConnectionString /p:ScriptRefreshModule="+"$ScriptRefreshModule"+" /p:ScriptNewConstraintValidation="+"$ScriptNewConstraintValidation"+ `
" /p:GenerateSmartDefaults="+"$GenerateSmartDefaults"+" /p:BlockOnPossibleDataLoss="+"$BlockOnPossibleDataLoss"+" /p:BackupDatabaseBeforeChanges="+"$BackupDatabaseBeforeChanges"+ `
" /v:ErrorEmailRecipients="+"$ErrorEmailRecipients"+" /v:DeploymentEnvironment="+"$environment"+ $additionalVariables
& $command

如果找不到sqlpackage cmdlet,则会失败。我尝试将整个exe路径放到命令中,而不是使用也没有改变结果的别名。

我还尝试了Invoke-CommandInvoke-ItemInvoke-Expression,但都未能成功运行。我在想bash脚本可能是我的垮台。 我该怎么做才能使这个命令工作?

它需要是动态的,因为变量根据源文件而变化。

2 个答案:

答案 0 :(得分:0)

New-Alias无法正常工作,因为它定义了现有cmdlet的别名。 此功能对我有用(根据herehere):

function sqlpackage(
    $sourceFile `
    ,$TargetConnectionString `
    ,$ScriptRefreshModule `
    ,$ScriptNewConstraintValidation `
    ,$GenerateSmartDefaults `
    ,$BlockOnPossibleDataLoss `
    ,$BackupDatabaseBeforeChanges `
    ,$ErrorEmailRecipients `
    ,$environment `
    ,$additionalVariable1 `
    ,$additionalVariable2 `
    ,$additionalVariable3 `
)
    {
    $command = "C:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin\SqlPackage.exe"

    &$command `
        /Action:Publish `
        /SourceFile:$sourceFile `
        /tcs:$TargetConnectionString `
        /p:ScriptRefreshModule=$ScriptRefreshModule `
        /p:ScriptNewConstraintValidation=$ScriptNewConstraintValidation `
        /p:GenerateSmartDefaults=$GenerateSmartDefaults `
        /p:BlockOnPossibleDataLoss=$BlockOnPossibleDataLoss `
        /p:BackupDatabaseBeforeChanges=$BackupDatabaseBeforeChanges `
        /v:ErrorEmailRecipients=$ErrorEmailRecipients `
        /v:DeploymentEnvironment=$environment `
       $additionalVariable1 `
       $additionalVariable2 `
       $additionalVariable3 `
}

sqlpackage `
    $sourceFile `
    $TargetConnectionString `
    $ScriptRefreshModule `
    $ScriptNewConstraintValidation `
    $GenerateSmartDefaults `
    $BlockOnPossibleDataLoss `
    $BackupDatabaseBeforeChanges `
    $ErrorEmailRecipients `
    $environment `
    $additionalVariable1 `
    $additionalVariable2 `
    $additionalVariable3 

答案 1 :(得分:0)

我使用了你的部分答案,离开了Invoke和&它起作用了。此外,New-Alias部分也在运作。

New-Alias -Name sqlpackage -Value "C:\Program Files (x86)\Microsoft SQL Server\130\DAC\bin\sqlpackage.exe"
sqlpackage `
  /Action:Publish `
  /SourceFile:$sourceFile `
  /tcs:$TargetConnectionString `
  /p:ScriptRefreshModule=$ScriptRefreshModule `
  /p:ScriptNewConstraintValidation=$ScriptNewConstraintValidation `
  /p:GenerateSmartDefaults=$GenerateSmartDefaults `
  /p:BlockOnPossibleDataLoss=$BlockOnPossibleDataLoss `
  /p:BackupDatabaseBeforeChanges=$BackupDatabaseBeforeChanges `
  /v:ErrorEmailRecipients=$ErrorEmailRecipients `
  /v:DeploymentEnvironment=$environment.LifeCycle `
  $additionalVariables