2020 年 3 月 25 日

IT Skills 波林

Polin WEI – 資訊工作者的技術手札

User Login Script – VBScript

1 min read
sunflower

User Login Script in AD – VBScript

在有 windows AD 網域的情況下, 可以利用 VBScript 撰寫 login script 讓每個使用者有自己的家目錄(Home Directory)

'==========================================================================
' VBScript Source File 
' 
' Writer      :  Polin WEI
' Last Update : 2019/08/22
'==========================================================================

' 有問題時, 執行下一個指令
On Error Resume Next 
Dim objSysInfo, objUser, objNetwork, oShell 

Set objSysInfo = CreateObject("ADSystemInfo")
' Currently logged in User 取得登入者資訊
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)

 ' or specific user: 若要指定用戶時
 'Set objUser = GetObject("LDAP://CN=polin,OU=Users,DC=demo,DC=com")

Set objNetwork = CreateObject("WScript.Network")
Set oShell = WScript.CreateObject ("WScript.Shell")


'********************************* Begin **********************************

'=========================== MAP Network Drivers ==========================

' Remove OLD Maping Drivers
Call RemoveDriver("M:")
Call RemoveDriver("P:")
Call RemoveDriver("Z:")


' Mount Default Maping Drivers 共同網路磁碟
Call MapDriver("M:","\\fs01.demo.com\DEPARTDATA")
Call MapDriver("P:","\\fs01.demo.com\PROJECTDATA")


' Map Network Driver for User Home Directory
Call MapDriver("Z:","\\fs01.demo.com\UserData\" & objUser.displayName)



'********************************* Sub Code *******************************

'-------------------------- Map Driver sub code ---------------------------

   Sub MapDriver (Drivers,FullUNC)    
           ' Connect to public area
           objNetwork.MapNetworkDrive Drivers , FullUNC , True     
   End Sub

'------------------------ Remove Driver sub code --------------------------

   Sub RemoveDriver (Drivers)   
           objNetwork.RemoveNetworkDrive Drivers, True, True
   End Sub

Ref: https://ss64.com/vb/syntax-userinfo.html

Copyright © All rights reserved. | Newsphere by AF themes.