Jump to content

Unsuccessfull VBS Conversion


Recommended Posts

Morning guys,

I have this vbscript that provides details on the subnet that a pc is logged on to. It obtains this information from AD sites and services. The problem I have is when I change the script extention to AU3 and make the relevant formatting changes the information comes back as blank.

Here is the VBScript

Subnet = "SubnetName"
    set rootDse = GetObject( "LDAP://RootDSE" )


    ConfigContainer = rootDse.Get( "configurationNamingContext" )

    set con = createobject("adodb.connection")
    set Com = createobject("adodb.command")

    con.provider="ADsDSOOBJECT"
    con.open "ADs Provider"

    set Com.ActiveConnection = con

    Com.CommandText= "<LDAP://" & ConfigContainer & ">;(&(objectCategory=subnet)(name=" & subnet & "*));name,siteObject,location;subtree"
    set rs = com.execute

    RS.movefirst

    getlocation = rs.fields("location")

    wscript.echo getlocation

This is the AutoIT Code I converted it into.

$Subnet = "SubnetName"

    $rootDse = ObjGet( "LDAP://RootDSE" )

    $ConfigContainer = $rootDse.Get( "configurationNamingContext" )

    $Conn = objcreate("adodb.connection")
    $Com = objcreate("adodb.command")

    $Conn.provider="ADsDSOObject"
    $Conn.open("ADs Provider")

    $Com.ActiveConnection = $Conn

    $Com.CommandText = "<LDAP://" & $ConfigContainer & ">;(&(name=" & $subnet & "));name,siteObject,location;subtree"

    $rs = $Com.execute

    $rs.movefirst


MsgBox(0,"",$rs.fields("location"))

Thank you in advance for any assistance provided.

Link to comment
Share on other sites

DemonAngel,

I added a check after objget and it looks like you are not getting the object that you expect.

$rootDse = ObjGet( "LDAP://RootDSE" )
if $rootdse = 0 then msgbox(0,'','Error from objget RootDSE = ' & @error)

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Hi Kylomas,

I ran the code mentioned above, however no errors were generated.

Furthermore I used a msgbox on the first instance of $ConfigContainer and it returned the expected value.

CN=Configuration,DC=XXXX,DC=yyy

So I think the initial objget is working as extected.

Link to comment
Share on other sites

Thank you for trying Kulomas.

The original vbs will only run if you have AD Subnets configured and change the

Subnet = "SubnetName"

Section to reflect the name of your configured subnet.

I have searched the entire AutoIT form and it doesnt seem like anyone has been working with obtaining subnet information from AD yet.

Link to comment
Share on other sites

@DemonAngel

I hope this get's you going.

#include <Array.au3>

; List all Sites
Dim $avArray[1]

$objRootDSE = ObjGet("LDAP://RootDSE")
$strConfigurationNC = $objRootDSE.Get("configurationNamingContext")

$strSitesContainer = "LDAP://cn=Sites," & $strConfigurationNC
$objSitesContainer = ObjGet($strSitesContainer)

$objSitesContainer.Filter = _ArrayAdd($avArray, "site") ; ArrayCreate

For $objSite In $objSitesContainer
    ConsoleWrite("Name: " & $objSite.Name & @CRLF)
Next

; List all Subnets on AD Sites
Dim $avArray1[1]
Dim $avArray2[1]

$objRootDSE = ObjGet("LDAP://RootDSE")
$strConfigurationNC = $objRootDSE.Get("configurationNamingContext")

$strSubnetsContainer = "LDAP://cn=Subnets,cn=Sites," & $strConfigurationNC
    ConsoleWrite($strSubnetsContainer & @CRLF)
$objSubnetsContainer = ObjGet($strSubnetsContainer)

$objSubnetsContainer.Filter = _ArrayAdd($avArray1, "subnet")

$objHash = ObjCreate("Scripting.Dictionary")

For $objSubnet In $objSubnetsContainer
    $objSubnet.GetInfoEx (_ArrayAdd($avArray2, "siteObject"), 0)
    $strSiteObjectDN = $objSubnet.Get("siteObject")

    $strSiteObjectName = StringSplit(StringSplit($strSiteObjectDN, ",",0), "=",1)

    If $objHash.Exists($strSiteObjectName) Then
        $objHash($strSiteObjectName) = $objHash($strSiteObjectName) & "," & _
            StringSplit($objSubnet.Name, "=",1)
    Else
        $objHash.Add ($strSiteObjectName, StringSplit($objSubnet.Name, "=",1))
    EndIf
Next

For $strKey In $objHash.Keys
    ConsoleWrite($strKey & "," & $objHash($strKey) & @CRLF)
Next

But I can't test it because I don't have subnets configured.

rgds

ptrex

Edited by ptrex
Link to comment
Share on other sites

Hi ptrex

The first section of the code provided above executed and returned the expected information.

The List all subnets on AD sites however returns the following error message.

\TEST\Subnet3.au3 (36) : ==> The requested action with this object has failed.:

$objSubnet.GetInfoEx (_ArrayAdd($avArray2, "siteObject"), 0)

$objSubnet.GetInfoEx (_ArrayAdd($avArray2, "siteObject"), 0)^ ERROR

->14:45:19 AutoIT3.exe ended.rc:1

>Exit code: 1 Time: 2.759

Thank you for your time so far, we are definitely getting closer. :x

Link to comment
Share on other sites

@ptrex + anyone that would require this code.

I modified your second example a bit and its doing exactly what I need now. Thank you for your assistance.

Dim $avArray1[1]

$Subnet = "SubnetNameGoesHere"

$objRootDSE = ObjGet("LDAP://RootDSE")
$strConfigurationNC = $objRootDSE.Get("configurationNamingContext")

$strSubnetsContainer = "LDAP://cn=Subnets,cn=Sites," & $strConfigurationNC
$objSubnetsContainer = ObjGet($strSubnetsContainer)

$objSubnetsContainer.Filter = _ArrayAdd($avArray1, "subnet")

For $objSubnet In $objSubnetsContainer
    If StringInStr($objSubnet.cn,$Subnet) Then
        $objSubnetInfo = ObjGet($objSubnet.adspath)
        MsgBox(0,"",$objSubnetInfo.Location)
        ExitLoop
    EndIf
Next
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...