These days I wrote a VCO workflow to create port mirror session automatically and then execute powershell script to drive wireshark capture network packages.
After capturing action, I want to delete the port mirror session created before. However, it can not work.
My workflow script can be seen as following:
// ------- CreateSession -------
var spec = new VcVMwareDVSConfigSpec();
spec.configVersion = DVS.config.configVersion;
spec.vspanConfigSpec=[new VcVMwareDVSVspanConfigSpec()];
spec.vspanConfigSpec[0].vspanSession=new VcVMwareVspanSession();
spec.vspanConfigSpec[0].vspanSession.name=sessionName;
//Span settings
spec.vspanConfigSpec[0].vspanSession.stripOriginalVlan=false;
spec.vspanConfigSpec[0].vspanSession.enabled=true;
spec.vspanConfigSpec[0].vspanSession.normalTrafficAllowed=true;
//Configure destination Port
spec.vspanConfigSpec[0].vspanSession.destinationPort= new VcVMwareVspanPort();
spec.vspanConfigSpec[0].vspanSession.destinationPort.portKey=DPort;
//Configure source Port
spec.vspanConfigSpec[0].vspanSession.sourcePortTransmitted =new VcVMwareVspanPort();
spec.vspanConfigSpec[0].vspanSession.sourcePortTransmitted.portKey=SPort;
spec.vspanConfigSpec[0].vspanSession.sourcePortReceived =new VcVMwareVspanPort();
spec.vspanConfigSpec[0].vspanSession.sourcePortReceived.portKey=SPort;
spec.vspanConfigSpec[0].operation="add";
DVS.reconfigureDvs_Task(spec); //without the operation, it can not add span session
CapPackage = true; //ready to capture package
//--------Execute script to drive Wireshark to capture packages on Powershell host--------
var output;
var session;
pathtoPowershell = "C:\\SPAN.ps1" //Note:use"\\" to escape
if(CapPackage){
try {
session = powershellhost.openSession();
var script = '& "' + pathtoPowershell + '" ' + time;
//output = System.getModule("com.vmware.library.powershell").invokeScript(powershellhost,script,session.getSessionId()) ;
output = powershellhost.invokeScript(script);
} finally {
if (session){
powershellhost.closeSession(session.getSessionId());
}
}
DelSpan = true; //ready to delete Spansession
}
If(DelSpan)
spec.vspanConfigSpec[0].operation="remove"; //remove the session
DVS.reconfigureDvs_Task(spec);
The "remove" operation can not work. The session created by "add" operation can not be deleted.
By the way, if I execute script like this:
spec.vspanConfigSpec[0].operation="add";
spec.vspanConfigSpec[0].operation="remove";
DVS.reconfigureDvs_Task(spec);
it can delete the session, actually it have not added the session. So, it indicate that we must excute
DVS.reconfigureDvs_Task(spec);
to make the "add" operation effective.
Attachments:
1: the session cereted seen from vSphere client.
2:error seen from vSphere web client task log.