Dpw808
Dpw808

Reputation: 129

Running a script from a powershell script with conditions

So, I have a script I use for deployments and some of these commands aren't recognized till after sqlps is run, usually I do it manually. I want to automate the running of that script. Here is the script:

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

}

When I try to run this script I get this error:

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

It tells me its an invalid command when it normally works when I type this in manually, how can I make this work? This script is suppose to install SQL databases on a SQL server. When I run this code manually I just type "sqlps" and then navigate to the directory of the script. Then I run it with the filled in parameters and it doesn't give me an error. I feel like this might be a simple fix to get this to work, but I'm not sure what it is and I wasn't really sure how to ask it. Thanks!

Upvotes: 3

Views: 645

Answers (1)

sodawillow
sodawillow

Reputation: 13176

Error:

Invoke-Command : A parameter cannot be found that matches parameter name 'DBServer'.
At line:17 char:38

Invoke-Command rejects parameter DBServer. This means you are passing the arguments to Invoke-Command instead of your script.

To pass the arguments to the script you are invoking, you have to use the -ArgumentList parameter.

Try :

Invoke-Command -ComputerName "targethost" .\sqlpatchremote.ps1 -ArgumentList "-DBServer $sqlsrvname –dbinstance $Instance –client $client –mainline HTFS –datefolder $date –targetenv $sqlsrvname"

EDIT: really not sure about the above syntax for the arguments :( (if anyone could confirm?)

With the arguments in the proper order I've successfully tested it like this:

Invoke-Command -ComputerName "targethost" "scriptpath" -ArgumentList $arg1,$arg2#,...

MSDN

Upvotes: 3

Related Questions