Batch file 带有sqlcmd的批处理文件需要正确的用户权限

Batch file 带有sqlcmd的批处理文件需要正确的用户权限,batch-file,sqlcmd,Batch File,Sqlcmd,我有一个SQL脚本,该脚本在SQL Management Studio中运行,但无法作为批处理执行。 我发现,当我使用路径C中的import.csv文件而不是所需的网络路径(D:)时,它正在工作 批量 rem ---------- SQL Import/Merge Configbuilder File ---------- SET LOGFILE=MyLogFile.log call :Logit >> %LOGFILE% exit /b 0 :Logit sqlcmd -S lo

我有一个SQL脚本,该脚本在SQL Management Studio中运行,但无法作为批处理执行。
我发现,当我使用路径C中的
import.csv
文件而不是所需的网络路径(D:)时,它正在工作

批量

rem ---------- SQL Import/Merge Configbuilder File ----------
SET LOGFILE=MyLogFile.log
call :Logit >> %LOGFILE% 
exit /b 0
:Logit
sqlcmd -S localhost\SERVER1 -d Database-U sa -P Password -i "Import.sql"
CMD
import.sql

BULK
INSERT Configbuildertemp
FROM 'D:\Batchfiles\Configbuilderimport.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ';',
ROWTERMINATOR = '\n'
)
GO
问题:

当我(与管理员在服务器上连接)期望的目标是通过Windows任务调度器执行时,哪个用户正在批处理文件中执行sqlcmd请求


但是,我可以批量插入以将文件复制到C:\并在执行后再次删除它,但这将是一个难题。

在Windows任务计划程序中,您可以为每个任务定义应使用哪个用户帐户来运行任务。如果为运行批处理文件的任务定义有权从包含
Configbuilderimport.csv
的网络共享中读取文件的用户帐户,则可以使用如下批处理文件:

rem Remove a perhaps already existing network share mapped to drive D:.
%SystemRoot%\system32\net.exe use D: /delete /yes 2>nul

rem Temporarily map \\ServerName\NameOfShare to drive D: using credentials
rem of user account running this batch file as defined in task scheduler.
%SystemRoot%\system32\net.exe use D: \\ServerName\NameOfShare /persistent:no

rem ---------- SQL Import/Merge Configbuilder File ----------
SET LOGFILE=MyLogFile.log
call :Logit >> %LOGFILE%

rem Remove network share mapped to drive D:.
%SystemRoot%\system32\net.exe use D: /delete /yes 2>nul
exit /b 0

:Logit
sqlcmd.exe -S localhost\SERVER1 -d Database-U sa -P Password -i "Import.sql"
goto :EOF
但是,如果批处理文件由本地管理员帐户执行,而本地管理员帐户当然无权访问
\\ServerName\NameOfShare
上的文件,则需要在批处理文件中指定密码、域和用户名。与使用正确的用户帐户定义的任务相比,这更不安全,因为访问批处理文件的每个人都可以读取密码。在任务计划程序中为用于运行批处理文件的用户帐户输入的密码由Windows加密

rem Remove a perhaps already existing network share mapped to drive D:.
%SystemRoot%\system32\net.exe use D: /delete /yes 2>nul

rem Temporarily map \\ServerName\NameOfShare to drive D: using credentials
rem specified here directly in the batch file in the line below.
%SystemRoot%\system32\net.exe use D: \\ServerName\NameOfShare password /user:domain\username /persistent:no

rem ---------- SQL Import/Merge Configbuilder File ----------
SET LOGFILE=MyLogFile.log
call :Logit >> %LOGFILE%

rem Remove network share mapped to drive D:.
%SystemRoot%\system32\net.exe use D: /delete /yes 2>nul
exit /b 0

:Logit
sqlcmd.exe -S localhost\SERVER1 -d Database-U sa -P Password -i "Import.sql"
goto :EOF
为什么本地管理员“当然”没有权限?因为当我连接到服务器时,我已经映射了网络驱动器,甚至批处理文件的位置都在网络中的Nass驱动器上。另外,谢谢,我会尝试一下,当我下班回家时,我想用“本地管理员”这个本地系统帐户,它在Windows默认情况下只有本地机器上的管理员权限。但也可以在ComputerA上使用具有本地管理员权限的本地UserX帐户,该帐户也可以配置为对ComputerB上的共享进行读取、读取/写入,甚至完全访问。关于其他机器上的权限,如果网络是具有本地用户的自我管理的专用网络,或者是具有域用户的域网络,则会有所不同。持久网络驱动器映射与用户相关,仅在登录时存在。