通过脚本实现AD用户自动连接打印机与共享文件夹

来源:互联网 发布:淘宝售后规则在哪看 编辑:程序博客网 时间:2024/06/12 04:28

ON ERROR RESUME NEXT

                               'Set up some variables and constants
Dim Choice
Dim Drive, DriveCollection, DriveObj
Dim EnvLogonServer, EnvOpSystem, EnvWinDir, EnvTempDir, EstNumOfMaps, EventLog, EventLogObj
Dim FileServer,FileSysObj
Dim LocalPort, LogContents, LoginBox
Dim MapCount, MsgString
Dim Proc0, PrintObj, PrintServer
Dim RemotePrinter
Dim Share
Dim UseLogonScreen
Dim WSHShell, WshProcessEnvironment, WSHNetwork
Dim oShell


'Disconnect ALL mapped drives

Call DisconnetDriver()

 

                              'This sets constants for the textfile object.

Const ForReading = 1, ForWriting = 2, ForAppending = 8

                              'This sets the use of the LoginBox Control to Yes.
                              'To Change it set the value to no
                             
UseLogonScreen = "yes"     

                              'This value is set for the progress bar of the
                              'LoginBox control. Estimate the average number
                              'of drives and printers mapped for a typical
                              'user session. It is important that you
                              '**ALWAYS OVERESTIMATE THIS VALUE**
                              'by a couple of drives. So that the user
                              'cannot close the loginbox dialog before the
                              '~~Script~~. has completed.

EstNumOfMaps = 9

                              'Set up some Objects for use later

set FileSysObj = CreateObject("Scripting.FileSystemObject")
Set WSHNetwork = CreateObject("WScript.Network")
Set WSHShell = CreateObject("WScript.Shell")
Set oShell = CreateObject("Shell.Application")
Set WshProcessEnvironment = WshShell.Environment("Process")


                              'Get some necessary environment variables

EnvWinDir = WshProcessEnvironment("windir")
EnvTempDir = WshProcessEnvironment("Temp")
EnvOpSystem = WshProcessEnvironment("OS")
Const FileServ01 = "fycnsrv03"
Const FileServ02 = "10.86.21.21"
Const PrintServ01 = "fycnsrv03"
Const printer1  = "it 2200"      'location: IT dept
Const printer2  = "fin hp1100"    'location: finance dept
Const printer3  = "Xerox_C3000_U" 'location: Fax room
Const printer4  = "HPLaserJ1320"  'location: planning section of material
Const printer5  = "Xerox_C3000_C" 'printing color, location:Fax room
Const printer6  = "  "

 


                             
User = ""    
Do While User = ""
  User = WSHNetwork.UserName
Loop

                                       
                              'Now its time to start the Logon Process

 

Set objUser = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & objUser.UserName)
strGroup = LCase(Join(CurrentUser.MemberOf))

 

 


                              'Now map some group specific drives. You can
                              'build the Select case to
                              'better reflect your environment

MapDrive "Y:", FileServ01, "personal/" & User
MapDrive "Z:", FileServ01, "public"

