After lot of findings I found the code. I have written it as a function so that I can avoid writing same code multiple times:
The below function will create a new vmk adapter and assign the portgroup and Ip address to it. If you want to add ip to a existing vmk interface then replace:
var newVnic = hostConfigManager.addVirtualNic(portGroup, hostNicSpec);
with
var newVnic = hostConfigManager.updateVirtualNic(portGroup, hostNicSpec);
Hope it helps.
function addvmk(gw, ip, mask, nsikey, dvsid, pgkey, mtu, esxhost)
{
var portGroup = "";
var hostNicSpec = new VcHostVirtualNicSpec();
hostNicSpec.ipRouteSpec = new VcHostVirtualNicIpRouteSpec();
hostNicSpec.ipRouteSpec.ipRouteConfig = new VcHostIpRouteConfig();
hostNicSpec.ipRouteSpec.ipRouteConfig.defaultGateway = gw.trim();
hostNicSpec.ip = new VcHostIpConfig();
hostNicSpec.ip.ipAddress = ip.trim();
hostNicSpec.ip.subnetMask = mask.trim();
hostNicSpec.ip.dhcp = false;
hostNicSpec.netStackInstanceKey = nsikey;
hostNicSpec.distributedVirtualPort = new VcDistributedVirtualSwitchPortConnection();
hostNicSpec.distributedVirtualPort.switchUuid = dvsid
hostNicSpec.distributedVirtualPort.portgroupKey = pgkey
hostNicSpec.mtu = mtu;
var hostConfigManager = esxhost.configManager.networkSystem;
var newVnic = hostConfigManager.addVirtualNic(portGroup, hostNicSpec);
System.sleep(1000)
}