Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript Help: If Computername = ctx

Status
Not open for further replies.

Belphegory

IS-IT--Management
Dec 13, 2010
7
ZA
Hi there.

I am a network guy so this programming stuff is way over my poor head and need to ask the professionals. :)

I have vbs scripts that run in a GPO under a certain OU per department etc etc and now that we are moving to citrix, I need the normal domain login scripts to run if computername is NOT ctx...

Anyhoo, my issue is that its now working too well...nothing runs\happens on either citrix sessions or normal login.

The only thing I have changed/fiddled with is under Main Process.
It used to be only:

GrpMeb UserName
ShowBox
Wscript.Quit

I have cobbled together whats in there now from browsing this great forum, but alas its still not working.

Any help would be muchly appreciated.

Here is the whole sordid thing:

'**********************************************************************************
' Set Environment Variables
'*********************************************************************************
Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set WSHShell = WScript.CreateObject("WScript.Shell")

On Error Resume Next

Domain = WSHNetwork.UserDomain
UserName = ""

While UserName = ""
UserName = WSHNetwork.UserName
MyGroups = GetGroups(Domain, UserName)
Wend

'*********************************************************************************
' Main Process:
'*********************************************************************************

Dim WshShell, objEnv, strComputerName, str2Search4

Set WshShell = CreateObject("Wscript.Shell")
Set objEnv = WshShell.Environment("PROCESS")
strComputerName = objEnv("COMPUTERNAME")
str2Search4 = "ctx"
If strComputerName <> "" Then
If InStr(CStr(Trim(LCase(strComputerName))), str2Search4) = 0 Then

GrpMeb UserName
ShowBox
End If
End If

Set objEnv = Nothing
Set WshShell = Nothing
Wscript.Quit

'*********************************************************************************
'Function: GetGroups
'*********************************************************************************
Function GetGroups(Domain, UserName)
Set objUser = GetObject("WinNT://" & Domain & "/" & UserName)
GetGroups=""
For Each objGroup In objUser.Groups
GetGroups=GetGroups & "[" & UCase(objGroup.Name) & "]"
Next
End Function

'********************************************************************************
'Function: InGroup
'********************************************************************************
Function InGroup(strGroup)
InGroup=False
If InStr(MyGroups,"[" & UCase(strGroup) & "]") Then
InGroup=True
End If
End Function

'*********************************************************************************
' MapDrives Subroutine
'*********************************************************************************
Sub MapDrive(sDrive,sShare)
On Error Resume Next
WSHNetwork.RemoveNetworkDrive sDrive, 1, 1
wscript.sleep 1000
Err.Clear
WSHNetwork.MapNetworkDrive sDrive,sShare
End Sub

'********************************************************************************
'Map Drives:
'********************************************************************************
Sub GrpMeb(UNAME)
MapDrive "G:", "\\jhb-DC\General$"
MapDrive "T:", "\\jhb-DC\Inbound"
Return = WSHShell.Run("\\jhb-DC\ofcscan\autopcc.exe", 5, true)
Return = WSHShell.Run("\\jhb-DC\AN\ina32.exe", 5, true)
strCmd = "net.exe time \\jhb-DC /set /yes"
WshShell.Run strCmd, 0
End Sub
'
'********************************************************************************
'Display Dialog:
'********************************************************************************
Sub ShowBox
strMsgtxt = "You are mapped to the following drive letters:" & vbNewLine
strMsgtxt = strMsgtxt & "G: = \\jhb-DC\General$" & vbNewLine
strMsgtxt = strMsgtxt & "T: = \\jhb-DC\Inbound" & vbNewLine
msgbox = WshShell.Popup(strMsgtxt, 2, "Logon Script", 0)
End Sub
'********************************************************************************


 
how about trying something like (see below)
i find an If statement without an Else a little vain.

