0

我一直在快速编写一个小脚本,以便我可以根据个人组成员身份设置和删除网络驱动器或创建文件夹,它似乎不起作用并且没有错误消息,可以使用第二对眼睛,我真的很感激!

DIM CHS
SET CHS = CreateObject("Scripting.FileSystemObject")
SET CHSshell = CreateObject("WScript.Shell")
SET CHSnetwork = CreateObject("WScript.Network")

PRIVATE FUNCTION isMember( Group )
    SET netCHS = CreateObject("WScript.Network")
    Domain = netCHS.UserDomain
    User = netCHS.UserName
    isMember = false
    SET userCHS = GetObject("WinNT://" & Domain & "/" & User & ",user")
        FOR EACH Group in userCHS.Groups
            IF (Group.Name = GroupName) THEN
                isMember = true
        EXIT FOR
            END IF
    NEXT    
    SET userCHS = NOTHING
    SET netCHS = NOTHING
END FUNCTION

SET CHS = NOTHING

IF ( isMember("Domain Admins") = "True" ) THEN
    CHSnetwork.RemoveNetworkDrive "z:"
    WSript.Echo "CHSnetwork.UserName"
END IF
4

2 回答 2

1

你打电话

isMember("Domain Admins")

功能

PRIVATE FUNCTION isMember( Group )

获取 name 中的参数Group。但是您(重新/误用Group

FOR EACH Group in userCHS.Groups

循环userCHS.Groups和在

IF (Group.Name = GroupName) THEN

得到.Name比较GroupName。从哪里来GroupName?。尝试将函数的标题更改为

PRIVATE FUNCTION isMember( GroupName )

并考虑使用Option Explicit以避免此类错误。

于 2013-03-19T10:55:38.743 回答
0

此 VBS 脚本将显示用户所属的所有组:

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim oNetwork: Set oNetwork = CreateObject("WScript.Network")

Dim sUserName: sUserName = oNetwork.UserDomain & "/" & oNetwork.UserName
sUserName = InputBox("Windows User Name","Enter Windows User Name", sUserName)

If sUserName <> "" Then
    GetGroups
End If

Sub GetGroups
    Const ADS_READONLY_SERVER = 4
    Dim oGroup, oUser
    Dim oShell: Set oShell = Wscript.CreateObject("WScript.Shell")
    Dim sFolderPath: sFolderPath = GetFolderPath()
    Dim oNS: Set oNS = GetObject("WinNT:")
    Dim oList: Set oList = CreateObject("System.Collections.ArrayList")

    Dim sFilePath: sFilePath = sFolderPath & "\" & Replace(Replace(sUserName,"\","-"),"/","-") & "_groups.txt"

    Set oUser = oNS.OpenDSObject("WinNT://" & sUserName, "", "", ADS_READONLY_SERVER)
    For Each oGroup In oUser.groups
        oList.Add oGroup.Name
    Next

    oList.Sort()

    Dim oLog: Set oLog = fso.CreateTextFile(sFilePath, True)
    For Each sItem in oList
        oLog.Write sItem & vbCrLf
    Next

    oLog.Close
    oShell.Run sFilePath
End Sub

Function GetFolderPath()
    Dim oFile 'As Scripting.File
    Set oFile = fso.GetFile(WScript.ScriptFullName)
    GetFolderPath = oFile.ParentFolder
End Function
于 2017-06-21T14:08:42.673 回答