获得&管理/更新某些Windows域的本地内置帐户&独立服务器(vbscript)

时间:2012-08-25 12:52:00

标签: windows vbscript accounts

** 嗨! 我想知道是否有人尝试了类似的东西,我有一些代码与很多胶水合并......但是因为我是vbs中的新手,我可以肯定大多数都是错的。基本上我想在内置管理员帐户审核/更新期间使用自动vbscript为此任务节省大量时间。 我有6个帐户名称,每个帐户都有一个特定的密码。 我不是100%确定每台服务器使用的是哪个本地帐户名,但我可能需要手动验证或尝试查看是否可以使用其他文件,此脚本将读取可能的帐户名称和密码和使用某种蛮力 这就是我所拥有的:**

**更新8-29-12 a(已删除)

**更新8-29-12 b“这一项正在运作..但我需要测试并使用cpau进行NDC”

Option Explicit

Dim strExcelPath, objExcel, objSheet, intRow, strUserDN, strPassword, comp
Dim objUser

' Spreadsheet file.
strExcelPath = "c:\List.xls"

' Bind to Excel object.
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Excel application not found."
    Wscript.Quit
End If
On Error GoTo 0

' Open spreadsheet.
On Error Resume Next
objExcel.Workbooks.Open strExcelPath
If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Spreadsheet cannot be opened: " & strExcelPath
    Wscript.Quit
End If
On Error GoTo 0

' Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)


intRow = 2
Do While objSheet.Cells(intRow, 1).Value <> ""
    comp = objSheet.Cells(intRow, 1).Value
    strUserDN = objSheet.Cells(intRow, 2).Value
    strPassword = objSheet.Cells(intRow, 3).Value
    On Error Resume Next
    Set objuser = GetObject ("WinNT://" & comp & "/" & strUserDN & ",user")
    If (Err.Number <> 0) Then
        On Error GoTo 0
        Wscript.Echo "Data NOT found: "
    Else
        objUser.SetPassword strPassword
        If (Err.Number <> 0) Then
            On Error GoTo 0
            Wscript.Echo "Password NOT set for: " & strUserDN
        Else


        End If
    End If
    intRow = intRow + 1
Loop

' Close the workbook.
objExcel.ActiveWorkbook.Close

' Quit Excel.
objExcel.Application.Quit

Wscript.Echo "Done"

我的选项“B”可以使用以下内容重新开始: @echo off

for / F“delims =”%% i in(servers.txt)do(  psexec \ %% i NET USER&gt; %% i.txt )

**

可能有数百种方法可以解决这个问题,我的想法是避免让某人手动修改列出的服务器的管理员密码,并在一个月后未在AD中列出。

任何帮助都将不胜感激。

此致

2 个答案:

答案 0 :(得分:0)

此代码将列出由变量server指定的一台计算机的本地用户,然后打印每个用户的用户ID。

server = "YourServerName"
Set oComputer = GetObject("WinNT://" & server & "")
oComputer.Filter = Array("User")
For Each oUser in oComputer
  WScript.Echo oUser.Name
Next

答案 1 :(得分:0)

'Objective: check multiple servers for admin accounts status and report to html file

Set iFSO = CreateObject("Scripting.FilesyStemObject") 
Set oFSO = CreateObject("Scripting.FilesyStemObject") 

InputFile = WScript.Arguments.Named("servers")
if len(InputFile) < 1 then
    wscript.echo "Error: Servers Parameter not found" & vbCrLf
    show_usage
    wscript.quit
end if

Outputfile= InputFile & "_guest_admins_" + cstr(Month(now()))+"_"+cstr(day(now()))+".htm" 

if not ofso.FileExists(inputfile) then
    wscript.echo "Error: Server list file not Found."
    wscript.quit
end if

Set ofile = ofso.createTextFile(OutputFile, True) 
Set ifile = iFSO.OpenTextFile(inputfile) 

ofile.writeline "<html>" & html_head & "<body>"

    ofile.writeline "<table border=1 cellpadding=1 cellspacing=0>"
    ofile.writeline o 
    ofile.writeline "<tr><td>Hostname</td><td>User</td><td>Disabled</td><td>Locked</td><td>Expiration Date</td><td>Flags</td><td>BuiltIn</td></tr>"

Do until ifile.AtEndOfLine
Computer = ifile.ReadLine
if ping(Computer) then
            Builtin = ""
            if Check_WMI(Computer) then
                Builtin = GetBuiltInAccount(Computer)
            else
                Builtin = "WMI Fail"
            end if

            strt = now
            wscript.echo "Checking Users for server: " & Computer

            on error resume next

            Set objGroup = GetObject("WinNT://" & Computer & "/Administrators,group")

            if err.number <> 0 then
                wscript.echo "GetObject WinNT Failed"
                ofile.writeline "<tr><td>" & computer & "</td><td colspan=6 align=center>GetObject WinNT Fail: "& err.number &"</td></tr>"
            else            
                on error goto 0

                For Each objUser in objGroup.Members
                        ofile.writeline GetUserNT(computer, objUser.Name, Builtin)
                Next

                wscript.echo "Elapsed Time: " & datediff("s", strt, now) & " seconds"
            end if
