Remote execute sql script for one server to another
<p>I have a "test.sql" in Server1, and I would like to run this script on server2 and server3. I utilized the code below:</p>

<pre><code>$InstanceNm = "server2\inst2,1433"

$CommandExecute_SP = "D:\testscript\test.sql" --located in Server1

invoke-expression "SQLCMD -E -S $InstanceNm -d 'master' -i  $CommandExecute_SP -b"
</code></pre>

<p>I run the all the codes from server1 and I target server2, but I am getting this error:</p>

<pre><code>SQLCMD : Sqlcmd: 'server2\inst2,1433': Unexpected argument. Enter '-?' for help.
</code></pre>

<p>Then when i type <code>-?</code>
It gives this:</p>

<pre><code>At line:1 char:2
+ -?
+  ~
Missing expression after unary operator '-'.
At line:1 char:2
+ -?
+  ~
Unexpected token '?' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator
</code></pre>

<p>I was wondering what the correct way is to run a .sql file which creates objects on another server from a separate server? I know how to run short sql statements via using <code>System.Data.SQLClient.SQLConnection</code> but I would like to avoid that since this .sql file has over 1000 lines of code.</p>