3

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!

1 Answer 1

3

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

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

7 Comments

yeah i tried what you said and this happened: Invoke-Command : Parameter set cannot be resolved using the specified named parameters. At line:7 char:2 + Invoke-Command .\sqlpatchremote.ps1 -ArgumentList "-DBServer $sqlsrvname –dbins ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindi ngException + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.I nvokeCommandCommand
If you are only running the script locally, just use . .\sqlpatchremote.ps1 followed by the arguments for the script. You don't need Invoke-Command
either way as i stated in my previous comment, i tried what you told me and it did not work unfortunately =/
is there anyway i should specially identify the variables in the arguments? i tried it and it doesnt seem to be detecting the variables values.
i got this error: Please specify a target environment At C:\SQLPatchRemote.ps1:8 char:32 + [String] $TargetEnv = $(throw, "Please specify a target environment"), + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (System.Object[]:Object[]) [], Runt imeException + FullyQualifiedErrorId : Please specify a target environment
|

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.