else
    wscript.echo computer & " does not reply ping"
    ofile.writeline "<tr><td>" & computer & "</td><td colspan=6 align=center>No Ping Reply</td></tr>"
end if
Loop 
ofile.writeline "</table>"

ofile.writeline "</body></html>"

function ping(target)
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colPingedComputers = objWMIService.ExecQuery("Select * from Win32_PingStatus Where Address = '"& target & "'")
    For each objComputer in colPingedComputers
      ' If the status code is Null or Not 0 then the ping failed
      If IsNull( objComputer.StatusCode ) Or objComputer.StatusCode <> 0 Then 
        ' Set the function to return Boolean FALSE
        Ping = False
      Else
        ' Set the function to return Boolean TRUE
        Ping = True
      End If
    Next
end function

sub show_usage
    wscript.echo "Usage:  cscript chkusers /servers:list.txt" & vbcrlf

    wscript.echo vbtab & "/servers Parameter is a Text File one Servername per Line" & vbcrlf

    wscript.echo "Notes:  This script generates an html report of server admin accounts."
    wscript.echo "  Results are saved in a file named + date + htm extension."
    wscript.echo "  Output example filename: list_guest_admins_" + cstr(Month(now()))+"_"+cstr(day(now()))+".htm"

end sub

Function Check_WMI(strServer)
    On Error Resume Next ' error handling off

    ' create object reference, connect to namespace root\default
    Set oCimOmId = GetObject("winmgmts:"& strServer & "\root\default:__cimomidentification=@")

    ' Test whether WMI is present or not.
    If Err <> 0 then
        Check_WMI= true
    else
        Check_WMI= false
    end if
    on error goto 0
end function

