将来自两个不同PowerShell脚本的代码合并到一个脚本中

时间:2016-01-13 15:54:58

标签: sql sql-server-2008 powershell

我在这里有这个脚本:

SQLPS

$ppath = Read-Host "Please enter the package database folder path" 
$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
 .\sqlpatchremote.ps1 -DBServer $sqlsrvname –dbinstance $Instance –client $client –mainline HTFS –datefolder $date –targetenv $sqlsrvname} 

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

 default {"Invalid Selection"}

}

它创建调用另一个脚本来创建SQL数据库的输入。我现在必须将两个脚本合并为一个脚本。有没有一种简单的方法可以实现这个脚本通常生成的输入并启动其他脚本?

另一个脚本的开头如下所示:

  param
(
    [String] $PatchVersion = "RemoteVersion",
    [String] $DBNameList   = "alldatabases",
    [String] $Client       = "noclient",
    [String] $DBServer     = "localhost",
    [String] $DBInstance   = "",
    [String] $TargetEnv    = $(throw, "Please specify a target environment"),
    [String] $MainLine     = $(throw, "Please specify a mainline"),
    [String] $DateFolder   = $(throw, "Please specify a date folder")
)

这是我的脚本生成的输出,通常会启动第二个脚本:.\sqlpatchremote.ps1 -DBServer $sqlsrvname –dbinstance $Instance –client $client –mainline HTFS –datefolder $date –targetenv $sqlsrvname

所以我想我不确定在哪里插入我的脚本输出以从同一个脚本中启动这个其他脚本。我主要希望它们像单独的脚本一样,即使它们在同一个脚本中。或者将它们混合在一起,我想这会更复杂。此外,我需要它与Windows Server 2008兼容,所以我不能使用较新的PowerShell命令。不幸的是,我们的很多客户过去都停留在使用Windows Server 2008。

1 个答案:

答案 0 :(得分:2)

你可以包装"其他"的内容。函数中的脚本,并在调用函数之前将Read-Host语句放在最后:

CombinedScript.ps1

function Patch-RemoteSQL
{
    param
    (
        [String] $WebServer    = $(throw, "Please specify a Web server"),
        [String] $AppServer    = $(throw, "Please specify an App server"),
        [String] $DBServer     = $(throw, "Please specify a database server"),
        [String] $DBInstance   = "", #$(throw, "Please specify a database instance"),
        [String] $PatchVersion = "RemoteVersion"    
    )

    # Script Body in here
}

$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" 
    Patch-RemoteSQL -DBServer $sqlsrvname –dbinstance $Instance –client $client –mainline HTFS –datefolder $date –targetenv $sqlsrvname
  }

  2 { 
    Patch-RemoteSQL –dbserver $sqlsrvname –client $client –mainline HTFS –datefolder $date –targetenv $sqlsrvname 
  }

  default {"Invalid Selection"}
}

虽然我必须说我认为这种做法是你想要避免的反模式。