Blake Norwood
Blake Norwood

Reputation: 63

How do I run a command from a network path in PowerShell/batch file

Essentially what I'm trying to run is this:

cd /d W:\RemoteMirror\ && winscp.com /ini=nul /script=Remote_Mirror_WinSCP.txt

or this:

cd /d \\fileServer\RemoteMirror\ && winscp.com /ini=nul /script=Remote_Mirror_WinSCP.txt

Of course, with SQL Server, I can't use UNC paths. But I'm not sure how to do it otherwise. I'm not too familiar with PowerShell yet, but that'd be my best guess.

Upvotes: 1

Views: 2119

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202282

UNC path cannot be a current working directory (that's a Windows limitation, it has nothing to do with SQL server).


As @lit already commented, you can temporarily allocate a drive letter for a UNC path and change working directory to it at once, using pushd command.


Alternatively, modify your WinSCP script not to rely on the current working directory.

Just use full UNC paths in the script, like:

get "/remote/path/*" "\\fileServer\RemoteMirror\"

See also Using batch file and WinSCP to download files from the FTP server to file server (shared folder)


Or, if you need to make the script working with different directories, use a parameterized script:

get "/remote/path/*" "%1%\"

And run it like:

winscp.com /ini=nul /script=Remote_Mirror_WinSCP.txt /parameter \\fileServer\RemoteMirror

See a full example for using parameterized scripts.

Upvotes: 1

Related Questions