Tuesday, December 1, 2009

Map Network Drives and Log the Results

VBScript


' Name         : Login.vbs


' Version      : 3.2, 04/15/2004


' Author       : J. Jason Bergh


 


' INITIALIZATION


 


' Declare Variables


 


Option Explicit


 


Dim objNetDrive        ' Network Drive Object


Dim objNetDriveEnum    ' Network Drive Enumeration Object


Dim objWMIService      ' WMI Service Object


Dim strComputer        ' WMI Computer Namspace String


Dim colItems           ' WMI Item Collection (Win32_OperatingSystem)


Dim colComputers       ' WMI Computer Collection (Win32_ComputerSystem)


Dim objItem                ' WMI Item Object


Dim objComputer        ' WMI Computer Object


Dim strError           ' Event Log Error String


Dim strSuccess         ' Event Log Success String


Dim strEventLog        ' Event Log Report String


Dim WshShl                 ' Windows Scripting Host Shell Object


Dim WshNet                 ' Windows Scripting Host Network Object


Dim i                      ' Counter


 


' Instantiate Variables


Set objNetDrive = CreateObject("WScript.Network")


Set WshShl = WScript.CreateObject("WScript.Shell")


Set WshNet = WScript.CreateObject("WScript.Network")


 


' Ignore Error Messages


On Error Resume Next


 


' Instantiate Event Log Report


strEventLog = "Login Script v.3.2 status as follows:"


 


' Determine OS Type and Role


OSTypeAndRole()


 


' Enumerate Network Drives


Set objNetDriveEnum = WshNet.EnumNetworkDrives


 


' Map Network Drives


NetworkMapDrive "V:", "\\GODZILLA\DOWNLOADS"


NetworkMapDrive "W:", "\\GODZILLA\DOCUMENTS"


NetworkMapDrive "X:", "\\GODZILLA\PICTURES"


NetworkMapDrive "Y:", "\\GODZILLA\MUSIC"


 


' Write Event To The Application Event Log


EventLog()


 


' End Script


 


' FUNCTIONS


 


Function OSTypeAndRole()


 


        ' Instantiate WMI


        strComputer = "."


        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")


        Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)


 


        ' Query Target OS Type


        For Each objItem In colItems


               ' Target is in the Windows NT family if OS Type 18


               If objItem.OSType = 18 Then


                       ' Query target and if the target's role is a Domain Controller (Primary, Backup) the Login script will exit.


              


                       ' Instantiate WMI


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


                       Set colComputers = objWMIService.ExecQuery ("Select DomainRole from Win32_ComputerSystem")


                       ' Query target role if target is in Windows NT family


                       For Each objComputer In colComputers


                               ' Check target's role and terminate script if target is a Domain Controller (Domain Role > 3)


                               If objComputer.DomainRole > 3 Then


                                      Wscript.Quit()


                               End If


                       Next


               Else


                       ' Operating System is not part of the Windows NT family, terminate the Login script


                       Wscript.Quit()


               End If


        Next


 


        ' Instantiate Error and Success string values


        strError = "- Error determining operating system and role."


        strSuccess = " - Windows NT family functioning as a member workstation / server."


 


        ' Check for errors


        CheckError strError, strSuccess


End Function


 


Function NetworkMapDrive (Drive, Share)


        ' Enumerate and compare specified Drive Letter and UNC share point to mapped Drive Letters and UNC share points


        For i = 0 to objNetDriveEnum.Count -1 Step 2


               If LCase(Drive) = LCase(objNetDriveEnum.Item(i)) Then


                       If Not LCase(Share) = LCase(objNetDriveEnum.Item(i+1)) Then


                               ' Remove Network Share Point


                               WshNet.RemoveNetworkDrive Drive, TRUE, TRUE


                       Else


                               ' Drive mapped correctly, exit function


                               Exit Function


                       End If


               End If


        Next


 


        ' Map specified Drive Letter to specified UNC share point


        WshNet.MapNetworkDrive Drive, Share, TRUE


 


        ' Instantiate Error and Success string values


        strError = " - Error mapping drive " & Drive & " to " & Share & "."


        strSuccess = " - Drive " & Drive & " mapped to " & Share & "."


       


        ' Check for errors


        CheckError strError, strSuccess


End Function


 


Function CheckError (EventError, EventSuccess)


        ' Function status reported to Application Event Log string


       


        ' Check for errors


        If Err > 0 Then


               ' Error occured, mark Event Log string accordingly


               strEventLog = strEventLog & vbCR & EventError


               Err.Clear


        Else


               ' No errors, mark Event Log string complete for this function


               strEventLog = strEventLog & vbCR & EventSuccess


        End If


End Function


 


Function EventLog()


        ' Write Event to the Application Event Log


        WshShl.LogEvent 0, strEventLog


End Function


 


 


http://gallery.technet.microsoft.com/ScriptCenter/en-us/5ff5f445-d28d-45d5-8db7-bc5998ca8fcd

Print Friendly and PDF
Share/Bookmark

No comments:

Post a Comment