1 回答

TA貢獻(xiàn)1794條經(jīng)驗 獲得超8個贊
經(jīng)過幾個小時的努力,簡單的解決方案是將命令分為兩部分,憑證部分和 sc 命令部分,如下所示。現(xiàn)在它按預(yù)期工作。
附言。我選擇從命令中刪除 echo 以避免在日志文件中打印憑據(jù)。
public void startService(int serviceId, String serviceName, String
ipAddress) {
CMDExecutor executor = new CMDExecutor();
try {
String credentialsCommand = "cmd /C echo "+ password +" runas /user:"+ username;
String startServiceCommand = "sc\\\\" +ipAddress+ " start "+ serviceName";
String credentialsResult = executor.getCMDResult(credentialsCommand );
logger.info(credentialsResult );
String startServiceResult= executor.getCMDResult(startServiceCommand );
logger.info(startServiceResult);
}
添加回答
舉報