logon script to check the next available drive letter (The code is a bit long) 
Author Message
 logon script to check the next available drive letter (The code is a bit long)

Hi there,

I am just wondaring that how can I modify the following script in order to
check the next avaliable drive letter and map the network share? Sorry for
the long code attached.

Many THX

Simon

<script language="VBScript">

'<-----------------<Place Holders>--------->
'The following are place holders for company specific settings:
'<Domain>--------> Company's Domain
'<Share>---------> Share name on the servers (and second part of server
name, eg.. <user's OU>-<servertype>\<path>)
'<User's OU>--------> Used as first part of the server name
'
Dim sUser, sComputer, aUserOu, sCheck
Dim oADsRootDSE, sDFsPath
Dim oADsSysInfo, sADsUser, sADssComputer, sUserPath, oADsContainer
Dim oIE, oDoc, oBdy

Set wn = CreateObject("WScript.Network")
Set Shell = CreateObject("WScript.Shell")

'Exposes environment variables:
Set Environment = Shell.environment("Process")

' Connect to the LDAP server's root object
Set oADsRootDSE = GetObject("LDAP://RootDSE")

Set oADsSysInfo = CreateObject("ADSystemInfo")

sUser = wn.UserName

sDFsPath = "\\<Domain>\dfs_root"

' Get distinguished name of the current user
sADsUser = oADsSysInfo.UserName

'Create an array from the distingiushed name
aUserOu = split(sADsUser,",",-1,1)

'<---------------IE INITIALIZATIONS--------------------->
'Get Ready for error reporting via html page
Set oIE = createobject("InternetExplorer.application")
' Set up the IE interface
ExecuteGlobal(GetResource("IE_Template"))
'<------------------END INIT--------------------------->

For i= 0 to ubound(aUserOu)
sCheck = mid(aUserOu(i), 4,3)

 Select Case sCheck
 Case "<User's OU>"
 MapDrive(sCheck)
 MapPrinter(sCheck)
 Case "<User's OU>"
 MAPDrive(sCheck)
 MapPrinter(sCheck)
 Case "<User's OU>"
 MapDrive(sCheck)
 MapPrinter(sCheck)
 Case "<User's OU>"
 MapDrive(sCheck)
 MapPrinter(sCheck)
 Case "<User's OU>"
 MapDrive(sCheck)
 MapPrinter(sCheck)
 Case "<User's OU>"
 MapDrive(sCheck)
 MapPrinter(sCheck)

 End Select
Next

