Link to home
Start Free TrialLog in
Avatar of fruitloopy
fruitloopy

asked on

VBScript - Remove characters and spaces from a string

I have used some code I found on the internet to query Active Directory and return the full name of a username.
The issue I have is another application injects the username into the VBS file first. It has the domain name (UK\) and spaces after the username which cause the script to fail.

What I would like is for the script to clean the string before it is run. It seems this is possible but I cant quite get my head around it!

Here's the code:
Option Explicit
Dim strComputer, strUsername, objWMI, colUsers, objUser

strComputer = "."
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\directory\LDAP")


strUsername = "uk\sulliap1   "


Set colUsers = objWMI.ExecQuery("SELECT * FROM ds_user where ds_sAMAccountName = '" & strUsername & "'")
   
If colUsers.Count > 0 Then
   For Each objUser in colUsers
      WScript.Echo "Name: " & objUser.ds_givenName & " " & objUser.ds_sn
   Next
Else
   WScript.Echo "No users found!"
End If

Open in new window


So I need to remove the 3 leading characters from strUsername and any spaces after the username, then continue running the code.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of madhatter550
madhatter550

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fruitloopy
fruitloopy

ASKER

Works perfectly...thanks!