在遠端建立使用者

hexiaomail發表於2008-07-07

在擁有對遠端計算管理員許可權時,建立一個使用者並將其加入到Administrators組中,在兩個小時後自動刪除。
' The user and password of Administratoruse what use user credential
Username = "Administrator"

Password = "Fih123456"

' Create new user and password
strUser = "Admin2"
strPassword = "Test123456"

' Input servername(not IP)
strComputer=InputBox(Chr(10) & Chr(10) & "Please Input Server Name ( Not IP!):","Input Name")

If (Err.Number<>0) Then
        MsgBox("An error has occurred. " & Err.Description)
End If

Set cont = Nothing

' Test the server  whether is reachable
Set bjPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
    ExecQuery("select * from Win32_PingStatus where address = '"_
        & strComputer & "'")
For Each objStatus in objPing
    If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
        WScript.Echo("machine " & machine & " is not reachable")  
    End If
'    WScript.Echo("machine:" & objStatus.Address)
Next

' get WinNT object
Set dso = GetObject("WinNT:")

' Insert code securely.
' Supply full credentials to initiate a server connection.
Set colAccounts = dso.OpenDSObject( _
    "WinNT://" & strComputer , _
    strComputer & "\" & Username, _
    Password, _
    ADS_SECURE_AUTHENTICATION + ADS_SERVER_BIND)

'Set colAccounts = GetObject("WinNT://" & strComputer & ",computer")

' Add an user and set password
Set bjUser = colAccounts.Create("user", strUser)

objUser.SetPassword strPassword

'objUser.AccountExpirationDate = NewDate

objUser.SetInfo

If (Err.Number<>0) Then
        MsgBox("An error has occurred. " & Err.Description)
End If


'Add an user to Administrators Group
Set bjGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
Set bjUser = GetObject("WinNT://" & strComputer & "/" & strUser & ",user")
objGroup.Add(objUser.ADsPath)

If (Err.Number=0) Then
        MsgBox "An user is already created and add to Users Group. " & Chr(13) &_
          "Please write down user and password!" & Chr(13) & Chr(13) &_
    "new user:  " & strUser & Chr(13) & "Password:  " & strPassword , 0 , "New User"
End If

'MsgBox("new user:" & strUser)
'MsgBox("Password:" & strPassword)

' Sleep 2 hours
WScript.Sleep 60000

'Remove user from Administrators Group
'strComputer = "cn111460"
Set bjGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
Set bjUser = GetObject("WinNT://" & strComputer & "/" & strUser & ",user")

objGroup.Remove(objUser.ADsPath)

If (Err.Number<>0) Then
        MsgBox("An Error has occurred:" & Err.Description)
End If

'Delete user
'strComputer = "cn111460"
'Set colAccounts = GetObject("WinNT://" & strComputer & ",computer")
colAccounts.Delete "user", "Admin2"
'colAccounts.SetInfo
If (Err.Number=0) Then
        MsgBox "An user is remove from group." & Chr(13) & Chr(13) &_
  strUser & " already Deleted!" , 0 , "Delete User"
End If
Set cont = Nothing

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9932141/viewspace-374311/,如需轉載,請註明出處,否則將追究法律責任。

相關文章