在Pwershell作业中使用sqlcmd时出错

问题描述 投票:0回答:1

当我在Powersheel中执行此命令时,一切正常:

sqlcmd -S dwh -i ".\script.sql" -o ".\log.txt"

但是,当我想设置一些作业但以下命令不起作用时:

Start-Job -Name TestSqlCmd -ScriptBlock {sqlcmd -S dwh -i ".\script.sql" -o ".\log.txt"}

我遇到以下错误:

Sqlcmd: Error: Error occurred while opening or operating on file .\script.sql (Reason: The system cannot find the path specified).
    + CategoryInfo          : NotSpecified: (Sqlcmd: Error: ...ath specified).:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
    + PSComputerName        : localhost

您能帮我解决这个问题吗?

powershell sqlcmd
1个回答
0
投票

[每当开始一个作业时,该作业中的脚本块实际上就在新的PS会话中运行,不再需要进入相同的路径,可以执行以下操作来解决该问题:

Start-Job -Name TestSqlCmd -ScriptBlock {
    Param(
        $path
    )
    echo "$path\script.sql" 
    echo "$path\log.txt"
} -ArgumentList (Get-Location).Path

或者只是在脚本块本身中添加完整路径。

© www.soinside.com 2019 - 2024. All rights reserved.