Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / July 2008

Tip: Looking for answers? Try searching our database.

xp_cmdshell cannot map drive in sql analyzer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nupee1 - 30 Apr 2008 22:43 GMT
Hello...I know there have been several posts about this but I still can't
resolve my problem. I'm using sql 2005 xp_cmdshell statement to try and
access a mapped drive or to map a drive using net use in a query analyzer
session as follows:

Declare @Execstring varchar(5000)

set @Execstring = 'Dir M:'
Exec master..xp_cmdshell @Execstring

OR

Declare @Execstring varchar(5000)

set @Execstring = 'net use L: \\servername\f$\  /user: domain name\username
password'

Exec master..xp_cmdshell @Execstring

I get the error message "The network name cannot be found."

I've tried several variations of the script with no success. For the first
query if i go to a dos prompt on the server and type the drive letter "M:"
which is already mapped, I get results. I've already verified that the sql
service acct can access the mapped drive and that this acct is sysadmin on
the box. The mapped drive is to another server. Thanks!!
Aaron Bertrand [SQL Server MVP] - 30 Apr 2008 23:50 GMT
Use the UNC path.  You can't map drives in SQL Server... it runs under a
service account.

> Hello...I know there have been several posts about this but I still can't
> resolve my problem. I'm using sql 2005 xp_cmdshell statement to try and
[quoted text clipped - 23 lines]
> service acct can access the mapped drive and that this acct is sysadmin on
> the box. The mapped drive is to another server. Thanks!!
Nupee1 - 01 May 2008 15:38 GMT
I alse executed the command EXEC sp_xp_cmdshell_proxy_account
AccountName,'password', to grant permissions to this account. I can't
understand why I can perform the operation through a dos window and see and
access the mapped drive, yet I cannot seem access the drive through
xp_cmdshell.

> Use the UNC path.  You can't map drives in SQL Server... it runs under a
> service account.
[quoted text clipped - 26 lines]
> > service acct can access the mapped drive and that this acct is sysadmin on
> > the box. The mapped drive is to another server. Thanks!!
Linchi Shea - 01 May 2008 03:23 GMT
Your second approach should work fine. The mapped drive using net use will
survive different T-SQL. Double check your share name.

Linchi

> Hello...I know there have been several posts about this but I still can't
> resolve my problem. I'm using sql 2005 xp_cmdshell statement to try and
[quoted text clipped - 22 lines]
> service acct can access the mapped drive and that this acct is sysadmin on
> the box. The mapped drive is to another server. Thanks!!
Roy Harvey (SQL Server MVP) - 01 May 2008 20:21 GMT
>Your second approach should work fine. The mapped drive using net use will
>survive different T-SQL. Double check your share name.
>
>Linchi

I thought anything set through NET USE within xp_cmdshell only lasted
as long as the execution of xp_cmdshell.  Are you sure it will
survive?

Roy Harvey
Beacon Falls, CT
John Bell - 01 May 2008 08:32 GMT
> Hello...I know there have been several posts about this but I still can't
> resolve my problem. I'm using sql 2005 xp_cmdshell statement to try and
[quoted text clipped - 22 lines]
> service acct can access the mapped drive and that this acct is sysadmin on
> the box. The mapped drive is to another server. Thanks!!

Hi

To add my bit! You are trying access an administrative share make sure the
security context of the SQL Server Service account or the
xp_sqlagent_proxy_account may not be sufficient to allow access.

I would use the UNC name as you can never be sure what else could be mapped
to a drive and mapping/remapping it may break something else!

John
Nupee1 - 01 May 2008 15:07 GMT
Sorry I forgot to mention that I have tried the unc path as well. Same error

Declare @Execstring varchar(5000)

set @Execstring = 'net use \\servername\share\  /user: domain name\username'
               
Exec master..xp_cmdshell @Execstring

> Hello...I know there have been several posts about this but I still can't
> resolve my problem. I'm using sql 2005 xp_cmdshell statement to try and
[quoted text clipped - 22 lines]
> service acct can access the mapped drive and that this acct is sysadmin on
> the box. The mapped drive is to another server. Thanks!!
John Bell - 04 May 2008 06:15 GMT
> Sorry I forgot to mention that I have tried the unc path as well. Same
> error
[quoted text clipped - 37 lines]
>> on
>> the box. The mapped drive is to another server. Thanks!!

Hi

If you are using the UNC then you should not need to use the NET command! As
from my previous post I think this is permissions for the admin share have
not been granted to the account being used.

John
Ceduljko - 18 Jul 2008 10:53 GMT
NET USE is needed if you need to provide credentials.

Try this:

exec master..xp_cmdshell 'net use \\myserver.mydomain.com\MyShareFolder
password /user:DOMAIN\username /PERSISTENT:YES'

exec master..xp_cmdshell 'dir
"\\myserver.mydomain.com\MyShareFolder\SubDir\*.txt" '

The first command ensures the persistence of your credentials for the
current SQL Server instance and the second one uses UNC path to access
the files.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.