Sub EnumNameSpaces(strNameSpace)
    'call enumnamespaces("root")
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\" & strNameSpace)
    Set colNameSpaces = objWMIService.InstancesOf("__NAMESPACE")
    For Each objNameSpace In colNameSpaces
        Call EnumNameSpaces(strNameSpace & "\" & objNameSpace.Name)
    Next
End Sub

function CSS
tt = "<style type=""text/css"">" & vbcrlf
tt=tt & " body {font-family:Verdana;font-size: 10px;color: #49403B;background: #EFEFEF;}" & vbcrlf
tt=tt & "table {font-family:Verdana;font-size: 12px; empty-cells:show; }" & vbcrlf
tt=tt & "</style>"  & vbcrlf
CSS = tt
end function

function html_head
    tt="<head>" & vbcrlf
    tt=tt & CSS
    html_head = tt & "</head>" & vbcrlf
end function


function GetUserNT(strComputer, usr, bltin)
    Const ADS_UF_DONT_EXPIRE_PASSWD = &H10000

    o=""
    On Error Resume Next

    Set objUser = GetObject("WinNT:// " & strComputer & "/" & usr & " ")

    o=o& "<tr><td>"& strcomputer &"</td><td>"& usr &"</td>"

    if len(objUser.AccountDisabled) = 0 then exit function

    o=o& "<td>&nbsp;"& StrDisabled(objUser.AccountDisabled) &"</td>"
    o=o& "<td>&nbsp;"& StrLocked(objUser.IsAccountLocked) &"</td>"


    o=o& "<td>&nbsp;"
        o=o& objUser.Get("UserFlags") AND ADS_UF_DONT_EXPIRE_PASSWD
    o=o& "</td>"

    o=o& "<td>&nbsp;"
        o=o& objUser.AccountExpirationDate
    o=o& "</td>"

    if lcase(bltin) = lcase(usr) then
        o=o & "<td>&nbsp;Built-In</td>"
    elseif instr(bltin, "[[Fail]]") > 0 then
        o=o & "<td>&nbsp;"& bltin &"</td>"
    else
        o=o & "<td>&nbsp;</td>"
    end if

    o = o & "</tr>"

    GetUserNT = o
end function

function StrLocked(str)
    if str = "True" then
        StrLocked = "Locked"
    else
        StrLocked = "Unlocked"
    end if
end function

function StrDisabled(str)
    if str = "True" then
        StrDisabled = "Disabled"
    else
        StrDisabled = "Enabled"
    end if
end function

function GetUsers(strComputer,grp,usr)
    On Error Resume Next
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_UserAccount Where name = '"& usr &"'")
    o=""
    For Each objItem in colItems
        o=o& "<tr>"
        o=o& "<td>" & strComputer & "</td>"
        o=o& "<td>" & grp & "</td>"
        o=o& "<td>" & objItem.AccountType & "</td>"
        o=o& "<td>" & objItem.Caption & "</td>"
        o=o& "<td>" & objItem.Description & "</td>"
        o=o& "<td>" & objItem.Disabled & "</td>"
        o=o& "<td>" & objItem.Domain & "</td>"
        o=o& "<td>" & objItem.FullName & "</td>"
        o=o& "<td>" & objItem.LocalAccount & "</td>"
        o=o& "<td>" & objItem.Lockout & "</td>"
        o=o& "<td>" & objItem.Name & "</td>"
        o=o& "<td>" & objItem.PasswordChangeable & "</td>"
        o=o& "<td>" & objItem.PasswordExpires & "</td>"
        o=o& "<td>" & objItem.PasswordRequired & "</td>"
        o=o& "<td>" & objItem.SID & "</td>"
        o=o& "<td>" & objItem.SIDType & "</td>"
        o=o& "<td>" & objItem.Status & "</td>"
        o=o& "</tr>"
    Next
    on error goto 0
    GetUsers = o
end function





Function getlcl(srvname)
    Set objComputer = GetObject("WinNT://" & srvname & "/Administrators,group")

 wscript.echo "Local Accounts on " & srvname
 wscript.echo "-------------------------------------------------"

    For Each objUser in objComputer.Members
        Wscript.Echo vbTab & objUser.Name
    Next

 wscript.echo "-------------------------------------------------"
end Function

function GetBuiltInAccount(strComputer)

    on error resume next
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    if err.number <> 0 then
        GetBuiltInAccout = "Get Object WMI [[Fail]]: " & err.number & ":: " & err.description
        err.clear
        exit function
    end if
    on error goto 0

    on error resume next
    Set colAccounts = objWMIService.ExecQuery("Select * From Win32_UserAccount Where Domain = '" & strComputer & "'")

    if err.number <> 0 then
        GetBuiltInAccout = "WMI_ExecQuery [[Fail]]: " & err.number & ":: " & err.description
        err.clear
        exit function
    end if
    on error goto 0

    on error resume next
    For Each objAccount in colAccounts
        if err.number <> 0 then
            GetBuiltInAccout = "WMI_ExecQuery_ForEachAccount [[Fail]]: " & err.number & ":: " & err.description
            wscript.echo "WMI_ExecQuery_ForEachAccount [[Fail]]: " & err.number & ":: " & err.description
            err.clear
            exit function
        end if
        on error goto 0

        If Left (objAccount.SID, 6) = "S-1-5-" and Right(objAccount.SID, 4) = "-500" Then
            GetBuiltInAccount = objAccount.Name
            exit function

        End If
    Next
end function

特别说明

对于域服务器:

创建一个包含以下行的批/ cmd文件(类似于admins.cmd):

cscript admin.vbs /servers:list.txt

对于独立服务器: - 下载CPAU.exe

创建批处理/ cmd文件(类似于adminsNDC.cmd)conteninig以下行:

CPAU -u%COMPUTERNAME%\ administrator -p mypassword -ex“cscript.exe Admins.vbs /servers:list.txt”-nowarn


- 本地管理员报告v1 -

说明

对于域服务器: 1 - 添加要扫描的服务器:“list.txt” 2 - 为以下文件中的“一个”运行/创建计划任务:  admins.cmd&lt; -------&gt; (您将获得:|主机名|用户|已禁用|已锁定|到期日期|标志| BuiltIn |) 3 - 检查html报告(您可以通过IE将其发送到Excel)

对于独立服务器:

1 - 将要扫描的服务器添加到:“list.txt” 2 - 将帐户名和密码信息编辑到adminsNDC.cmd中 3-run adminsNDC.cmd&lt; -------&gt; (您将获得:|主机名|用户|已禁用|已锁定|到期日期|标志| BuiltIn |) 3 - 检查html报告



获取服务器和帐户的报告后,可以使用此脚本执行密码更新。

Option Explicit

Dim strExcelPath, objExcel, objSheet, intRow, strUserDN, strPassword, comp
Dim objUser

' Spreadsheet file.
strExcelPath = "c:\List.xls"

' Bind to Excel object.
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Excel application not found."
    Wscript.Quit
End If
On Error GoTo 0

' Open spreadsheet.
On Error Resume Next
objExcel.Workbooks.Open strExcelPath
If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Spreadsheet cannot be opened: " & strExcelPath
    Wscript.Quit
End If
On Error GoTo 0

' Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)


intRow = 2
Do While objSheet.Cells(intRow, 1).Value <> ""
    comp = objSheet.Cells(intRow, 1).Value
    strUserDN = objSheet.Cells(intRow, 2).Value
    strPassword = objSheet.Cells(intRow, 3).Value
    On Error Resume Next
    Set objuser = GetObject ("WinNT://" & comp & "/" & strUserDN & ",user")
    If (Err.Number <> 0) Then
        On Error GoTo 0
        Wscript.Echo "Data NOT found: "
    Else
        objUser.SetPassword strPassword
        If (Err.Number <> 0) Then
            On Error GoTo 0
            Wscript.Echo "Password NOT set for: " & strUserDN
        Else


        End If
    End If
    intRow = intRow + 1
Loop

' Close the workbook.
objExcel.ActiveWorkbook.Close

' Quit Excel.
objExcel.Application.Quit

Wscript.Echo "Done"