title: Invoke-sqlcmd to remote server issue: Could not find file
body: <p>I'm trying to write a PowerShell script which will execute tsql query to only one remote server using invoke-sqlcmd. Those tsql queries are simple one like backup/restore database, create a user, etc.
  Below is an extract of it :</p>
  
  <pre><code>  # 5 # Create clientdb database on secondary server by restoring the full backup for primary
    Try {
        Invoke-sqlcmd -ServerInstance 'REMOTESQLSRV'`
          -Username 'ts_sql' -Password 'somepassword'`
          -InputFile "$LScltid\__01_On_Secondary_CreateDB2_srv2.sql"`
          -ErrorAction Stop
  
        Write-Host " clt_$id is now restored to secondary server "`
          -ForegroundColor White -BackgroundColor Green
  } Catch {
      Write-Host " Restore operation for clt_$id did not succeed. Check the error logs " -ForegroundColor Black -BackgroundColor Red 
  }
  </code></pre>
  
  <p>My scripts always break here. For some reasons that i could not put my head on, invoke-sqlcmd does not use the variable "$LScltid" to resolve the path where it will find the .sql script.</p>
  
  <p>Everytime i had to run it, it change the current directory to either the SQLERVER:\ provider or some other causing the script to failed at this step. </p>
  
  <p>Am I doing this the right way? If so, how should i adapt the command to perform as I expect it to.?</p>
  
  <p><strong>UPDATE</strong>
  Forgot to mention, if i run the script with the variables values hard-coded I'm able to get the result i need (in this case restoring a database from device).</p>
  
  <p>Thanks for your feedbacks.</p>