使用 sqlcmd 的批处理文件需要正确的用户权限
batch file with sqlcmd needs correct user permissions
我有一个 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
我有一个 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