Quick Tip: Navigating to a UNC within SQLPS

I’ve been getting a funky error with a new Powershell script, running as a SQL Agent job step. Let’s pretend for the moment that the script is simply “cd \\backup\SQL; dir *.bak

The error information returned by PowerShell is: ‘Cannot find path ‘\\backup\SQL’ because it does not exist.

The script I have runs fine manually in Powershell, but not in SQLPS. The job itself runs under the SQL Agent service account, which is an AD account, which has full permissions to the share. That should really cover all the bases, shouldn’t it?

As it turns out, no. A web search turned up this exact issue in Microsoft Connect. At least the workaround is simple! You have to get out of the SQL Server “context” in SQLPS before you try to hit a UNC path. So I just added this to the start of my script:

cd c:\;

Making the full script like this:

cd c:\; cd \\backup\SQL; dir *.bak

And that’s it! I do love a simple solution…

Happy days,
Jen McCown

One thought on “Quick Tip: Navigating to a UNC within SQLPS”

Leave a Reply

Your email address will not be published. Required fields are marked *