在遠程創建用戶

在擁有對遠程計算管理員權限時,創建一個用戶並將其加入到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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9932141/viewspace-374311/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值