Function MapDrive(<User's OU>)

'Lets the script continue to the next task after an error:
On Error Resume Next

 With WN
  .MapNetworkDrive "g:", sDFsPath & "<share>"
   If Err.Number <> 0 then
   DummyVar = ErrHndl(oIE, ErrMsg)
   On Error Resume Next
   End If
  .MapNetworkDrive "h:", "\\" & <User's OU> & "<share>"
   If Err.Number <> 0 then
   DummyVar = ErrHndl(oIE, ErrMsg)
   On Error Resume Next
   End If
  .MapNetworkDrive "i:", sDFsPath & "<share>"
   If Err.Number <> 0 then
   DummyVar = ErrHndl(oIE, ErrMsg)
   On Error Resume Next
   End If
  .MapNetworkDrive "j:", "\\" & <User's OU> & "<share>" &
Environment("UserName")
   If Err.Number <> 0 then
   DummyVar = ErrHndl(oIE, ErrMsg)
   On Error Resume Next
   End If
  .MapNetworkDrive "m:", "\\" & <User's OU> & "<share>"
   If Err.Number <> 0 then
   DummyVar = ErrHndl(oIE, ErrMsg)
   On Error Resume Next
   End If
  .MapNetworkDrive "o:", "\\" & <User's OU> & "<share>"
   If Err.Number <> 0 then
   DummyVar = ErrHndl(oIE, ErrMsg)
   On Error Resume Next
   End If
  .MapNetworkDrive "s:", sDFsPath & "<share>"
   If Err.Number <> 0 then
   DummyVar = ErrHndl(oIE, ErrMsg)
   On Error Resume Next
   End If
  .MapNetworkDrive "u:", sDFsPath & "<share>"
   If Err.Number <> 0 then
   DummyVar = ErrHndl(oIE, ErrMsg)
   On Error Resume Next
   End If
 End With

'Error Handling:
If Err.Number <> 0 then
 ErrHndl
On Error Resume Next
End If
End Function

Function MapPrinter(<User's OU>)

Dim oPrinters, oContainer, oServer, oPrinter
Set oPrinters = WN.EnumPrinterConnections
Set oContainer = GetObject("LDAP://ou=<Servers' OU>,dc=<Domain>,dc=com")

'Find the server(s) for the matching <User's OU> & map all its printers
For Each oServer In oContainer

  If Mid(oserver.name, 4, 3) = <User's OU> Then
  oServer.Filter = Array("PrintQueue")
  For Each oPrinter In oServer
 On Error Resume Next

 wn.AddWindowsPrinterConnection oPrinter.PrinterPath
  If Err.Number <> 0 then
  DummyVar = ErrHndl(oIE, ErrMsg)
  On Error Resume Next
  End If

  Next
  Else
  End If

'Error Handling:
If Err.Number <> 0 then
 ErrHndl
On Error Resume Next
End If

Next

'Cleanup
Set oPrinters = Nothing
Set oContainer = Nothing
Set oServer = Nothing
Set oPrinters = Nothing

End Function

'<--------Error reporting using the IE template(see resource
below)-------------------->
'<------------Arguments are IE and textarea
objects------------------------->
Function ErrHndl(oIE, ErrMsg)

While oIE.Busy
wscript.sleep 100
Wend

oIE.Visible = True

With ErrMsg
 .Value = ErrMsg.Value & VBCLRF & sUser & "  Error: " & Err.Number & " " &
Err.Description
End With

'Let IE catch up
Do Until oIE.ReadyState = 4:WScript.Sleep 10 : Loop

End Function

'<-----------------Following is pop up window error
reporting--------------------->
'Error Handling:
' If false essentially comments out the popup (using html instead)
If False Then
If Err.Number <> 0 then
 MsgBox "Error: " & Err.Number & " " & Err.Description & _
 "Please keep this window open and call the helpdesk at <HelpDesk #>"
 Err.Clear
On Error Resume Next
end if
end if
</script>
<script language="VBScript">

'<--------------IE Interface------------------>

'===============================
' Procedures ---->  Not being used, but kept for reference
'===============================
'Sub Launch_Interface()
' ExecuteGlobal(GetResource("IE_Template"))

'End Sub

'Sub Wait_For_Input()
 ' This is here to keep the script thread waiting
 'Do
  'wscript.sleep 100
 'If WaitingForInput = False Then Exit Sub
 'Loop
'End Sub

'Sub GetInput()
 'WaitingForInput = false
'End Sub

'Sub oIE_onQuit()
 ' Just dies if IE is closed
 ' Wscript.Quit
'End Sub

</script>
<resource id="IE_Template">
<![CDATA[

'<--------------------HTML to be written:----------------------->

sLoc = """<Path to Pics>"
sMsg = "This window provides error reporting. Please keep " & _
    "this window open and call the helpdesk at <HelpDesk #>. You may also
email " & _
    "the Helpdesk by clicking on the &quot;Report&quot; button in the upper
right " & _
    "corner. The Helpdesk will need the information in the box below, please
do " & _
    "not delete any information."
sTableStart = "<div align=""center""><table width=""100%""><tr>"

sFormat = "<p align=""left""><font size=""+1"">"

STable = "<td><strong><font size=""+2"" face=""Arial, Helvetica,
sans-serif"">Company " & _
    "Error Reporting <img src=" & sLoc & "BbBar.gif"" width=""429""
height=""8""></font></strong></td>" & _
    "<td><div align=""right""><strong><font size=""+2"" face=""Arial,
Helvetica, sans-serif""><a href=""mailto:HelpDesk"">" & _
    "<img src=" & sLoc & "Report.gif"" width=""113"" height=""62""
border=""0""></a></font></strong></div></td></tr></table>"

sTableEnd = "<p align=""left""></div>"
'<----------------------End HTML---------------------------------------->

oIE.navigate "about:blank"

Do Until oIE.ReadyState = 4:WScript.Sleep 10 : Loop

 With oIE
 .MenuBar = True
 .AddressBar = False
 .StatusBar = False
 .Width = 650
 .Height = 400
 .Toolbar = True
 End With

set oDoc = oIE.document
set oBdy = oDoc.body

oDoc.Title = "Error Reporting " & string(5,"_") & sUser

 oBdy.scroll="auto"
 oBdy.insertAdjacentHTML "beforeEnd", sTableStrt
 oBdy.insertAdjacentHTML "beforeEnd", sTable
 oBdy.insertAdjacentHTML "beforeEnd",  sFormat & sMsg & sTableEnd
 set ErrMsg = oDoc.createElement("<textarea " _
  & "cols=70 rows=20 wrap=""off"">")
 oBdy.appendChild ErrMsg
 oBdy.appendChild oDoc.createElement("BR")

oIE.visible = False

 ]]>

</resource>
'Cleanup:
Set wn = Nothing
Set Shell = Nothing
Set Environment = Nothing
Set oADsRootDSE = Nothing
</job>
</package>



Mon, 12 Dec 2005 16:25:28 GMT  
 logon script to check the next available drive letter (The code is a bit long)

Quote:

> Hi there,

> I am just wondaring that how can I modify the following script in
> order to check the next avaliable drive letter and map the network
> share? Sorry for the long code attached.

> Many THX

> Simon

Everywhere you are using a hardcoded drive letter, use the results of the
following function:

Function FreeDrive()
  Dim i
  Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
  For i = Asc("D") To Asc("Z")
    If Not fso.DriveExists(Chr(i)) Then
      FreeDrive = Chr(i) & ":"
      Exit Function
    End If
  Next
End Function

For example...

 With WN
  .MapNetworkDrive FreeDrive(), sDFsPath & "<share>"
  ...
  .MapNetworkDrive FreeDrive(), "\\" & <User's OU> & "<share>"
  ...
  ..
  .
 End With

Quote:

> <script language="VBscript">

> '<-----------------<Place Holders>--------->
> 'The following are place holders for company specific settings:
> '<Domain>--------> Company's Domain
> '<Share>---------> Share name on the servers (and second part of
> server name, eg.. <user's OU>-<servertype>\<path>)
> '<User's OU>--------> Used as first part of the server name
> '
> Dim sUser, sComputer, aUserOu, sCheck
> Dim oADsRootDSE, sDFsPath
> Dim oADsSysInfo, sADsUser, sADssComputer, sUserPath, oADsContainer
> Dim oIE, oDoc, oBdy

> Set wn = CreateObject("WScript.Network")
> Set Shell = CreateObject("WScript.Shell")

> 'Exposes environment variables:
> Set Environment = Shell.environment("Process")

> ' Connect to the LDAP server's root object
> Set oADsRootDSE = GetObject("LDAP://RootDSE")

> Set oADsSysInfo = CreateObject("ADSystemInfo")

> sUser = wn.UserName

> sDFsPath = "\\<Domain>\dfs_root"

> ' Get distinguished name of the current user
> sADsUser = oADsSysInfo.UserName

> 'Create an array from the distingiushed name
> aUserOu = split(sADsUser,",",-1,1)

> '<---------------IE INITIALIZATIONS--------------------->
> 'Get Ready for error reporting via html page
> Set oIE = createobject("InternetExplorer.application")
> ' Set up the IE interface
> ExecuteGlobal(GetResource("IE_Template"))
> '<------------------END INIT--------------------------->

> For i= 0 to ubound(aUserOu)
> sCheck = mid(aUserOu(i), 4,3)

>  Select Case sCheck
>  Case "<User's OU>"
>  MapDrive(sCheck)
>  MapPrinter(sCheck)
>  Case "<User's OU>"
>  MAPDrive(sCheck)
>  MapPrinter(sCheck)
>  Case "<User's OU>"
>  MapDrive(sCheck)
>  MapPrinter(sCheck)
>  Case "<User's OU>"
>  MapDrive(sCheck)
>  MapPrinter(sCheck)
>  Case "<User's OU>"
>  MapDrive(sCheck)
>  MapPrinter(sCheck)
>  Case "<User's OU>"
>  MapDrive(sCheck)
>  MapPrinter(sCheck)

>  End Select
> Next

> Function MapDrive(<User's OU>)

> 'Lets the script continue to the next task after an error:
> On Error Resume Next

>  With WN
>   .MapNetworkDrive "g:", sDFsPath & "<share>"
>    If Err.Number <> 0 then
>    DummyVar = ErrHndl(oIE, ErrMsg)
>    On Error Resume Next
>    End If
>   .MapNetworkDrive "h:", "\\" & <User's OU> & "<share>"
>    If Err.Number <> 0 then
>    DummyVar = ErrHndl(oIE, ErrMsg)
>    On Error Resume Next
>    End If
>   .MapNetworkDrive "i:", sDFsPath & "<share>"
>    If Err.Number <> 0 then
>    DummyVar = ErrHndl(oIE, ErrMsg)
>    On Error Resume Next
>    End If
>   .MapNetworkDrive "j:", "\\" & <User's OU> & "<share>" &
> Environment("UserName")
>    If Err.Number <> 0 then
>    DummyVar = ErrHndl(oIE, ErrMsg)
>    On Error Resume Next
>    End If
>   .MapNetworkDrive "m:", "\\" & <User's OU> & "<share>"
>    If Err.Number <> 0 then
>    DummyVar = ErrHndl(oIE, ErrMsg)
>    On Error Resume Next
>    End If
>   .MapNetworkDrive "o:", "\\" & <User's OU> & "<share>"
>    If Err.Number <> 0 then
>    DummyVar = ErrHndl(oIE, ErrMsg)
>    On Error Resume Next
>    End If
>   .MapNetworkDrive "s:", sDFsPath & "<share>"
>    If Err.Number <> 0 then
>    DummyVar = ErrHndl(oIE, ErrMsg)
>    On Error Resume Next
>    End If
>   .MapNetworkDrive "u:", sDFsPath & "<share>"
>    If Err.Number <> 0 then
>    DummyVar = ErrHndl(oIE, ErrMsg)
>    On Error Resume Next
>    End If
>  End With

> 'Error Handling:
> If Err.Number <> 0 then
>  ErrHndl
> On Error Resume Next
> End If
> End Function

> Function MapPrinter(<User's OU>)

> Dim oPrinters, oContainer, oServer, oPrinter
> Set oPrinters = WN.EnumPrinterConnections
> Set oContainer = GetObject("LDAP://ou=<Servers'
> OU>,dc=<Domain>,dc=com")

> 'Find the server(s) for the matching <User's OU> & map all its
> printers
> For Each oServer In oContainer

>   If Mid(oserver.name, 4, 3) = <User's OU> Then
>   oServer.Filter = Array("PrintQueue")
>   For Each oPrinter In oServer
>  On Error Resume Next

>  wn.AddWindowsPrinterConnection oPrinter.PrinterPath
>   If Err.Number <> 0 then
>   DummyVar = ErrHndl(oIE, ErrMsg)
>   On Error Resume Next
>   End If

>   Next
>   Else
>   End If

> 'Error Handling:
> If Err.Number <> 0 then
>  ErrHndl
> On Error Resume Next
> End If

> Next

> 'Cleanup
> Set oPrinters = Nothing
> Set oContainer = Nothing
> Set oServer = Nothing
> Set oPrinters = Nothing

> End Function

> '<--------Error reporting using the IE template(see resource
> below)-------------------->
> '<------------Arguments are IE and textarea
> objects------------------------->
> Function ErrHndl(oIE, ErrMsg)

> While oIE.Busy
> wscript.sleep 100
> Wend

> oIE.Visible = True

> With ErrMsg
>  .Value = ErrMsg.Value & VBCLRF & sUser & "  Error: " & Err.Number &
> " " & Err.Description
> End With

> 'Let IE catch up
> Do Until oIE.ReadyState = 4:WScript.Sleep 10 : Loop

> End Function

> '<-----------------Following is pop up window error
> reporting--------------------->
> 'Error Handling:
> ' If false essentially comments out the popup (using html instead)
> If False Then
> If Err.Number <> 0 then
>  MsgBox "Error: " & Err.Number & " " & Err.Description & _
>  "Please keep this window open and call the helpdesk at <HelpDesk #>"
>  Err.Clear
> On Error Resume Next
> end if
> end if
> </script>
> <script language="VBScript">

> '<--------------IE Interface------------------>

> '===============================
> ' Procedures ---->  Not being used, but kept for reference
> '===============================
> 'Sub Launch_Interface()
> ' ExecuteGlobal(GetResource("IE_Template"))

> 'End Sub

> 'Sub Wait_For_Input()
>  ' This is here to keep the script thread waiting
>  'Do
>   'wscript.sleep 100
>  'If WaitingForInput = False Then Exit Sub
>  'Loop
> 'End Sub

> 'Sub GetInput()
>  'WaitingForInput = false
> 'End Sub

> 'Sub oIE_onQuit()
>  ' Just dies if IE is closed
>  ' Wscript.Quit
> 'End Sub

> </script>
> <resource id="IE_Template">
> <![CDATA[

> '<--------------------HTML to be written:----------------------->

> sLoc = """<Path to Pics>"
> sMsg = "This window provides error reporting. Please keep " & _
>     "this window open and call the helpdesk at <HelpDesk #>. You may
> also email " & _
>     "the Helpdesk by clicking on the &quot;Report&quot; button in the
> upper right " & _
>     "corner. The Helpdesk will need the information in the box below,
> please do " & _
>     "not delete any information."
> sTableStart = "<div align=""center""><table width=""100%""><tr>"

> sFormat = "<p align=""left""><font size=""+1"">"

> STable = "<td><strong><font size=""+2"" face=""Arial, Helvetica,
> sans-serif"">Company " & _
>     "Error Reporting <img src=" & sLoc & "BbBar.gif"" width=""429""
> height=""8""></font></strong></td>" & _
>     "<td><div align=""right""><strong><font size=""+2"" face=""Arial,
> Helvetica, sans-serif""><a href=""mailto:HelpDesk"">" & _
>     "<img src=" & sLoc & "Report.gif"" width=""113"" height=""62""
> border=""0""></a></font></strong></div></td></tr></table>"

> sTableEnd = "<p align=""left""></div>"
> '<----------------------End
> HTML---------------------------------------->

> oIE.navigate "about:blank"

> Do Until oIE.ReadyState = 4:WScript.Sleep 10 : Loop

>  With oIE
>  .MenuBar = True
>  .AddressBar = False
>  .StatusBar = False
>  .Width = 650
>  .Height = 400
>  .Toolbar = True
>  End With

> set oDoc = oIE.document
> set oBdy = oDoc.body

> oDoc.Title = "Error Reporting " & string(5,"_") & sUser

>  oBdy.scroll="auto"
>  oBdy.insertAdjacentHTML "beforeEnd", sTableStrt
>  oBdy.insertAdjacentHTML "beforeEnd", sTable
>  oBdy.insertAdjacentHTML "beforeEnd",  sFormat & sMsg & sTableEnd
>  set ErrMsg = oDoc.createElement("<textarea " _
>   & "cols=70 rows=20 wrap=""off"">")
>  oBdy.appendChild ErrMsg
>  oBdy.appendChild oDoc.createElement("BR")

> oIE.visible = False

>  ]]>

> </resource>
> 'Cleanup:
> Set wn = Nothing
> Set Shell = Nothing
> Set Environment = Nothing
> Set oADsRootDSE = Nothing
> </job>
> </package>

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US

Technet Script Center
http://www.microsoft.com/technet/scriptcenter/default.asp

Microsoft? Windows?2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overvie...



Tue, 13 Dec 2005 09:23:21 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Finding next available drive letter

2. next available drive letter

3. first available drive letter?

4. next available drive

5. Can you check out this logon script code?

6. Checking For Valid Drive Letter

7. checking to see if Network Drive available

8. How can I check available space on a drive

9. 128-bit (or 40-bit) Encryption Source-Code

10. Drive letter driving me crazy!

11. Retrieving drive letter for user's cd-rom drive

12. Determining drive letter assigned to floppy drive?

 

 
Powered by phpBB® Forum Software