Windows-Server-2003

更改驅動器映射的腳本

  • October 7, 2015

我計劃淘汰舊的文件伺服器。我注意到我的一些使用者已將大約 10-15 個“映射網路驅動器”映射到此伺服器。是否有一個腳本可以用來更改映射中的伺服器名稱?…為了避免他們刪除和重新創建映射的驅動器?

這是我在所有域中用於登錄腳本的基本佈局。我會注意到

WshShell.Run "net use z: /delete", 0, False

行…可能會製作登錄腳本,該腳本只是簡單地刪除映射的驅動器,然後讓它映射正確的驅動器(如果有的話)。

這在登錄時的批處理腳本中簡單得多,只需使用

net use x: /delete
net use x: \\server\share

我知道這個腳本很大,但是在為廣告中的安全組設置列印機/驅動器/等時它會派上用場。

Option Explicit
Const ADS_PROPERTY_APPEND = 3        'sets the variable to Append
Const ADS_UF_NORMAL_ACCOUNT = 512  
Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D
CONST HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Dim WshShell : Set WshShell = CreateObject("wscript.shell")
Dim strContainer, strUser, i, objRootDSE, strDisplayName, ObjFSO, objInFile, objContainer, strLine, strName, objOU, objGroup, objUser, objFile, objFile2, varDomainNC, objRoot, strText, FirstLine, arrMemberOf, Group, strFirstName, strLastName, strLine2, objOU2, objNetwork, strGroup, objConnection, objCommand, objRecordSet, objErrorLog, strComputer, colItems, objWMIService, colInstalledPrinters, strComputer2
Set objOU2 = GetObject("LDAP://CN=users,DC=arra,DC=local")
Set objOU = GetObject("LDAP://OU=arra-users,DC=arra,DC=local")
ObjOU.Filter= Array("user")
Set objGroup = objOU2.Getobject("group", "cn=CSRs")
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objNetwork  = WScript.CreateObject("Wscript.Network")
Set objRootDSE = GetObject("LDAP://rootDSE")
strComputer2 = "."
Dim CRLF
CRLF = Chr(13) & Chr(10)

'*************(Global Scripting) this section applies to all computers no matter what group users are in.

''default lockheed banner script
Function Ask(strAction)

   Dim intButton
   intButton = MsgBox(strAction,                   _
                      vbQuestion + vbYesNo,        _
                      L_Welcome_MsgBox_Title_Text )
   Ask = intButton = vbYes

End Function

MsgBox "This system is the property of this Corporation, and is intended for" & CRLF &   _
      "the use of authorized users only. All activities of individuals using this computer" & CRLF &   _
      "with or without authority, or in excess of their authority, may be monitored and recorded" & CRLF &   _
      "by system personnel. If any such monitoring reveals evidence of criminal activity or is in" & CRLF &   _
      "violation of foreign or U.S. state or federal law, such evidence may be provided to law" & CRLF &   _
      "enforcement officials and/or used for further legal action by this Corporation and/or the" & CRLF &   _
      "organization's Information Protection group. Unauthorized use of this system is prohibited" & CRLF &   _
      "and may result in revocation of access, disciplinary action and/or legal action. The" & CRLF &   _
      "company reserves the right to monitor and review user activity, files and electronic messages." & CRLF &   _
      "REMINDER: Information transmitted to a foreign person on this network may be subject " & CRLF &   _
      "to applicable Export Control laws. Contact your Export Coordinator for assistance." & CRLF &   _
      "(This machine is not authorized for classified processing)",   _
      vbOKOnly,   _
      "SYSTEM USE MONITORING NOTICE - IPM-003 Banner Statement"

'*************End of global scripting

''pull local computer name for loggin info.
strComputer = objNetwork.ComputerName 

''pull logon id
strUser = objNetwork.UserName

''turn logon id into container name for LDAP queries

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = "<LDAP://" & objRootDSE.get("defaultNamingContext") & _
       ">;(&(objectCategory=User)(samAccountName=" & strUser & "));name;subtree"
Set objRecordSet = objCommand.Execute
On Error Resume Next
strUser = objRecordSet.Fields("name")
On Error GoTo 0
objConnection.Close
Set objRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing

''set user to have LDAP queries run
Set objUser = GetObject("LDAP://cn=" & strUser & ",ou=arra-users,dc=arra,dc=local")

''\/\/\/\/\/\/Determine Group memberships.  PLEASE NOTE:  group names must be in UPPER case and the "Left(strGroup, X)" 
'    X must be the number of characters in the group name.
'\/\/\/\/\/\/\/

arrMemberOf = objUser.GetEx("memberOf")

If Err.Number <>  E_ADS_PROPERTY_NOT_FOUND Then
For Each Group in arrMemberOf
 strGroup = UCase(Group)
 strGroup = Right(strGroup, Len(strGroup) - 3)
 If Left(strGroup, 2) = "IT" Then
 '*****IT group scripting

 ''set Z:IT drive
 WshShell.Run "net use z: /delete", 0, False
 WshShell.Run "Net use z: \\indarradc04\it", 0, False

 ''Prepare to set printers
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

 ''This prevents script from stopping when mapping network printers on the server where they
 ''are shared from
 ON ERROR RESUME NEXT

 ''Add Printers
 objNetwork.AddWindowsPrinterConnection("\\indarradc03\Xerox WorkCentre 5675 PS")

 '*****End of IT
 Else
  If Left(strGroup, 4) = "CSRS" Then
  '*****CSR group scripting

  '*****End of CSR
  Else
   If Left(strGroup, 10) = "MANAGEMENT" Then
   '*****Management group scripting - NOTE: all managers are members of "Team Leads" group

   '*****End of Management
   Else
    If Left(strGroup, 10) = "TEAM LEADS" Then
    '*****Team Lead scripting

     ''Prepare to set printers
     Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

     ''This prevents script from stopping when mapping network printers on the server
     ''where they are shared from
     ON ERROR RESUME NEXT

     ''Add Printers
     objNetwork.AddWindowsPrinterConnection("\\indarradc03\Xerox WorkCentre 5675 PS")

    '*****End of Team Lead
    End If
   End If
  End If
 End If
Next
Else
'*****Create Error Log if groups could not be determined

Set objErrorLog = objFSO.OpenTextFile("\\indarradc04\errors\signonerrors.txt", ForAppending, True)
objErrorLog.WriteLine strUser & " on " & strComputer & " could not be found in Active Directory on " & Date
Err.Clear
End If

引用自:https://serverfault.com/questions/87012