i recall having to do something like you are trying many years ago, the machine name approach could be alittle hit and miss, i seem to recall using an environment variable which was only applicable to a terminal server session, or perhaps the value of an environment variable being something specific to a terminal server session. I would advise using the SET command at the cmd.exe prompt on a terminal server session and compare this against a SET command on a real session. sorry i cant dig out the test i was doing previously

str2Search4 = "ctx"
Wscript.Echo strComputerName
If strComputerName <> "" Then
Wscript.Echo "machine name not blank"
Wscript.Echo "will try and search for " & str2Search4
Wscript.Echo InStr(CStr(Trim(LCase(strComputerName))),str2Search4)
If InStr(CStr(Trim(LCase(strComputerName))),str2Search4) = 0 Then
Wscript.Echo "found str2Search4 in name"
GrpMeb UserName
ShowBox
Else
Wscript.Echo "didnt find str2Search4 in name"
End If
Else
Wscript.Echo "machine is blank!!!"
End If


I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Hi there.

Wow thanks for the prompt response!

I tried inputting what you had...removing everything under Main Process, but it didnt work:

Dim WshShell, objEnv, strComputerName, str2Search4

Set WshShell = CreateObject("Wscript.Shell")
Set objEnv = WshShell.Environment("PROCESS")
strComputerName = objEnv("COMPUTERNAME")
str2Search4 = "ctx"
Wscript.Echo strComputerName
If strComputerName <> "" Then
Wscript.Echo "machine name not blank"
Wscript.Echo "will try and search for " & str2Search4
Wscript.Echo InStr(CStr(Trim(LCase(strComputerName))),str2Search4)
If InStr(CStr(Trim(LCase(strComputerName))),str2Search4) = 0 Then
Wscript.Echo "found str2Search4 in name"
GrpMeb UserName
ShowBox
Else
Wscript.Echo "didnt find str2Search4 in name"
End If
Else
Wscript.Echo "machine is blank!!!"
End If
 
If InStr(CStr(Trim(LCase(strComputerName))),str2Search4) [!]>[/!] 0 Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Ooops sorry, also, forgot to ask what about the SET command?
The only thing I could pick up different from terminal server session to the actual session is this:
CLIENTNAME=PC103 (Which the actual login doesnt have)

and

SESSIONNAME=RDP-Tcp#0


Regards

 
PHVs comment might be your issue though.
You say that 'it doesnt work'. If you implemented all the wscript.echo's what are you getting on the screen? this should indicate what the value of your InStr() is etc.

if SESSIONNNAME is only there for a terminal server session then something like this might be more robust?

If WshShell.ExpandEnvironmentStrings("%SESSIONNAME%") <> "%SESSIONAME%" Then
Wscript.Echo "is a terminal services session"
Else
Wscript.Echo "not a terminal services session"
End If
'i think that is right??

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Hmmm very strange...

I changed all the echos and this is the order I got:
Pcname

Machine name is not blank

will try and search for ctx

0

found str2Search4 in name

and then all the scripts ran....


however this only worked when running the .vbs from a network share....when logging in...nothing ran at all...

I r very confused at the mo....oh also...changed that =0 to >0 and it got as far as didnt find str2Search4 in name and then nothing happened.... so changed it back to =0
 
is it your use of:
Set objEnv = WshShell.Environment("PROCESS")
strComputerName = objEnv("COMPUTERNAME")

rather than
Set objEnv = WshShell.Environment("PROCESS")
strComputerName = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%)

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
balls, i meant to write:

is it your use of:
Set objEnv = WshShell.Environment("PROCESS")
strComputerName = objEnv("COMPUTERNAME")

rather than
strComputerName = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%)

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
[0] Try login with
[tt] strComputerName = WshShell.ExpandEnvironmentStrings("%ClientName%")[/tt]
also with plenty of echo to verify what you get along the road. Is there "ctx" particle in the name? or some other signature?

[1] Delete/comment out the "on error resume next" line!
 
