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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript If computername = ..............

Status
Not open for further replies.

shuanpicto

Programmer
May 27, 2005
14
0
0
GB
I am creating a script to import and export user settings into mandatory profiles at logon and logoff.

The problem occurs when a user logs onto a citrix server, it runs the script, and messes up the settings.

I need to capture the computer name, set the citrix server in a variable, and do an If statement that only runs the required code, if the user IS NOT logging onto the citrix server.
This is what I have

Set wshshell = CreateObject("WScript.Shell")
Set ObjEnv = WshShell.Environment("Process")
COMPUTERNAME = ObjEnv("COMPUTERNAME")
citrixserver = "citrixserver"

If COMPUTERNAME="citrixserver" Then

' Do Nothing

Else


' Do this
wshshell.Run "REGEDIT /s " & Chr(34) & APPDATA & "\Temp\Wallpaper.reg" & Chr(34), 1, Wait

End IF

Any help would be greatly appreciated.
 
your code looks ok to me?
what else do you want to do?
do your citrix server names have a convention? i.e. do they start with CIT_?
Then you can have stuff like

If LCase(Left(WshShell.ExpandEnvironmentStrings("%computername%"), 4)) = "cit_" Then

Else
'do something?
End If

or, do your citrix servers have an environment variable specific to citrix servers? you could check for that?

or does your enterprise have registry keys for machine specific information...i.e.

strX = WshShell.RegRead("HKLM\Software\<CompanyName>\DeviceInfo\DeviceType")
If strX = "citrixserver" Then

Else

End If

or do citrix servers have a unique file/registry key present that sets them apart from all other machines???

perhaps the machine name is not the best check? if you dont have a naming standard then you ahve to maintain a list of citrix machine names somewhere, ...perhaps in a ini file located on the NETLOGON share? then you read this file into memory and check against it....i would say this would slow the logon process down. user coming over dialup has to load a file across a slow connection in order to determine stuff.

anyway just some thoughts on the matter.

i had a similar issue when i wrote logonscripts from my last place. i was lucky enough that our citrix team had specific registry keys which they maintained so it was a simple case of reading one of them...
 
sorry yo uneed to watch for LCASE or uppercase when comparing strings

SO,

If Trim(LCase(COMPUTERNAME)) = "citrixserver" Then

and your WshShell.Run needs to have 0, True at the end rather than 1, Wait (i presume you dont want users to see black box popping upo, if one does of course, plus the wait isnt right

wshshell.Run "REGEDIT /s " & Chr(34) & APPDATA & "\Temp\Wallpaper.reg" & Chr(34), 0, True

i am not sure if you set APPDATA anywhere so you might be better off with

wshshell.Run "REGEDIT /s %appdata%\Temp\Wallpaper.reg" & Chr(34), 0, True

as WshShell.Run will auto convert environment strings for you.
i do like you use of Chr(34) is it very prudent when passing any command line params,,,especially if they contain white space :)
 
HI, I am a VERY newbie here and never write any VB. my programming experience is almost non existent, but I have a few questions I hope you can help me with.

I run a script (leftover from previous employee) that creates a startmenu with specific icons according to Ad group membership (we publish a desktop). This works great on the citrix servers, however some users login to citrix as well as to their local pc's.

If I run this on their local pc's it locks them down and gives them their citrix startmenu, which is not great as they need to run some apps locally.

What I want to do is check the computername

If computer is not citrix then run blah blah blah
else
continue as normal.

What I have gleemed from this post so far is:
Oh: and being new I comment most things as I have only done a bit of java a while back so beware I may use terminology incorrectly.

' creates an environment object called ObjEnv
Set ObjEnv=wshshell.Environment("PROCESS")
' and checks and sets the VB COMPUTERNAME variable from
' the environment variable compputername.
COMPUTERNAME = ObjEnv("COMPUTERNAME")

' does this make the whole computername lowercase and
' then compare the lowercase name?
if Trim(LCase(COMPUTERNAME)) = "citrixserver"


end if

As we use a std naming convention for ctx servers of
ctx1-xxx
ctx2-xxx
and
XXXXXXXctxX

etc
What I would like to do is look for ctx in the string variable computername.

I have looked at
InStr([start, ]string1, string2[, compare])

so I think I should be doing something like this.

' I am not sure if I should "0" the 0 as it seems to be a
' numerical comparison.
if InStr((Trim(LCase(COMPUTERNAME)), ctx,1) = 0

then
blah blah load local start menu
exit
else
bleh bleh load ctx start menu....


end if

But I am now quite confused.

Any help would be appreciated.

Thanks in advance
Oz

 
you are on the right lines...just be careful that a legit pc doesnt contain ctx in its name.

i havent got my old logonscript to hand but i am sure i used something more specific that computername or at least a combination.

there must be something more specific about a citrix server...something like a registry key??

str2Check = WshShell.RegRead("HKLM\Software\Citrix\Version")

or perhaps you can chuck in an OS check as well
 
Hi Mr Movie,

This is what I got with the help of another colleague and yourself and this posting.

Set ObjEnv=wshshell.Environment("PROCESS")
' COULD YOU EXPLAIN WHY THIS IS NOT "Dim Computername"
COMPUTERNAME = ObjEnv("COMPUTERNAME")

'cast COMPUTERNAME to a STRING to compare it
' our naming convention is quite rigid and I am 99%
' sure CTX wont be in another computer's name but will
' check for a unique reg key for ctx servers.
if InStr(CStr(Trim(LCase(COMPUTERNAME))),"ctx") = 0 Then
' create LOCAL START MENU
' MsgBox to test selection
MsgBox("This is a local startmenu")

else

' build a citrix startmenu
blah blah
blah blah

end if

regards

Oz
PS: I hope this helps some other newbies
 
Hi Oz, i hope you dont mind a few style pointers?

+ to start with i would recommend hungarian notification. (i know it is not as well thought as it used to be but it is a great place to start)
+ I would recommend Dim'n your variables before you use them
+ i would recommend making good use of capitals...like If Else End, rather than if else end
+ make use of tabs
+ defensive programming when you can, the InStr statemnet will cause an error if the strComputerName is blank...i think ;-)
+ drag out your 'static' info, perhaps i should have made a CONST but i am not perfect

Dim objEnv, strComputerName, str2Search4

Set objEnv=wshshell.Environment("PROCESS")
strComputerName = objEnv("COMPUTERNAME")
str2Search4 = "ctx"
If strComputerName <> "" Then
If InStr(CStr(Trim(LCase(strComputerName))), str2Search4) = 0 Then
'do something
Else
'do something different
End If
End If
 
bugger

+ destroy your objects when you are finished with them

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
'do something
Else
'do something different
End If
End If
Set objEnv = Nothing
Set WshShell = Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top