我正在尝试编写VBScript以从服务器删除特定用户帐户。 如果在服务器中发现用户“adm”和/或“ame”,脚本应该删除它们,并打印结果是否删除成功。
下面是我尝试编写的脚本,但是收到错误。任何人都可以帮我纠正这个问题吗?
Dim disuser, objNetwork, strComputer, objComputer
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
Set objComputer = GetObject("WinNT://" & strComputer)
On Error Resume Next
For Each disuser In "adm ame"
Call objComputer.Delete("user", disuser)
On Error GoTo 0
Next
'On Error res="fail"
答案 0 :(得分:3)
如果要将多个用户名作为列表传递,这很好,但是要使用Array
枚举它们,它们需要是可枚举的,换句话说,支持枚举的对象,如Collection
或{ {1}}。
Dim disuser, objNetwork, strComputer, objComputer
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
Set objComputer = GetObject("WinNT://" & strComputer)
On Error Resume Next
'Split list of space delimited usernames into an Array.
Dim list: list = Split("adm ame", Chr(32))
For Each disuser In list
'Clear previous error before checking if the Delete works.
Call Err.Clear()
Call objComputer.Delete("user", disuser)
If Err.Number = 0 Then
'Was successful do something
Else
'Failed do something
End If
Next
On Error GoTo 0
'On Error res="fail"