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

时间:2011-10-05 09:01:24

标签: 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:\test\sd_event.exe -f sd_event.ini -v event_id=5 description=%1 status="Registered" priority=low

1 个答案:

答案 0 :(得分:1)

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

当你有:

String description = event.getDescription();

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

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

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

但正如我所说的,我不确定......你必须自己将该函数的输出写入result.txt,因为从Java / Groovy调用shell时无法以这种方式进行重定向/ p>