首页 | 邮件资讯 | 技术教程 | 解决方案 | 产品评测 | 邮件人才 | 邮件博客 | 邮件系统论坛 | 软件下载 | 邮件周刊 | 热点专题 | 工具
网络技术 | 操作系统 | 邮件系统 | 客户端 | 电子邮箱 | 反垃圾邮件 | 邮件安全 | 邮件营销 | 移动电邮 | 邮件软件下载 | 电子书下载

操作系统

Windows 9X | Linux&Uinx | Windows Server | 其它操作系统 | Vista | FreeBSD | Windows 7 |
首页 > 操作系统 > Windows Server > 通过脚本实现AD用户自动连接打印机与共享文件夹 > 正文

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

出处:徐火军博客 作者:徐火军 时间:2011-6-9 16:23:51

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中用户属性配置成登陆的脚本,可以在计算机启动过程中完成这些动作,大大提高了工作效率。

相关文章 热门文章
  • 如何在iPhone/iPod touch/iPad邮件应用程序Mail中使用QQ邮箱Exchange移动终端同步服务?
  • 避免AD攻击 防止密码破解和其它常用目录攻击
  • AD 1058与1030错误日志过多解决办法
  • AD灾难恢复
  • 实现微软AD与Domino OA系统的互连互用
  • ADMT 3.2 迁移密码
  • 在Exchange Server 2010中启用结构化地址簿(Hierarchical Address Book)
  • AD复制排错
  • 从Windows 2000 AD升级到Windows Server 2003 R2时遇到的一点小麻烦
  • 明明白白AdminSDHolder对象
  • 解决IPad和IPhone接收exchange邮件的问题
  • postfix+dovecot+postfixadmin+mysql架设邮件服务器
  • “http 500内部服务器错误”的解决方法
  • 利用Windows 2000 Server的RRAS实现VPN服务器
  • 用凤凰万能启动盘解决本地/域管理员密码丢失
  • Win2003 Server企业版安装配置
  • Active directory 灾难恢复
  • Windows 2000/03域和活动目录
  • 如何在vmware4上创建windows 2003群集
  • MSI文件制作全过程
  • Win2000命令全集(一)
  • Windows 2000/AD技巧
  • 此系统的本地策略不允许您采用交互式登录解决方法
  • Win2000路由的安装与设置实现不同网段互通
  • 自由广告区
     
    最新软件下载
  • SharePoint Server 2010 部署文档
  • Exchange 2010 RTM升级至SP1 教程
  • Exchange 2010 OWA下RBAC实现的组功能...
  • Lync Server 2010 Standard Edition 标..
  • Lync Server 2010 Enterprise Edition...
  • Forefront Endpoint Protection 2010 ...
  • Lync Server 2010 Edge 服务器部署文档
  • 《Exchange 2003专家指南》
  • Mastering Hyper-V Deployment
  • Windows Server 2008 R2 Hyper-V
  • Microsoft Lync Server 2010 Unleashed
  • Windows Server 2008 R2 Unleashed
  • 今日邮件技术文章
  • 腾讯,在创新中演绎互联网“进化论”
  • 华科人 张小龙 (中国第二代程序员 QQ...
  • 微软推出新功能 提高Hotmail密码安全性
  • 快压技巧分享:秒传邮件超大附件
  • 不容忽视的邮件营销数据分析过程中的算..
  • 国内手机邮箱的现状与未来发展——访尚..
  • 易观数据:2011Q2中国手机邮箱市场收入..
  • 穿越时空的爱恋 QQ邮箱音视频及贺卡邮件
  • Hotmail新功能:“我的朋友可能被黑了”
  • 入侵邻居网络发骚扰邮件 美国男子被重..
  • 网易邮箱莫子睿:《非你莫属》招聘多过..
  • 中国电信推广189邮箱绿色账单
  • 最新专题
  • 鸟哥的Linux私房菜之Mail服务器
  • Exchange Server 2010技术专题
  • Windows 7 技术专题
  • Sendmail 邮件系统配置
  • 组建Exchange 2003邮件系统
  • Windows Server 2008 专题
  • ORF 反垃圾邮件系统
  • Exchange Server 2007 专题
  • ISA Server 2006 教程专题
  • Windows Vista 技术专题
  • “黑莓”(BlackBerry)专题
  • Apache James 专题
  • 分类导航
    邮件新闻资讯:
    IT业界 | 邮件服务器 | 邮件趣闻 | 移动电邮
    电子邮箱 | 反垃圾邮件|邮件客户端|网络安全
    行业数据 | 邮件人物 | 网站公告 | 行业法规
    网络技术:
    邮件原理 | 网络协议 | 网络管理 | 传输介质
    线路接入 | 路由接口 | 邮件存储 | 华为3Com
    CISCO技术 | 网络与服务器硬件
    操作系统:
    Windows 9X | Linux&Uinx | Windows NT
    Windows Vista | FreeBSD | 其它操作系统
    邮件服务器:
    程序与开发 | Exchange | Qmail | Postfix
    Sendmail | MDaemon | Domino | Foxmail
    KerioMail | JavaMail | Winwebmail |James
    Merak&VisNetic | CMailServer | WinMail
    金笛邮件系统 | 其它 |
    反垃圾邮件:
    综述| 客户端反垃圾邮件|服务器端反垃圾邮件
    邮件客户端软件:
    Outlook | Foxmail | DreamMail| KooMail
    The bat | 雷鸟 | Eudora |Becky! |Pegasus
    IncrediMail |其它
    电子邮箱: 个人邮箱 | 企业邮箱 |Gmail
    移动电子邮件:服务器 | 客户端 | 技术前沿
    邮件网络安全:
    软件漏洞 | 安全知识 | 病毒公告 |防火墙
    攻防技术 | 病毒查杀| ISA | 数字签名
    邮件营销:
    Email营销 | 网络营销 | 营销技巧 |营销案例
    邮件人才:招聘 | 职场 | 培训 | 指南 | 职场
    解决方案:
    邮件系统|反垃圾邮件 |安全 |移动电邮 |招标
    产品评测:
    邮件系统 |反垃圾邮件 |邮箱 |安全 |客户端
    广告联系 | 合作联系 | 关于我们 | 联系我们 | 繁體中文
    版权所有:邮件技术资讯网©2003-2010 www.5dmail.net, All Rights Reserved
    www.5Dmail.net Web Team   粤ICP备05009143号