PDA

View Full Version : Sleeper: Get server name



mdmackillop
05-20-2005, 10:13 AM
I want to distribute some code which requires that I determine the server name in each office in order to append it to a printername eg \\MyServer\LaserJet (file://\MyServerLaserJet). Any ideas? There is only one server in each office.

Paleo
05-20-2005, 08:22 PM
Hi Malcolm,

I think these WSH scripts may help you out:

Identifying Your Domain Controller:


Set objDomain = getObject(ldap://rootDse)
objDC = objDomain.Get("dnsHostName")
Wscript.Echo objDC


Retrieving the Domain Controller Site Name:


strDcName = "atl-dc-01"
Set objADSysInfo = CreateObject("ADSystemInfo")
strDcSiteName = objADSysInfo.GetDCSiteName(strDcName)
WScript.Echo "DC Site Name: " & strDcSiteName

mdmackillop
05-23-2005, 02:02 PM
Hi Carlos,
I couldn't get these to work. I now have the following


Sub ShowDriveList()
Dim fs, d, dc, s, n
Set fs = CreateObject("Scripting.FileSystemObject")
Set dc = fs.Drives
For Each d In dc
s = s & d.DriveLetter & " - "
If d.DriveType = 3 Then
n = d.ShareName
End If
s = s & n & vbCrLf
Next
MsgBox s
End Sub



This gives me the server and foldernames eg \\MyServer\Contract (file://\MyServerContract), from which I could extract the first bit easily enough, but surely there's a "name" to get MyServer on its own.

Bob Phillips
05-23-2005, 02:55 PM
This gives me the server and foldernames eg \\MyServer\Contract (file:///MyServerContract), from which I could extract the first bit easily enough, but surely there's a "name" to get MyServer on its own.

Isn't the problem that you are not 'connected' to a server as such, you just map to network shared folders? Those shares could all be on separate servers, or could be different folders on the same server, or a mix of both. As long as the servers are in the same domain, or their is a trusted relationship between domains, and their is a shared folder on those drives, you can map to those shares.

mdmackillop
05-23-2005, 03:08 PM
Each of our seven offices have their own server to which we log on, and all have an S: drive. This server also holds the shared network printer which we all use. I don't want to write seven different versions of my code, just to add the server name. The following works in my own office, but it seems a bit clumsy.


Sub DoPrint ()
Application.ActivePrinter = MyServer & "Network"
Application.PrintOut
End Sub


'Get the name of the server
Function MyServer()
Dim fs, d, s, Pos As Long
Set fs = CreateObject("Scripting.FileSystemObject")
Set d = fs.GetDrive(fs.GetDriveName(fs.GetAbsolutePathName("S:\")))
s = d.sharename
Pos = InStr(3, s, "\")
MyServer = Left(s, Pos)
End Function

Bob Phillips
05-23-2005, 03:22 PM
Each of our seven offices have their own server to which we log on, and all have an S: drive. This server also holds the shared network printer which we all use. I don't want to write seven different versions of my code, just to add the server name. The following works in my own office, but it seems a bit clumsy.

It is difficult to be precise as we don't have the setup you have, so I can only offer suggestions.

I would list out all of the Environ variables, and see if any of them look as if they have the value that you want (maybe (UserDomain)



Sub GetEnviron()
Dim sEnviron As String
Dim i As Long
i = 1
Do
sEnviron = Environ(i)
Debug.Print sEnviron
i = i + 1
Loop Until sEnviron = ""
End Sub


If one is what you want, you can then use


sServer = Environ("env_setting")

mdmackillop
05-23-2005, 03:29 PM
Exactly what I needed thanks.


MyServer = Environ("logonserver")

mvidas
05-23-2005, 03:38 PM
I just got your PM and am about 15 minutes too late :) glad you got it though!

Bob Phillips
05-23-2005, 03:59 PM
Exactly what I needed thanks.


MyServer = Environ("logonserver")

Good guess :)

mdmackillop
05-24-2005, 12:04 AM
How about a short Multiple Apps KB item. (I know there is one there for excel). Maybe just modify the debug print line to


Debug.Print i & " - " & Environ

Bob Phillips
05-24-2005, 02:01 AM
How about a short Multiple Apps KB item. (I know there is one there for excel).

The thing about Environ is that is relates to an OS attribute (collection), so the Excel one applies to an app.

What perhaps is more interesting is an article on environment variables that might change during a session, or even using environment variables to direct the logic flow. Environ is no good for this as all the values are loaded into memory at Excel start, so if they change during the session, Excel does not know. APIs are needed here.

Bob Phillips
05-24-2005, 02:11 AM
Just checked, there is also an article on retrieving all environment variables, http://www.vbaexpress.com/kb/getarticle.php?kb_id=217, by mvidas.

This article makes the common mistake of using an index number to tell how to retrieve a value. This article states


' Note: using Msgbox Environ(31) will return USERNAME=computerusername, where
' Msgbox Environ("username") will return just the username


The second line is correct, the first is (potentially) incorrect, as it depends on other variables. For instance on my machine here, Username is index 27, on your machine, who knows.