We help IT Professionals succeed at work.

Running login script only if logging in to a specific Termianl Server

cgitst
cgitst asked
on
I am trying to run the logon script below but only if the user is logging into 1 of 4 terminal servers.  I know the script runs but it was running on any computer the user was logging in to.  We had this setup before but we rebuilt the server and lost the script.  I just need an if statement that checks if the server is either server1, server 2, server3, or server 4 else do nothing

Dim objNetwork, objNetworkSet
Dim filesys, newfolder

set objNetwork = CreateObject("WScript.Network")
set filesys = CreateObject("Scripting.FileSystemObject")

If  Not filesys.FolderExists("C:\Users\" & objNetwork.UserName & "\AppData\Local\Microsoft\Outlook\") Then
	Dim wshShell 
	Set wshShell = CreateObject("WScript.Shell") 
	strResult = wshShell.Run("Outlook.exe /importprf \\servershare\public$\it\Outlook.prf")
End If

Open in new window

Comment
Watch Question

CERTIFIED EXPERT

Commented:
I would try like this:
Dim objNetwork, objNetworkSet, wshShell
Dim filesys, newfolder, strComputerName

set objNetwork = CreateObject("WScript.Network")
set filesys = CreateObject("Scripting.FileSystemObject")
Set wshShell = CreateObject("WScript.Shell")
strComputerName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

If LCase(Left(strComputerName, 6)) = "server" Then
	If  Not filesys.FolderExists("C:\Users\" & objNetwork.UserName & "\AppData\Local\Microsoft\Outlook\") Then
        strResult = wshShell.Run("Outlook.exe /importprf \\servershare\public$\it\Outlook.prf")
	End If
End If

Open in new window

Author

Commented:
X layer thanks for the help but it still doesn't seem to work, when I log in as a user to any of the 4 servers listed the outlook profile is not being created.  When I remove the if statement the script runs but it on any computer I log into which is what I need to avoid.  I also dont want it to override the existing profile which is why I check if the file exists.

Dim objNetwork, objNetworkSet, wshShell
Dim filesys, newfolder, strComputerName

set objNetwork = CreateObject("WScript.Network")
set filesys = CreateObject("Scripting.FileSystemObject")
Set wshShell = CreateObject("WScript.Shell")
strComputerName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

If LCase(Left(strComputerName, 6)) = "EdenTS1" OR LCase(Left(strComputerName, 6)) = "EdenTS2" OR LCase(Left(strComputerName, 6)) = "EdenTS3" OR LCase(Left(strComputerName, 6)) = "EdenTS4" Then
	If  Not filesys.FolderExists("C:\Users\" & objNetwork.UserName & "\AppData\Local\Microsoft\Outlook\") Then
'		Dim wshShell 
		Set wshShell = CreateObject("WScript.Shell") 
		strResult = wshShell.Run("Outlook.exe /importprf \\cgnas\public$\it\Outlook2010.prf")
	End If

End If

Open in new window

Author

Commented:
Something I forgot to add, when I remove the if statement it only works when I log in to a client workstation XP or Windows 7 , however when I log in to a 2008 server nothing, the script doesn't run with or without the if statement.
I use this code in my startup script to monitor for server logon:

Set objNetwork = CreateObject("WScript.Network")
If objNetwork.ComputerName = "servername" Then
Else
...
End If

Open in new window


You create add additional conditions to the if statement to account for all your TS servers, or use a Select Case instead.
I believe I found the error in the code you used from X_layer.

The Left() procedure is looking for 6 characters

Left(strComputerName, 6)

Open in new window


Your server names are 7 characters. Adjust your code to account for 7 characters instead:

Dim objNetwork, objNetworkSet, wshShell
Dim filesys, newfolder, strComputerName

set objNetwork = CreateObject("WScript.Network")
set filesys = CreateObject("Scripting.FileSystemObject")
Set wshShell = CreateObject("WScript.Shell")
strComputerName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

If LCase(Left(strComputerName, 7)) = "EdenTS1" OR LCase(Left(strComputerName, 7)) = "EdenTS2" OR LCase(Left(strComputerName, 7)) = "EdenTS3" OR LCase(Left(strComputerName, 7)) = "EdenTS4" Then
	If  Not filesys.FolderExists("C:\Users\" & objNetwork.UserName & "\AppData\Local\Microsoft\Outlook\") Then
'		Dim wshShell 
		Set wshShell = CreateObject("WScript.Shell") 
		strResult = wshShell.Run("Outlook.exe /importprf \\cgnas\public$\it\Outlook2010.prf")
	End If

End If

Open in new window

Author

Commented:
Sorry for the extremely late response...but I have tried the updated script provided above and it does not work anywhere not on the servers or on any of the clients.
CERTIFIED EXPERT

Commented:
Put this before first IF statement and look what you get:
WScript.Echo strComputerName

Open in new window

Try to connect to one of the four servers.

Author

Commented:
I was able to get this working once i added the server names in lower case because we used the LCase command.  Thanks to everyone who offered a solution and assistance.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.