0

When I'm executing this command in PowerShell, everything works fine:

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

However, when I'd like to set up several jobs, the following command doesn't work:

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

I got the following error:

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

Could you help me with that issue please?

2 Answers 2

2

Whenever you start a job, the script block inside that job actually runs in a new PS session. Therefore, you are nor longer cd'ed into the same path, but you can do the following to fix that:

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

Or just add a full path in the script-block itself.

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

Comments

0

Powershell 7 has a -workingdirectory parameter too. Actually it looks like start-job or start-threadjob in ps 7 respects the current directory anyway.

Comments

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.