set UserObj = GetObject("WinNT://" & WshNetwork.UserDomain & "/" & WshNetwork.UserName)
For Each Group In UserObj.groups

     Select Case Group.Name
             Case "DG_IT"

      MapDrive "S:", FileServ01, "sap"
                    MapDrive "I:", FileServ01, "it2"
      ConnectPrinter PrintServ01, printer3
                 'ConnectPrinter PrintServ01, printer2
                 'ConnectPrinter PrintServ01, printer4
                    WshNetwork.SetDefaultPrinter "//" & PrintServ01 & "/" & Printer3
                   
             Case "DG_HR"
                   
      MapDrive "X:", FileServ01, "HR"
    
    
     
                    ConnectPrinter PrintServ01, printer5
                   
                   ' WshNetwork.SetDefaultPrinter "//" & PrintServ01 & "/" & Printer2


             Case "DG_Finance"
  
                
   MapDrive "h:", FileServ01, "report"
                 MapDrive "J:", FileServ01, "project"
    MapDrive "s:", FileServ01, "sap"
   MapDrive "X:", FileServ01, "account"
                
                 ConnectPrinter PrintServ01, printer2
                 WshNetwork.SetDefaultPrinter "//" & PrintServ01 & "/" & Printer2

             Case  "DG_Material"
             MapDrive "h:", FileServ01, "bom"
      MapDrive "i:", FileServ01, "report"
                    MapDrive "k:", FileServ01, "plan"
      MapDrive "m:", FileServ01, "pc"
      MapDrive "u:", FileServ01, "purchase"
      MapDrive "s:", FileServ01, "sap"
                    MapDrive "t:", FileServ01, "buyer"
                
   ConnectPrinter PrintServ01, printer4
                 WshNetwork.SetDefaultPrinter "//" & PrintServ01 & "/" & Printer4
                
             Case "DG_GM_Office"
                    MapDrive "h:", FileServ01, "bom"
      MapDrive "i:", FileServ01, "report"
                    MapDrive "k:", FileServ01, "plan"
      MapDrive "l:", FileServ01, "sales-price"
      MapDrive "m:", FileServ01, "project"
      MapDrive "o:", FileServ01, "meeting"
      MapDrive "q:", FileServ01, "customfile"
      MapDrive "s:", FileServ01, "sap"
                    MapDrive "w:", FileServ01, "sale"
     
  ' ConnectPrinter PrintServ01, printer5
                ' WshNetwork.SetDefaultPrinter "//" & PrintServ01 & "/" & Printer5
                
             Case "DG_Sales"
      MapDrive "h:", FileServ01, "bom"
      MapDrive "i:", FileServ01, "report"
                    MapDrive "k:", FileServ01, "plan"
      MapDrive "l:", FileServ01, "sales-price"
      MapDrive "m:", FileServ01, "project"
      MapDrive "q:", FileServ01, "customfile"
      MapDrive "s:", FileServ01, "sap"
                    MapDrive "x:", FileServ01, "sale"
               
                
                    ConnectPrinter PrintServ01, printer3
                    WshNetwork.SetDefaultPrinter "//" & PrintServ01 & "/" & Printer3
             
  
            
                
     End Select
Next

 

Wscript.quit

 

 

Sub MapDrive(Drive, FileServer, Share)
    
     FileServer = "//" & FileServer
     Share = "/" & Share
    
     WshNetwork.MapNetworkDrive Drive, FileServer & Share
                
                                
       
    
          
End Sub

                              'This subroutine adds printers
                                       
Sub ConnectPrinter(PrintServer, Queue)

     PrintServer = "//" & PrintServer
     Queue = "/" & Queue
        
     On error resume next
                             
                              'This next line is a workaround to provide
                              'some error checking when adding a printer
                              'since WshAdmin.dll does not report an error
                              'when the printer does not exists.
                             
        WsHNetwork.AddPrinterConnection Lpt8, PrintServer & Queue
     If err.number <> 0 Then
          LogContents = LogContents & "FAILURE!!! UNABLE to connect printer " & PrintServer & Queue & ". The remote file printer is unavailable!" & vbcrlf
          msgbox "The Logon Script was unable to connect " & vbcrlf & vbcrlf & PrintServer & Queue & vbcrlf & vbcrlf & "The remote printer is either unavailable or " & Port & " is already in use!" & vbCRLF & "Please contact your network administrator.", vbOk + vbcritical,"Acme Information Services."
       Else
    
                              'This next line deletes our dummy printer
                              'connection
                             
          'WsHNetwork.RemovePrinterConnection PrintServer & Queue
          WshNetwork.AddWindowsPrinterConnection  PrintServer & Queue
     End If
    
    
    
     err.clear
    
End Sub

                              'This subroutine enumerates the resultant
                              'drive mappings and printers for display in
                              'the logon screen control

Sub DisplayMappings()
    
     Set DriveCollection = WSHNetwork.EnumNetworkDrives
     MsgString = "You have access to following network " & vbcrlf & "drive and printer connections: " & vbCRLF & vbCRLF
     For i = 0 To DriveCollection.Count - 1 Step 2
          MsgString = MsgString & vbCRLF & vbtab & DriveCollection(i) & Chr(9) & DriveCollection(i + 1)
     Next
    
     Set PrinterCollection = WshNetwork.EnumPrinterConnections
     For i = 0 To PrinterCollection.Count - 1 Step 2
          MsgString = MsgString & vbCRLF & vbtab & PrinterCollection(i) & Chr(9) & PrinterCollection(i + 1)
     Next
    
  End Sub 


Sub DisconnetDriver()  

ON ERROR RESUME NEXT


  Dim WSHShell, WSHNetwork
Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")


'Disconnect ALL mapped drives
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
    WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
Next

'Give the PC time to do the disconnect, wait 300 milliseconds
wscript.sleep 3000

 


End Sub                       
                 
上文脚本是一个设备配置自动映射所属部门的文件夹及打印机,在AD中用户属性配置成登陆的脚本,可以在计算机启动过程中完成这些动作,大大提高了工作效率。