0

I have this script here:

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"}

}

It creates input that calls another script to create SQL databases. I now have to combine the two scripts into one script. Is there an easy way to do that so that I can take the input this script normally generates and launch the other script?

The beginning of the other script looks like this:

  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")
)

This is the output my script generates that normally launches the second script: .\sqlpatchremote.ps1 -DBServer $sqlsrvname –dbinstance $Instance –client $client –mainline HTFS –datefolder $date –targetenv $sqlsrvname

So I guess I'm not sure where I am suppose to plug the output of my script to launch this other script from within the same script. I mainly want them to act like separate scripts even though they're in the same script. Or blend them together which I imagine would be more complicated. Also, I need it to be compatible with Windows Server 2008, so I cant use newer PowerShell commands. Unfortunately, a lot of our clients are stuck in the past and still use Windows Server 2008.

5
  • Why, though? What's the idea behind a shitload of Read-Host statements, why can't your user just use the parameter names and tab completion? Commented Jan 13, 2016 at 16:43
  • I am the user for this and I like using read-host because I like to spell out all the parts of my script to make it nice and easy to follow. Also my boss wants me to have users input the paths manually so that's why they're there. I'm kinda OCD when it comes to scripting I like it to look a certain way and spelling out my commands and making it user friendly is just my preference. Also my boss is the one who wants me to combine these scripts into 1. I was happy running them separately before. Commented Jan 13, 2016 at 16:48
  • Also the read-host is necessary because that information is always different. Commented Jan 13, 2016 at 16:55
  • Windows Server 2008 with SP1 can get your servers up to PowerShell v3.0. Lots of new functionality came with that. It's worth a shot if you can get your clients to install it. Commented Jan 13, 2016 at 22:28
  • sadly the clients wont do that =( we work with pretty much only financial clients so they're super uptight about literally everything. Commented Jan 14, 2016 at 17:58

1 Answer 1

2

You could wrap the contents of the "other" script in a function and put your Read-Host statements at the end before calling the function:

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"}
}

Although I must say I regard this approach to be an anti-pattern that you'd want to avoid.

Sign up to request clarification or add additional context in comments.

7 Comments

This script is massively long, that is just a small fraction of what i think has to do with what senses when i just manually type out the input that the program i made does instead. You sure moving stuff around like that wont break everything? i mean ill try it. My issue is I guess I don't totally understand out the param works in this scenario which is why i didn't quite know how to plug it in. Ill try what you said and get back to you.
It may break stuff, since the parameter set is no longer available outside of the script (which is why I'd recommend against doing what you're trying to do). So if you have others (scripts or people) that depend on the ability to call .\sqlpatchremote.ps1 -DBServer mydb01 or similar, that will no longer work. My advice is to keep them separate
can do that without having 2 separate scripts? or do i just have to have 2 separate scripts? lol
I tried what you said and it worked! it did not break everything! i will mark this as an answer once i've tested it in a real environment. I get it, the function basically puts code into a variable. That's basically what I was trying to find out. thanks!
Nope it did not work, it asked me to specify a web server.. didn't get an error till that. Which means somehow the manual input i usually type in ties into this? even though i never have to specify a web server...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.