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

Groovy-如何设置参数外部命令,groovy,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.

我有一个软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;
  }

}
我还尝试运行:

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

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

如果您有:

String description = event.getDescription();
什么是
事件
?我只能在该函数中看到事件的
列表

假设这只是剪切和粘贴的问题,我认为您想要的答案是:

"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时,您无法以这种方式重定向

我尝试过清理代码,祈祷我没有做任何更改。PS:您不需要在Groovy中导入java.util.List,我重新表述了我的问题,您现在明白我的意思了吗?@ostapv您尝试了我建议的行吗?我修改了脚本并尝试了您的答案,但没有结果((@ostapv它做什么?什么都没有?崩溃?打印一个异常?如果是的话,异常是什么?它只是挂起了吗?你得帮忙,因为我看不到你的屏幕,也没有你的设置…我找到答案:description=${event.description}谢谢你,tim_yates
"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