Groovy -如何设置参数外部命令



我有一个软HP BSM。操作符的动作将用Groovy脚本编写。

这是一个例子:

import java.util.List;
import com.hp.opr.api.scripting.Action;
import com.hp.opr.api.scripting.Event;
import com.hp.opr.api.scripting.EventActionFlag;
import com.hp.opr.api.scripting.LifecycleState;
import com.hp.opr.api.scripting.MatchInfo;
import com.hp.opr.api.scripting.NodeInfo;
import com.hp.opr.api.scripting.PolicyType;
import com.hp.opr.api.scripting.Priority;
import com.hp.opr.api.scripting.ResolutionHints;
import com.hp.opr.api.scripting.Severity;
/*
 * This example set all possible event attribute to some example values.
 */
class SimpleExample_new
{
  def init()
  {
    }
    def destroy()
    {
    }
    def process(List<Event> events)
    {
          events.each {
              event -> modifyEvent(event); 
"cmd.exe /c C:\test\sd_event.exe -f C:\test\sd_event.ini -v event_id=2 description='$description' status=Registered priority=low".execute().text
            }
    }
    def modifyEvent(Event event)
    {
// "cmd.exe /c C:\test\sd_event.exe -f C:\test\sd_event.ini -v event_id=10 description='$description' status=Registered priority=low".execute().text
    String application = event.getApplication();
    event.setApplication("Modified by ostap: " + application);
    String description = event.getDescription();
    event.setDescription("Modified by ostap: " + description);
    long groupId = event.getAssignedGroupId();
    event.setAssignedGroupId(groupId);
    int assignedUserId = event.getAssignedUserId();
    event.setAssignedUserId(assignedUserId);
     String category = event.getCategory();
    event.setCategory("Modified by EPI: " + category);
    String correlationKeyPattern = event.getCloseKeyPattern();
    event.setCloseKeyPattern("Modified by EPI: " + correlationKeyPattern);
    String etiInfo = event.getEtiHint();
    event.setEtiHint(etiInfo);
    String correlationKey = event.getKey();
    event.setKey("Modified by EPI: " + correlationKey);
    MatchInfo matchInfo = createSampleMatchInfo();
    event.setMatchInfo(matchInfo);
    event.setNoDedup(true);
    ResolutionHints hints = createSampleResolutionHints();
    event.setNodeHints(hints);
    String object = event.getObject();
    event.setObject("Modified by EPI: " + object);
    String omServiceId = event.getOmServiceId();
    event.setOmServiceId(omServiceId);
    String omUser = event.getOmUser();
    event.setOmUser(omUser);
    String originalText = event.getOriginalData();
    event.setOriginalData("Modified by EPI: " + originalText);
    String originalId = event.getOriginalId();
    event.setOriginalId(originalId);
    event.setSeverity(Severity.MINOR);
    String solution = event.getSolution();
    event.setSolution("Modified by ostap: " + solution);
    ResolutionHints sourceCiHints = createSampleResolutionHints();
    event.setSourceCiHints(sourceCiHints);
    event.setState(LifecycleState.IN_PROGRESS);
    String subCategory = event.getSubCategory();
    event.setSubCategory("Modified by EPI: " + subCategory);
    event.setTimeReceived(new Date());
    String title = event.getTitle();
    event.setTitle("Modified by EPI: " + title);
    String type = event.getType();
    event.setType("Modified by EPI: " + type);
    }
  def ResolutionHints createSampleResolutionHints()
  {
    ResolutionHints hints = new ResolutionHints(false);
    hints.setCoreId("CoreId");
    hints.setDnsName("mydqdn.com");
    hints.setHint("My Hint");
    hints.setIpAddress("0.0.0.0");
    return hints;
  }
  def MatchInfo createSampleMatchInfo()
  {
    MatchInfo matchInfo = new MatchInfo(false);
    matchInfo.setConditionId("conditionId");
    matchInfo.setPolicyName("policyName");
    matchInfo.setPolicyType(PolicyType.CONSOLE);
    return matchInfo;
  }
}

我想修改这个例子。

我想获得"描述"并设置(放置)这个"描述"到外部命令(sd_event.exe)

cmd.exe /c C:\test\sd_event.exe -f C:\test\sd_event.ini -v event_id=50 description=____  status=Registered priority=low".execute().text

我也试着运行:

cmd.exe /c C:\test\mybatch.bat".execute().text  

但是我不知道如何把参数放到bat文件

mybatch.bat:

C:testsd_event.exe -f sd_event.ini -v event_id=5 description=%1 status="Registered" priority=low

由于代码中的格式和错误,很难看到发生了什么,或者您在问什么…

当你有:

String description = event.getDescription();

event是什么?我只能在该函数中看到事件的List

假设这只是一个剪切和粘贴的问题,我认为你正在寻找的答案是:

"cmd.exe /c C:testsd_event.exe -f C:testsd_event.ini -v event_id=50 description='$description' status=Registered priority=low".execute().text

但正如我所说,我不确定…您必须自己编写该函数的输出到result.txt,因为当从Java/Groovy

调用shell时,无法以这种方式进行重定向。

最新更新