Link to home
Start Free TrialLog in
Avatar of bjv211
bjv211

asked on

Check for remote desktop connection in VBscript

I currently have a logon.vbs that sends users to our intranet on logon.  However, I want to not send them there if they are connecting through remote desktop.  Is there a way to figure out if they are connecting through remote desktop and not do the following script?

MY CURRENT logon.vbs:

Set WshShell = CreateObject("WScript.Shell")
strUserName = WshShell.ExpandEnvironmentStrings("%username%")

IF StrComp(strUserName,"EXEMPTUSER") = 0 Then

ELSE
Set WshShell = WScript.CreateObject("WScript.Shell")
ReturnCode = WshShell.Run("iexplore http://WEBSITE.com", 1, True)
END IF
Avatar of Tony Massa
Tony Massa
Flag of United States of America image

If your servers have a common naming convention, you could check for the computer name and run the intranet page if the computername (or IP Address) <> (does not equal) a specific value or range.

List the IPAddresses for a computer:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.IPAddress) Then
        For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
            WScript.Echo IPConfig.IPAddress(i)
        Next
    End If
Next



SCRIPT TO GET COMPUTER NAME:

Set wshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set objComputer = GetObject("LDAP://" & ADSysInfo.ComputerName)
WScript.Echo objComputer.sAMAccountName

-TM
Avatar of bjv211
bjv211

ASKER

TM, will this work even if the client is remote desktop connection to their individual workstation?
The script, if attached to a user logon, will run, no matter where they log on to.  If they RDP to a Terminal Server, the script will run.  When it's run, the script will show that it's running on the server, not on the users' local workstation.  So if you put in some code that checks the computer name, and run if the computer name does NOT begin with "SRV", then it won't run the intranet page.

If you want an example of code:  This script will find the ComputerName of where the user is logging on to and will only run the intranet kickoff if the computer name does not begin with "SRV" or "SERVER":


Set wshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set objComputer = GetObject("LDAP://" & ADSysInfo.ComputerName)

StrComputerName = objComputer.sAMAccountName

Set WshShell = CreateObject("WScript.Shell")
strUserName = WshShell.ExpandEnvironmentStrings("%username%")

If UCase(Left(StrComputerName, 3)) = "SRV" Or UCase(Left(StrComputerName, 6)) = "SERVER" Then
 'Do Nothing
Else
      ReturnCode = WshShell.Run("iexplore http://WEBSITE.com", 1, True)
End If


-TM
Avatar of bjv211

ASKER

we are not running a 'terminal server' , each workstation is being directly connected to through terminal services, all our machines have external IPs
It doesn't matter...if the user is logging on to any computer via local logon or over RDP, this will work.
Ohhh...I see what you're saying.

Check the SessionName:

Set WshShell = CreateObject("WScript.Shell")
strSessionName = WshShell.ExpandEnvironmentStrings("%SessionName%")

If Ucase(strSessionName) = "CONSOLE" Then
 ' You're logged on locally
Set WshShell = WScript.CreateObject("WScript.Shell")
ReturnCode = WshShell.Run("iexplore http://WEBSITE.com", 1, True)
Else
  'Do Nothing: You're logged in remotely via RDP
End If

-TM
Avatar of bjv211

ASKER

sweet and can you integrate my existing check for username
What are you checking for...a specific user, or group of users?  What do you want to happen for these users?

-TM
Avatar of bjv211

ASKER

heres the logic...

IF RDP,
   do nothing
ELSE
         IF NOT 1 specfic user
             GOTO SITE
           ELSE
              d nothing
         END IF
END IF
Avatar of bjv211

ASKER

i'm not so good at the logic of VBS, but i hope you get the point
ASKER CERTIFIED SOLUTION
Avatar of Tony Massa
Tony Massa
Flag of United States of America image

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
The last script just does what you asked...doesn't check for a group in AD.

-TM
Geeze....I keep missing this:

Since you have already SET the WshShell variable, you only need it once at the beginning of the script, you can delete the second occurance of:

            Set WshShell = WScript.CreateObject("WScript.Shell")
Avatar of bjv211

ASKER

Thanks for the AD tip, we cannot do that in our enterprise setup :)