I have a script that contains a parameter list of connection strings to some databases. The script does the following things:
- Takes a database name as input using "Read-Host"
- Uses the input to meet any of the If/ElseIf conditions and...
- connects to the database
- runs a SQL script to update the db
Here is some example code:
param (
# Connection Strings
[string] $TestDb1_ConnectStr = "username/pwd@//hostname1:port1/sid1",
[string] $TestDb2_ConnectStr = "username/pwd@//hostname2:port2/sid2",
[string] $TestDb3_ConnectStr = "username/pwd@//hostname2:port3/sid3",
[string] $SQL_Path = "C:\bin\sql"
)
$Environment = Read-Host "Please enter environment name..."
If ($Environment -eq "TestDb1")
{
function Invoke-SqlPlus($file) { (gc $file) | sqlplus $TestDb1_ConnectStr }
new-alias sql Invoke-SqlPlus
sql $SQL_Path\$Environment"_sometable.sql"
}
ElseIf ($Environment -eq "TestDb2")
{
function Invoke-SqlPlus($file) { (gc $file) | sqlplus $TestDb2_ConnectStr }
new-alias sql Invoke-SqlPlus
sql $SQL_Path\$Environment"_sometable.sql"
}
ElseIf ($Environment -eq "TestDb3")
{
function Invoke-SqlPlus($file) { (gc $file) | sqlplus $TestDb3_ConnectStr }
new-alias sql Invoke-SqlPlus
sql $SQL_Path\$Environment"_sometable.sql"
}
...
I actually have 14 databases that I can connect to and instead of having 14 separate If/ElseIf conditions I want to be able to use the input from Read-Host to pass in the connection string and SQL script. Currently I can use the input to pass in the SQL script but I'm not able to pass in the db connection string.
I want to do something like this:
function Invoke-SqlPlus($file) { (gc $file) | sqlplus $Environment_ConnectStr }
new-alias sql Invoke-SqlPlus
sql $SQL_Path\$Environment"_sometable.sql"
However, I'm not sure how I can use the input from Read-Host to get the value from the parameter list for the connection string. How can I achieve this?
SQLPlus.exe 'username/pwd@//hostname1:port1/sid1' '@C:\MyScript.sql'?function Invoke-SqlPlus($file) { (gc $file) + 'exit' | sqlplus (get-variable "$($Environment)ConnectStr").value } new-alias sql Invoke-SqlPlus sql $SQL_Path\$Environment"_db_table.sql"