本文介绍了执行xp_cmdshell'wmic ...'java -jar ...''时使用哪个帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 jar 文件,该文件要作为 SQL作业中的一个步骤运行.但是,jar文件必须在 machineA 上运行,但SQL作业是在 serverA 上安排的.

I have a jar file that I want to run as a step in an SQL Job.However, the jar file has to run on machineA but the SQL job is schedule on serverA.

为此,在serverA的SQL作业中,我使用xp_cmdshell向终端发出wmic命令.

To make this possible, in serverA's SQL job, I use xp_cmdshell to issue a wmic command to the terminal.

xp_cmdshell允许我从T-SQL脚本发出终端命令

xp_cmdshell permits me to issue a terminal command from an T-SQL script

wmic允许我向计算机发出终端命令(在本例中为Java -jar命令)

wmic permits me to issue a terminal command to machine (in this case a java -jar command)

下面是我使用的命令

EXEC master..xp_cmdshell 'wmic /user:mydomain\myuser /password:mypassword /node:machineA process call create "cmd /c java -jar D:\jars\saveToSharedFolder.jar"'

saveToSharedFolder.jar 是一个Java应用程序,可从数据库中提取数据并将其写入Excel文件,然后将该Excel文件保存到位于服务器中的共享文件夹,例如 \ serverA \ files \ savedData.xlsx .

saveToSharedFolder.jar is a java application that extracts data from the database and writes it to an excel file, then saves this excel file to a shared folder located in a server say, \serverA\files\savedData.xlsx.

savedData.xlsx文件未保存在\ serverA \ files中.

The file savedData.xlsx is not being saved in \serverA\files.

当我尝试将Java错误消息输出到文件时,我明白了.

When I tried outputting the java error messages to a file, I got this.

java.io.FileNotFoundException: \\serverA\files\savedData.xlsx (Access is denied)
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : null
    at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:602)
    at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1557)
    at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:248)
    at saveToSharedFolder.saveData(saveToSharedFolder.java:115)
    at saveToSharedFolder.main(saveToSharedFolder.java:46)
    ... 5 more
Caused by: java.lang.NullPointerException
    at java.util.zip.DeflaterOutputStream.<init>(Unknown Source)
    at java.util.zip.DeflaterOutputStream.<init>(Unknown Source)
    at java.util.zip.ZipOutputStream.<init>(Unknown Source)
    at java.util.zip.ZipOutputStream.<init>(Unknown Source)
    at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:531)
    ... 9 more

我知道这是一个文件夹权限问题.但是,我对于应该授予哪个帐户对该文件夹的写权限感到茫然.

I know it is a folder permission issue. However, I am quite loss as to what account should I grant write permission to the folder.

当我尝试使用我在wmic命令中使用的mydomain\myuse r和mypassword登录到machineA并在machineA中运行jar文件时,它成功保存了\ serverA \ files \ savedData.xlsx

When I try to login in machineA using mydomain\myuser and mypassword that I used in the wmic command, and run the jar file in machineA, it successfully saves \serverA\files\savedData.xlsx

我还尝试授予运行EXEC master..xp_cmdshell 'whoami'时获得的nt service帐户.因为那是运行wmic帐户的帐户吗?但是它仍然不会创建\ serverA \ files \ savedData.xlsx

I also tried granting the nt service account I get when I run EXEC master..xp_cmdshell 'whoami'. Because that's the account that runs the wmic account right? But it still doesn't create \serverA\files\savedData.xlsx

但是,这很奇怪,因为我使用mydomain\myusermypassword启动 wmic 时,不应使用myuser的凭据运行jar文件,因此无法创建\ serverA \ files \ savedData .xlsx?

It's quite weird though, Since I launched wmic using mydomain\myuser and mypassword shouldn't the jar file be run with myuser's credentials and thus be able to create \serverA\files\savedData.xlsx?

推荐答案

xp_cmdshell在SQL Server服务帐户的安全上下文中运行.对于非系统管理员角色成员,xp_cmdshell在 xp_cmdshell代理帐户的安全上下文下运行.

xp_cmdshell runs under the security context of the SQL Server service account if the invoking login is a sysadmin role member. For non-sysadmin role members, xp_cmdshell runs under the security context of the xp_cmdshell proxy account.

不确定为什么要使用T-SQL来调用该过程.而是考虑使用CmdExec作业步骤类型直接执行命令.您可以在作业步骤配置(SQL Server代理服务帐户或代理)中指定所需的过程安全上下文.请参见 https://msdn.microsoft.com/en-us/library/ms190264 .aspx .

Not sure why you are using T-SQL to invoke the process. Instead, consider using a CmdExec job step type to execute the command directly. You can specify the desired security context of the process in the job step configuration (SQL Server Agent service account or proxy). See https://msdn.microsoft.com/en-us/library/ms190264.aspx.

这篇关于执行xp_cmdshell'wmic ...'java -jar ...''时使用哪个帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 11:58
查看更多