VBScript Contribution

Started by rmpf2

rmpf2

VBScript Contribution   29 November 2016, 14:39

Hi Andrew.

I want to thank you for the opportunity to contribute and be part of this great project. It's been a while since the scanner was in its beginning and it was not even half as great as it is today. In a certain way, we, netscan users, have been at your side, supporting you and enjoying your success. Everyone can navigate the forum and read every comment, suggestion, or request answered and well developed by you to understand what I mean.

1) Doing some test on netscan new feature i found an strange behavior in the copy (ctrl+C) command.

As for the next Example (* 2) ) the new item name was correct after pasting it but the contents was about just one line (strComputer = input.current). I don't know is that what you want but i think maybe is better to have the new item exactically to its source.

*2) The following script has been tested so you can share it:


Is to display User Full Name (displayName)

strComputer = input.current

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
  ("Select * from Win32_Process Where Name = 'explorer.exe'")

If colItems.Count = 0 Then
  output.write "No one is logged on to the computer."
Else
  For Each objProcess in colItems
    objProcess.GetOwner strUser,strDomain
  Next
End If

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

objCommand.CommandText = "SELECT displayName FROM " & _
  "'LDAP://DC=domain_name,DC=local' WHERE " & _
    "objectCategory='user' " & _
    "AND samAccountName = '" & strUser & "'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
  output.write objRecordSet.Fields("displayName").Value
  objRecordSet.MoveNext
Loop
SoftPerfect Support forum - Andrew avatar image

Re: VBScript Contribution   29 November 2016, 22:39

Thank you for your feedback and kind words wink

We have fixed the issue with copying scripts - it didn't handle multiline items. We have also added your script as an example. It will appear in the default config. You can download the fixed version here.
rmpf2

Re: VBScript Contribution   02 December 2016, 12:23

Hi, just to share!!!

1) The script (displayName) need some modification to work
DC=domain_name,DC=local should be replaced with corresponding Active Directory base values.

2) The script can be modified to find other attributes like mail and distinguishedName.

3) For distinguishedName column I give you the following regex:
(?:^.*?)(?=,OU=(.+?),)

Example:

Original value obtainded by script:
CN=Rick Lee,OU=Budget Office,OU=President Office,OU=Central Goverment,OU=USA_CG,DC=WhiteHouse,DC=local

Value obtainded after applied column regex:
Budget Office

Great to know our users office or location base on AD.

So running from Netscan the script modified 3 times you can obtain something like this:
rlee rlee@whitehouse.local Budget Office
rmpf2

Re: VBScript Contribution   02 December 2016, 12:28

Sorry, correction to example
displayName mail distinguishedName
Rick Lee rlee@whitehouse.local Budget Office
rmpf2

Re: VBScript Contribution   02 December 2016, 21:27

Now deploy and sort by office is possible.
What I mean is that now sorting and filtering by office or department is a dream come true. And deploy software just to those host too. Thanks.
rmpf2

Re: VBScript Contribution   08 December 2016, 06:34

Hi, just to share!!!

1) Computer OU (script)
Dim strComputerName

'Call Ip2Hostname function to resolve Ip 
Ip2Hostname
 
 
Dim wshShell, wshNetwork
' Create Global Objects
Set wshShell = CreateObject("WScript.Shell")
Set wshNetwork = CreateObject("WScript.Network")
     
output.write "Computer DN: " & GetDN
  
 
Function Ip2Hostname()
 
  ' Use to resolve ip to hostname 
  
  Dim ipAddress,hostname
  ipAddress = input.current

  On Error Resume Next
  Set objWMIService = GetObject("winmgmts:\\" & ipAddress & "\root\cimv2")
   If Err.Number <> 0 Then
    hostname = "Cannot resolve hostname"
    Err.Clear
   Else
    Set colItems = objWMIService.ExecQuery("Select * FROM Win32_ComputerSystem", "WQL", _
     wbemFlagReturnImmediately + wbemFlagForwardOnly)
    For Each objItem In colItems
     hostname = objItem.Name
    Next
   End If

  strComputerName = hostname
 
End Function
  
 
Function GetDN()
 ' Use the NameTranslate object to convert the NT name of the computer to
 ' the Distinguished name required for the LDAP provider. Computer names
 ' must end with "$". Returns comma delimited string to calling code.
 Dim objTrans, objDomain
 ' Constants for the NameTranslate object.
 Const ADS_NAME_INITTYPE_GC = 3
 Const ADS_NAME_TYPE_NT4 = 3
 Const ADS_NAME_TYPE_1779 = 1
 Set objTrans = CreateObject("NameTranslate")
 Set objDomain = getObject("LDAP://rootDse")
 objTrans.Init ADS_NAME_INITTYPE_GC, ""
 objTrans.Set ADS_NAME_TYPE_NT4, wshNetwork.UserDomain & "\" _
 & strComputerName & "$"
 GetDN = objTrans.Get(ADS_NAME_TYPE_1779)
 'Set DN to upper Case
 GetDN = UCase(GetDN)
End Function
Note: Use (?:^.*?)(?=,OU=(.+?),) for the column regex

2) memberOf (script)
Dim usr_adsPath
 
' List the Active Directory Groups a User Belongs To
 
On Error Resume Next
Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D

GetadsPath

Set objUser = GetObject (usr_adsPath)
      
intPrimaryGroupID = objUser.Get("primaryGroupID")
arrMemberOf = objUser.GetEx("memberOf")

If Err.Number = E_ADS_PROPERTY_NOT_FOUND Then
 output.write "The memberOf attribute is not set."
Else
For Each Group in arrMemberOf
 output.write Group
Next
End If

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"<LDAP://dc=sportsnet,dc=local>;(objectCategory=Group);" & _
"distinguishedName,primaryGroupToken;subtree" 
Set objRecordSet = objCommand.Execute

Do Until objRecordset.EOF
If objRecordset.Fields("primaryGroupToken") = intPrimaryGroupID Then
 output.write "Primary group:"
 output.write objRecordset.Fields("distinguishedName") & _
" (primaryGroupID: " & intPrimaryGroupID & ")"
End If
 objRecordset.MoveNext
Loop

objConnection.Close


Function GetadsPath()
strComputer = input.current

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
  ("Select * from Win32_Process Where Name = 'explorer.exe'")

If colItems.Count = 0 Then
  output.write "No one is logged on to the computer."
Else
  For Each objProcess in colItems
    objProcess.GetOwner strUser,strDomain
  Next
End If

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

objCommand.CommandText = "SELECT adsPath FROM " & _
  "'LDAP://DC=sportsnet,DC=local' WHERE " & _
    "objectCategory='user' " & _
    "AND samAccountName = '" & strUser & "'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
  usr_adsPath = objRecordSet.Fields("adsPath").Value
  objRecordSet.MoveNext
Loop
End Function
Note: Use CN=(.+?), for the column regex

Reply to this topic

Sometimes you can find a solution faster if you try the forum search, have a look at the knowledge base, or check the software user manual to see if your question has already been answered.

Our forum rules are simple:

  • Be polite.
  • Do not spam.
  • Write in English. If possible, check your spelling and grammar.

Author:

Email:

Subject

A brief and informative title for your message, approximately 4–8 words:

     

Spam prevention: please enter the following code in the input field below.

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

Message: