Page 1 of 1 1
Topic Options
#201549 - 2011-02-10 01:02 AM Use numbers from %logonserver%
newjackswinger Offline
Fresh Scripter

Registered: 2011-02-02
Posts: 6
Loc: Australia
Hey guys,

This is my first post on this forum, hope I am posting in the right section.

My question is:

Is it possible to extract numbers from %logonserver% and use those numbers to map network drives?

Situation is as follows:

I have roaming users on a school network. Depending on what school they are at, will depend on what network drives I would like mapped. I have decided that the best unique identifier would be their logonserver. Each school has a domain controller and a file/print server. The first few digits of the logonserver are the same digits on the FNP server.

So what I would like is something like the following:

%logonserver% = 1234DC01

I would like to use the 1234 in the example above to map drives:
use M: \\1234fnp01\data

If the logonserver has 5 digits e.g. 12345, I would like to use that also.


I want to avoid doing the following:
IF %logonserver% = 1234dc01
use m: \\1234fnp01\data
IF %logonserver% = 13456
use m: \\13456\data

Reason being is that they have close to 100 domain controllers and the script will not look very nice and probably take a while to run. I would like to just query the logonserver for the numbers I need and map drives based on that.

Hope I am clear in the description...

Thanks in advance...

Top
#201550 - 2011-02-10 01:56 AM Re: Use numbers from %logonserver% [Re: newjackswinger]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Welcome to KORG!

Take a look at the login script on my web site. It has a user-defined module specifically to do what you need and was developed for a large regional school district. Their code was simplified and used as an example in the package. It allows you to extract part of the computer name (easily changed to the %LOGONSERVER% value) and use it to look up the complete UNC path.

It's free to download and use, we provide two free support emails, and offer paid commercial support for it. No coding other than your small custom routine - just configure an INI file and deploy.

I can check my records as I believe there is a school in AUS that's using this script - they may be able to offer some ideas as well.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#201551 - 2011-02-10 02:12 AM Re: Use numbers from %logonserver% [Re: Glenn Barnas]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA
If the server name always ends in the last 4 chars as dc## then you could use something like:

$server=left(%logonserver%,-4)
use M:\ "\\" + $server + "fnp01\data"

A better way would be to use RegEx, but I am so weak in it, I could't begin to give you any advice on it.

Curious, how do you know for a fact that they will authenticate with the local dc? Maybe a local subnet lookup would be a better guide to location.
_________________________
(... better days ahead)

Top
#201552 - 2011-02-10 02:39 AM Re: Use numbers from %logonserver% [Re: Allen]
newjackswinger Offline
Fresh Scripter

Registered: 2011-02-02
Posts: 6
Loc: Australia
Hi Glenn,

Sorry whereabouts on your site can I find the script you are referring to?

Allen, I will give your code a go.

We have AD Sites and Services configured with all the subnets available. If someone doesn't authenticate with their local DC, then we may have an issue and we will know about it and resolve it.

Thanks for replies...

Top
#201556 - 2011-02-10 08:57 AM Re: Use numbers from %logonserver% [Re: newjackswinger]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
If you just want the numeric prefix you can go very simple:
 Code:
$sServerPfx=Val(%LOGONSERVER%)
USE M: "\\" + $sServerPfx + "fnp01\data"

Top
#201560 - 2011-02-10 12:11 PM Re: Use numbers from %logonserver% [Re: newjackswinger]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
It's on the product page, under the Admin Toolchest. The toolchest is a suite of ready to use scripts, all freely available for use. Several are offered with commercial support, including the login script.

Here's the direct link for the Universal Login Script.

The script is INI file based, so all data is kept separate from the code.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#201565 - 2011-02-11 04:04 AM Re: Use numbers from %logonserver% [Re: Glenn Barnas]
newjackswinger Offline
Fresh Scripter

Registered: 2011-02-02
Posts: 6
Loc: Australia
Hi Richard and Allen,

I have tried to use your code and have been unsuccessful.
I am using Kix 4.53.

I literally copied and paste your code into the script and it has not mapped. I am getting the right logonserver, access to the network share is working. Drive just won't auto map. I can map network drives by using IF INGROUP, use m: etc etc....

Any ideas?

Top
#201566 - 2011-02-11 06:34 AM Re: Use numbers from %logonserver% [Re: newjackswinger]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA
display your values so you know if they are right or not...

? %logonserver%
? $server
? "\\" + $server + "fnp01\data"
_________________________
(... better days ahead)

Top
#201568 - 2011-02-11 10:18 AM Re: Use numbers from %logonserver% [Re: Allen]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
And check for the failure reason:
 Code:
$sServerPfx=Val(%LOGONSERVER%)
USE M: "\\" + $sServerPfx + "fnp01\data"
If @ERROR "*FAILED* to map M: to \\" + $sServerPfx + "fnp01\data - Reason: ["+@ERROR+"] "+@SERROR+@CRLF EndIf

Top
#201577 - 2011-02-14 01:26 AM Re: Use numbers from %logonserver% [Re: Richard H.]
newjackswinger Offline
Fresh Scripter

Registered: 2011-02-02
Posts: 6
Loc: Australia
Hey guys,

Please excuse my ignorance. I am only familiar with the basic kix commands. I have tried the above and did not see any errors punched out?

Below are my basic scripts (without any of the code above put it). I am using 7777 as unique identifier for server.

My logonserver is 7777DCC01. I wish to use the 7777 from the logonserver to map drives to the FNP server

======================================================
7777.bat

@ECHO OFF
%logonserver%\netlogon\software\wkix32.exe %logonserver%\netlogon\7777\7777.kix /i
======================================================
7777.kix

; 7777.KIX
; KiXtart script to map network drives

$SERVER = '7777FNP01'
$DOMAIN = 'DOMAIN.LOCAL'

; ******** Map Network Drives ********

use i: /delete /persistent
use g: /delete /persistent
use m: /delete /persistent
use s: /delete /persistent

SLEEP 1
======================================================


Thank you for your assistance and patience.

Top
#201578 - 2011-02-14 02:18 AM Re: Use numbers from %logonserver% [Re: newjackswinger]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
A few things i'll add:

1) I find it's best not to statically reference the name of the server you are running the script from. You can use relative paths instead. Why this is useful is because if the name of the server changes, or you are running the script from another location, it will still work. For example, if its a multi-DC environment and you are storing the scripts in the NETLOGON share (SYSVOL), this will replicate between domain controllers.

So to do this, you would be best to place the batch file and kix script in the same directory and use:

%0\..\wkix32.exe %0\..\7777.kix /i

The "%0\.." bit is basically just saying 'in the same directory as the batch file'.

However that's not really critical, it just causes less headaches in the long run.

As for the script, a few pointers.

2) There is two reasons you aren't getting errors.
> Firstly, you are using wkix32 instead of kix32. wkix is the same as kix32 except it suppreses console output, so you will not see console output. For testing, kix32.exe is in my opinion, the better choice, once happy with it swap to wkix32.exe

> Secondly, kix only outputs error messages by default if there is a syntax error. The way you get error information is by telling the script you want it. For example:

 Code:

use i: /delete /persistent 
? "ERROR CODE = " + @ERROR
? "Verbose error description = " + @SERROR



@ERROR typically returns an integer, corresponding to (i think) typical windows error codes, such as '0' for successful. These are good for checking as conditions in loops, if/else/endif statements etc.

@SERROR typically returns a string, such as 'The operation has been succesful'

Without knowing the exact setup at your site it's difficult to know for sure, but this is possibly close to what you want:

<disclaimer> : I havn't tested this at all, use at own risk, do own testing etc if it breaks your computer, not my fault!! </disclaimer>

 Code:

   ; 7777.KIX
   ; KiXtart script to map network drives

   ;Variables

   ;Get the hostname of the server that served the logon request
   ;IE this should be the local domain controller, if in a domain environment

$strLogonServer = @LSERVER

   ;Get the domain name, rather than declaring it explicitly.
   ;This way script wont break if domain renamed or something

$strDomain = @DOMAIN

   ;Determine the Site Code.
   ;Do this by making the variable $strSiteCode equal to the
   ;first 4 characters of the $strLogonServer variable

$strSiteCode = Left($strLogonServer,4)

   ;Lets build a site-specific string equal to the name of the file server\share

$strSomeFileShare = "\\" + $strSiteCode + "FNP01\SomeShare"

   ;Delete some mappings, display error codes.
   ; This would really be best as a function, but best to keep it simple
   ; while learning

use i: /delete /persistent
? "i: - Error (" + @error + ") SERROR(" + @SERROR + ")"
use g: /delete /persistent
? "g: - Error (" + @error + ") SERROR(" + @SERROR + ")"
use m: /delete /persistent
? "m: - Error (" + @error + ") SERROR(" + @SERROR + ")"
use s: /delete /persistent
? "s: - Error (" + @error + ") SERROR(" + @SERROR + ")"

   ;Presumably you will want to map a drive(s). lets map it to z:\

mapdrive($strSomeFileShare, "z:")


   ;To prevent the script from closing the second 
   ;it has completed, and thereby not giving
   ;us a chance to see the output, we can tell it to 
   ;wait for the user to press a key before continuing/closing
   ;This should be removed before production, but handy for testing

Get $

   ;---------------------
   ;Function to Map a Drive
   ;---------------------
Function mapdrive($drivepath, $driveletter)
	Use $driveletter $drivepath
	If @ERROR = 0
		? "Drive: " $drivepath " was mapped successfully"
	Else
		? "Drive ( " + $drivepath + " ) was NOT mapped!"
		? "Error: " + @SERROR 
	EndIf
EndFunction







Edited by lukeod (2011-02-14 02:31 AM)

Top
#201579 - 2011-02-14 05:00 PM Re: Use numbers from %logonserver% [Re: lukeod]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
I think I realize the error. Val(%logonserver%) will not work due to the "\\" at the beginning. Try using this instead when you need to get the server number...

 Code:
$servernumber = Val(Split(%logonserver%,"\\")[1])
$server = "\\"+$servernumber+"FNP01"

You can then use the new $server variable further down to map your drives.
For example...
 Code:
Use i: $server+"\sharename"

Top
#201605 - 2011-02-16 04:28 AM Re: Use numbers from %logonserver% [Re: ShaneEP]
newjackswinger Offline
Fresh Scripter

Registered: 2011-02-02
Posts: 6
Loc: Australia
Hey CitrixMan,

That code works perfectly. I tried it on three different servers with different server numbers (some had 4 and some had 5) and the drives mapped correctly. Thanks, much appreciated.

What would happen if the logonserver doesn't have any numeric values i.e. OFFDC01 rather than 7777DC01, will the script just fail or continue to process?

Thanks heaps everyone for your help...

Top
#201606 - 2011-02-16 04:38 AM Re: Use numbers from %logonserver% [Re: newjackswinger]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Any portion that uses that $server variable will more than likely not work.

What the Val() function does int he first line is tries to get the numerical value of the %logonserver%. It stops when it reaches the first non numeric character, and will simply return a 0 if non are encountered...so in the case of OFFDC01, the $server variable will become '0FNP01' instead of something like '7777FNP01'.

You could add an IF statement before the segment of the script that uses the $server variable to ensure it was set correctly first...For example...
 Code:
IF Len($servernumber)>1
   net use i: $server+"\sharename"
   net use j: $server+"\sharename"
   etc...
EndIf

Top
#201607 - 2011-02-16 04:42 AM Re: Use numbers from %logonserver% [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
You could also add an alternative when the $server is initially set...example...
 Code:
$servernumber = Val(Split(%logonserver%,"\\")[1])
if $servernumber>0
   $server = "\\"+$servernumber+"FNP01"
else
   $server = %logonserver%
endif

Top
#201608 - 2011-02-16 05:41 AM Re: Use numbers from %logonserver% [Re: ShaneEP]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA
This might work too...

 Code:
$server=right(left(%logonserver%,-4),-2)


This would get everything between the \\ and DC01.


Edited by Allen (2011-02-16 05:43 AM)
_________________________
(... better days ahead)

Top
#201609 - 2011-02-16 05:58 AM Re: Use numbers from %logonserver% [Re: Allen]
newjackswinger Offline
Fresh Scripter

Registered: 2011-02-02
Posts: 6
Loc: Australia
Awesome, thanks guys for the help...
Top
#201610 - 2011-02-16 05:27 PM Re: Use numbers from %logonserver% [Re: newjackswinger]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA
And just for fun... using substr

 Code:
$server=substr(%logonserver%,3,len(%logonserver%)-6)
_________________________
(... better days ahead)

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (StuTheCoder) and 798 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.074 seconds in which 0.029 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org