I'm trying to convert a MSDN routine from VBScript to JScript. I've
got everything but the last little bit. Of course like most things
without that last bit it is useless. The function gets SNMP data from
a remote device. I know that for the most part it is working (I
sniffed the network) but I haven't figured out the file translation to
get usable data.

The problem is the for loop. colSystem is an ActiveXObject and which
is apparently a container for another ActiveXObject. In VBScript the
For Each loop assigns each of the embedded objects to objSystem. My
trouble is that I have not been able to figure out the equivelent
functionality in JScript. Yes I did try "for in" but that did not
work.


The original routine in VBScript
(http://www.microsoft.com/technet/treeview/default.asp?url=/technet/ScriptCenter/network/scrnet07.asp):

********************
strTargetSnmpDevice = "192.168.0.1"

Set objWmiLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWmiServices = objWmiLocator.ConnectServer("",
"root\snmp\localhost")

Set objWmiNamedValueSet =
CreateObject("WbemScripting.SWbemNamedValueSet")
objWmiNamedValueSet.Add "AgentAddress", strTargetSnmpDevice
objWmiNamedValueSet.Add "AgentReadCommunityName", "public"

Set colSystem = objWmiServices.InstancesOf("SNMP_RFC1213_MIB_system",
, _
objWmiNamedValueSet)

For Each objSystem In colSystem
WScript.Echo "sysContact: " & objSystem.sysContact & vbCrLf & _
"sysDescr: " & objSystem.sysDescr & vbCrLf & _
"sysLocation: " & objSystem.sysLocation & vbCrLf & _
"sysName: " & objSystem.sysName & vbCrLf & _
"sysObjectID: " & objSystem.sysObjectID & vbCrLf & _
"sysServices: " & objSystem.sysServices & vbCrLf & _
"sysUpTime: " & objSystem.sysUpTime
Next

********************
My JScript version

var strTargetSnmpDevice = "192.168.0.1";
var connectServer = "winmgmts://./root/snmp/localhost";
var strQuery = "Select * from SNMP_RFC1213_MIB_system";

var wmiLocator = new ActiveXObject("WbemScripting.SWbemLocator");
var objWmiNamedValueSet =
ActiveXObject("WbemScripting.SWbemNamedValueSet");

var objWmiServices = wmiWmiLocator.ConnectServer("",
"root\snmp\localhost")

objWmiNamedValueSet.Add ("AgentAddress", strTargetSnmpDevice);
objWmiNamedValueSet.Add ("AgentReadCommunityName", "public");

var colSystem = new Enumerator
(ActiveXObject(connectServer).ExecQuery(strQuery,null,null,objWmiNamedValueSet));

// Problem Area
for (objSystem in colSystem)
{
WScript.Echo (" sysContact: " + objSystem.sysContact +
"\n sysDescr: " + objSystem.sysDescr +
"\n sysLocation: " + objSystem.sysLocation +
"\n sysName: " + objSystem.sysName +
"\n sysObjectID: " + objSystem.sysObjectID +
"\n sysServices: " + objSystem.sysServices +
"\n sysUpTime: " + objSystem.sysUpTime);
}

********************

Re: Translating VBScript to JScript by Bob

Bob
Wed Dec 24 16:36:48 CST 2003

Jeff Ullmann wrote:
> I'm trying to convert a MSDN routine from VBScript to JScript. I've
> got everything but the last little bit. Of course like most things
> without that last bit it is useless. The function gets SNMP data from
> a remote device. I know that for the most part it is working (I
> sniffed the network) but I haven't figured out the file translation to
> get usable data.
>
> The problem is the for loop. colSystem is an ActiveXObject and which
> is apparently a container for another ActiveXObject. In VBScript the
> For Each loop assigns each of the embedded objects to objSystem. My
> trouble is that I have not been able to figure out the equivelent
> functionality in JScript. Yes I did try "for in" but that did not
> work.
>
Use an Enumerator object. Here's an example:
http://msdn.microsoft.com/library/en-us/script56/html/js56jsobjEnumerator.asp

Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"



Re: Translating VBScript to JScript by Stewart

Stewart
Fri Jan 02 14:48:23 CST 2004

I am attempting to translate this script for the script repository...
Set objUser = GetObject _
("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com")

WScript.Echo "Distinguished Name: " & VbCrLf & _
objUser.Get("distinguishedName")

objMemberOf = objUser.GetEx("MemberOf")
WScript.Echo VbCrLf & "The user is a member of:"
For Each Group in objMemberOf
Wscript.echo Group
NextI am able to acquire the objUser in Jscript, but am not able to
enumerate the memberof list...objUser.distinguishedName will return
properly... but,var grps = new Enumerator(objUser.GetEx('memberOf')); does
not work... Anyone have a solution?"Bob Barrows" <reb01501@NOyahoo.SPAMcom>
wrote in message news:enSom5myDHA.1660@TK2MSFTNGP09.phx.gbl...
> Jeff Ullmann wrote:
> > I'm trying to convert a MSDN routine from VBScript to JScript. I've
> > got everything but the last little bit. Of course like most things
> > without that last bit it is useless. The function gets SNMP data from
> > a remote device. I know that for the most part it is working (I
> > sniffed the network) but I haven't figured out the file translation to
> > get usable data.
> >
> > The problem is the for loop. colSystem is an ActiveXObject and which
> > is apparently a container for another ActiveXObject. In VBScript the
> > For Each loop assigns each of the embedded objects to objSystem. My
> > trouble is that I have not been able to figure out the equivelent
> > functionality in JScript. Yes I did try "for in" but that did not
> > work.
> >
> Use an Enumerator object. Here's an example:
>
http://msdn.microsoft.com/library/en-us/script56/html/js56jsobjEnumerator.asp
>
> Bob Barrows
>
> --
> Microsoft MVP - ASP/ASP.NET
> Please reply to the newsgroup. This email account is my spam trap so I
> don't check it very often. If you must reply off-line, then remove the
> "NO SPAM"
>
>



Re: Translating VBScript to JScript by Michael

Michael
Fri Jan 02 18:01:04 CST 2004

Stewart Basterash wrote:
> I am attempting to translate this script for the script repository...
> Set objUser = GetObject _
> ("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com")
>
> WScript.Echo "Distinguished Name: " & VbCrLf & _
> objUser.Get("distinguishedName")
>
> objMemberOf = objUser.GetEx("MemberOf")
> WScript.Echo VbCrLf & "The user is a member of:"
> For Each Group in objMemberOf
> Wscript.echo Group
> NextI am able to acquire the objUser in Jscript, but am not able to
> enumerate the memberof list...objUser.distinguishedName will return
> properly... but,var grps = new Enumerator(objUser.GetEx('memberOf'));
> does not work... Anyone have a solution?...


memberOf is returned as a COM SAFEARRAY (i.e., a VB/VBA/VBScript style
array). Use the VBArray() object to convert it to a JScript array.


--
Michael Harris
Microsoft.MVP.Scripting

Windows 2000 Scripting Guide
Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp

TechNet Script Center Sample Scripts
http://www.microsoft.com/downloads/release.asp?ReleaseID=38942

WSH 5.6 documentation download
http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en


Re: Translating VBScript to JScript by Stewart

Stewart
Fri Jan 02 20:07:58 CST 2004

As usual Mr. Harris is right on the money... Here is the code for anyone
else looking...

Thanks Michael

// My Global Junk

var ads = new ActiveXObject("ADSystemInfo");
var sho = new ActiveXObject("WScript.Shell");

// My function

function currentUserMemberOf(group)
{
try {
var objUser = GetObject("LDAP://" + ads.UserName);
var a = VBArray(objUser.GetEx('memberOf')).toArray();
for(var i=0; i<a.length; i++)
{
var g = GetObject('LDAP://' + a[i]);
if (g.Get('samAccountName').toLowerCase() == group.toLowerCase())
return true;
}
return false;
}
catch (e)
{
sho.Popup(e + ' #' + e.number & 0xFFFF + ' desc: ' + e.description);
return false;
}
}






"Michael Harris (MVP) >" <<mikhar at="mvps.org" /> wrote in message
news:emoWayY0DHA.1724@TK2MSFTNGP10.phx.gbl...
> Stewart Basterash wrote:
> > I am attempting to translate this script for the script repository...
> > Set objUser = GetObject _
> > ("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com")
> >
> > WScript.Echo "Distinguished Name: " & VbCrLf & _
> > objUser.Get("distinguishedName")
> >
> > objMemberOf = objUser.GetEx("MemberOf")
> > WScript.Echo VbCrLf & "The user is a member of:"
> > For Each Group in objMemberOf
> > Wscript.echo Group
> > NextI am able to acquire the objUser in Jscript, but am not able to
> > enumerate the memberof list...objUser.distinguishedName will return
> > properly... but,var grps = new Enumerator(objUser.GetEx('memberOf'));
> > does not work... Anyone have a solution?...
>
>
> memberOf is returned as a COM SAFEARRAY (i.e., a VB/VBA/VBScript style
> array). Use the VBArray() object to convert it to a JScript array.
>
>
> --
> Michael Harris
> Microsoft.MVP.Scripting
>
> Windows 2000 Scripting Guide
> Microsoft® Windows®2000 Scripting Guide
> http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp
>
> TechNet Script Center Sample Scripts
> http://www.microsoft.com/downloads/release.asp?ReleaseID=38942
>
> WSH 5.6 documentation download
>
http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en
>