I had issues with %CLIENTNAME% not being set at login. It seems my login script was running before %CLIENTNAME% was set. So I changed the GPO from running the login script to “Run these Programs at user logon” located in the Computer Container at <Administrative Templates>-<System>-<Logon>. For some reason it works this way. Also, I'm using a Local GPO( don't ask why, not my idea), this might not be an issue with an OU GPO
 
Yeh Id rather not use clientname as its pure AD logins.

The citrix servers are ctx1 and ctx2, thats why I had ctx in the search string...

mrmovie:where do I put in your line?

Dim WshShell, objEnv, strComputerName, str2Search4

Set WshShell = CreateObject("Wscript.Shell")
strComputerName = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%)
str2Search4 = "ctx"
Wscript.Echo strComputerName
If strComputerName <> "" Then
Wscript.Echo "machine name not blank"
Wscript.Echo "will try and search for " & str2Search4
Wscript.Echo InStr(CStr(Trim(LCase(strComputerName))),str2Search4)
If InStr(CStr(Trim(LCase(strComputerName))),str2Search4) = 0 Then
Wscript.Echo "found str2Search4 in name"
GrpMeb UserName
ShowBox
Else
Wscript.Echo "didnt find str2Search4 in name"
End If
Else
Wscript.Echo "machine is blank!!!"
End If
 
hi Belphegory,
i think you have the strComputerName = WshShell.ExpandEnvironmentStrings in the right place.
you are still using = 0, rather than PHV' suggestion of > 0 in your If InStr(...) line. I think you should take PHV's advice?
Can you run the last script you have just posted at the command prompt and tell us the outcome on the screen?
Perhaps you should change the wscript.echo to objTS.WriteLine and start writing a log file at logon so you have all the information to hand for debugging

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
It is fine to proceed with computername. I only suggest %clientname% as a trial enlarging a bit testing scope. If that's off, just don't do the test.

And then:
[tt] strComputerName = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%[highlight][red]"[/red][/highlight])[/tt]
though it would coincide with
[tt] wshnetwork.computername[/tt]
Since you've instantiated wshnetwork, I don't see why you don't make good use of it.

Also have you take out the on error resume next?! Error will show up.
 
This is what I currently have and its working when run \ double clicking on it:
Dim WshShell, objEnv, strComputerName, str2Search4

Set WshShell = CreateObject("Wscript.Shell")
Set objEnv = WshShell.Environment("PROCESS")
strComputerName = objEnv("COMPUTERNAME")
str2Search4 = "ctx"
Wscript.Echo strComputerName
If strComputerName <> "" Then
Wscript.Echo "machine name not blank"
Wscript.Echo "will try and search for " & str2Search4
Wscript.Echo InStr(CStr(Trim(LCase(strComputerName))),str2Search4)
If InStr(CStr(Trim(LCase(strComputerName))),str2Search4) = 0 Then
Wscript.Echo "didnt find str2Search4 in name"
GrpMeb UserName
ShowBox
Else
Wscript.Echo "found str2Search4 in name"
End If
Else
Wscript.Echo "machine is blank!!!"
End If

I tried changing the = 0 to > 0 and then the script doesn't work?
Says str2Search4 in name and then nothing runs....

 
i presume you are still saying that this script doesnt work under the context in which you are trying to run it? group policy machine startup script or something like that?
so, how can you determine what is going wrong with the script running under that context? = change the wscript.echo to objTS.WriteLine so you create a log file and put the results there....

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Control and log all outcomes of the variable "Result". Same for "net.exe" line as well... I don't see some initiative...
 
My apologies, I missread the original issue. I thought you WANTED to run the login script if it was Citrix
 
Hi there guys.

Sorry for the lack of presence....its all sorted out now.

Working like a bomb... if it picks up citrix server,it doesn't run scripts, and then runs scripts when logged on locally.

Thanks very much you guys...you have saved me soooo much AD work its not even funny.

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top