How to execute sqlcmd from powershell?

前端 未结 7 1494
忘掉有多难
忘掉有多难 2020-12-29 05:11

I have a string in powershell, which contains a native sqlcmd command. The command itself can be executed successfully in cmd.exe. I have difficulty in executing them in pow

7条回答
  •  悲&欢浪女
    2020-12-29 06:10

    The first positional parameter of invoke-command is -scriptblock, and it expects a script block argument. To take advantage of a here-string to build the command and then run it with invoke-command, you need to convert the here-string to a script block:

    $sql = @"
    sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"
     "@
    
     Invoke-Command ([scriptblock]::create($sql))
    

提交回复
热议问题