To use Scripting API for WMI in VB, you must reference the Microsoft WMI Scripting V1.1 Library to introduce several objects of the Scripting API for WMI Swbemlocator - used to get the SwbemServices object, which represents a local or remote computer. connection. SwbemService - represents a connection to the namespace, can be used to handle the part of the part SwbemObject - represent a separate class definition or an object instance swbemojbectset - Collection of SwbemObject below is a few objects of DNS WMI Provider MicrosoftDns_zone - To manage the area of the area on the DNS server, MicrosoftDNS_CNAMETYPE, MicrosoftDNS_MXTYPE, etc. Library - Setting and System Management - Windows Management Instrumentation (WMI) - SDK Document - WMI Reference - Scripting API for Wmidns WMI Provider path is MSDN Library - Network and Directory Service - Domain Name System (DNS) - -SDK Document - DNS WMI Provider - DNS WMI Classes The following is the code implementation Requirements Reference Microsoft Scripting Runtime and Microsoft WMI Scripting V1.1 Library, just examples of A, MX, and CNAME records, You can also extend the operation of other resource records, or the area of the area can be added, and the reference MSDN can be
Class DNSController
Private ObjService As Object
Private Declare Function GetversionEx Lib "kernel32" Alias "getversionExa" (LPVERSIONEXA "(LPVERSIONEXA" (LPVERSIONEXA "
Private Type OsversionInfo
DWOSVERSIONFOSIZE AS long
DWmajorversion As Long
DWMINORVERSION As Long
DWBUILDNUMBER AS Long
DWPLATFORMID AS Long
SZCSDVERSION As String * 128
Osname as string
End Type
Private function getWindowsVersion () As OsversionInfo
Dim Ver as OsVersionInfo
Ver.dwosversionInFoSize = 148
GetversionEx Ver
WITH VER
Select Case .dwplatformID
Case 1
Select Case .dwminorVersion
Case 0
. osName = "Windows 95"
Case 10
. osName = "Windows 98"
Case 90
. osName = "Windows Mellinnium"
End SELECT
Case 2
Select Case .dwmajorVersion
Case 3
. ospname = "Windows NT 3.51"
Case 4.OSName = "Windows NT 4.0"
Case 5
If .dwminorversion = 0 THEN
. osName = "Windows 2000"
Elseif.dwminorversion = 1 THEN
. osName = "Windows XP"
Else
. ospname = "windows 2003"
END IF
End SELECT
Case Else
. ospname = "failed"
End SELECT
End with
GetWindowsVersion = VER
END FUNCTION
'Judging the operating system, since the implementation of WMI is slightly different in 2003 and 2000, it is necessary to judge the operating system.
Private function iswin2k3 () as boolean
DIM V as OsversionInfo
v = getWindowsVersion ()
If v.osname = "Windows 2003" THEN
Iswin2k3 = true
Else
ISWIN2K3 = FALSE
END IF
END FUNCTION
'//
'// Connect to a DNS server
'//
'// server name, can be a computer name, or IP
'// Connect the username used by the server, if it is connected to this machine, please use ""
'// Connect the password used by the server, if it is connected to this machine, please use ""
Public Function Connect (Byval Struser AS Variant, Byval Struseword As Variant, Byref Errmsg As Variant) AS VARIANT
ON Error Goto LL
Connect = TRUE
Err.clear
Dim objlocator as wbemscripting.swbemlocator
Set objlocator = creteObject ("wbemscripting.swbemlocator")
Set objService = Objlocator.connect Server (struserver, "root / microsoftdns", strusername, strpassword)
ObjService.security_.impersonationLevel = 3
Connect = TRUE
EXIT FUNCTION
LL: Connect = FALSE
Errmsg = "Error 0X" & CSTR (HEX (Err.Number)) & ", error occurs when connecting to server" & strserver & ", specific information is" & vbcrlf & err.description
Set objlocator = Nothing
Set objService = Nothing
Err.clear
END FUNCTION
'//
'// Disconnect from the server
'//
Public Sub Disconnect ()
Set objService = Nothing
End Sub
'//
'// Create a zone function
'//
'// area name
The file name saved in '// area is generally "area name .dns"
'// Return error message
'//
Back to the operation is successful
Public Function Createzone (Byval Szonename As Variant, ByVal SDataFileName As Variant, Byref errmsg as variant) AS VARIANT
SET OBJINST = SELECTRRRR ("MicrosoftDns_zone", "ContainerName =" & Chr (34) & Szonename & Chr (34), ERRMSG)
IF errmsg <> "" ""
Createzone = false
EXIT FUNCTION
END IF
IF Objinst.count> 0 THEN
Errmsg = "This area already exists"
Createzone = false
END IF
Set objinst = Nothing
DIM OPARAMS AS New Dictionary
Oparams.Add "Zonename", Szonename
'This is because the ZONETYPE parameter of the Createzone function in Win2003 and Win2000 systems is inconsistent with the value of primaryzone in 2000. In 2003, it is 0
IF iswin2k3 () THEN
ZONETYPE = 0
Else
ZONETYPE = 1
END IF
Oparams.Add "ZoneType", ZoneType
CreateZone = Create ("MicrosoftDns_zone", "Createzone", Oparams, Errmsg
Set oparams = Nothing
END FUNCTION
'//
'// Delete a zone
'//
'// To delete the domain name
Public Function Deletezone (Byval Scontainername As Variant, Byref errmsg as variant) AS VARIANT
Deletezone = delete ("MicrosoftDns_zone", "ContainerName", Scontainername, Errmsg
END FUNCTION
'//
'// Add a record
'//
'// host name
'// Host IP corresponding to
'// domain name in the area
Public Function CreateArecord (Byval Shostname As Variant, Byval Sipaddress As Variant, ByRef Errmsg As Variant) AS VARIANT
If ShostName = "" ""
SownerName = Scontainername
Else
SownerName = ShostName & "." & scontainername
END IF
Set objinst = SELECTRR ("MicrosoftDns_ATYPE", "OwnerName =" & Chr (34) & SownerName & Chr (34), Errmsg) IF errmsg <> "" "
Createarecord = false
EXIT FUNCTION
END IF
IF Objinst.count> 0 THEN
Errmsg = "This record already exists"
Createarecord = false
END IF
Set objinst = Nothing
DIM OPARAMS AS New Dictionary
Oparams.Add "ContainerName", Scontainername
Oparams.Add "OwnerName", SOWNERNAME
Oparams.Add "ipaddress", SIPAddress
Createarecord = Create ("MicrosoftDns_ATYPE", "CreateInstanceFromPropertyData", Oparams, Errmsg
Set oparams = Nothing
END FUNCTION
'//
'// Modify A Record Information
'//
'// Host full name, www.mglz.net
'// Host IP corresponding to
Public Function ModifyAregurad (Byval SiPaddress As Variant, Byref Errmsg As Variant) AS VARIANT
DIM OPARAMS AS New Dictionary
Oparams.Add "ipaddress", SIPAddress
Modifyarecord = Modify ("MicrosoftDns_AType", "OwnerName", SownerName, "Modify", Oparams, Errmsg
Set oparams = Nothing
END FUNCTION
'//
'// Delete A Record Record
'//
'// Host full name, www.mglz.net
Public Function Deletearecord (ByVal SownerName As Variant, Byref Errmsg As Variant) AS VARIANT
Deletearecord = Delete ("MicrosoftDns_AType", "OwnerName", SownerName, Errmsg
END FUNCTION
'//
'// Add MX Record
'//
'// host name
'// domain name in the area
'// To turn to the mail server
'// priority
Public Function Createmxrecord, Byval SCONTAINERNAME AS VARIANT, BYVAL SMAILSERVER AS VARIANT, BYVAL SPREMENCE AS VARIANT, BYREF ERRMSG AS VARIANT AS VARIANT
If ShostName = "" ""
SownerName = ScontainerNameElse
SownerName = ShostName & "." & scontainername
END IF
SET OBJINST = SELECTRR ("MicrosoftDns_MXType", "OwnerName =" & Chr (34) & SownerName & Chr (34), Errmsg)
IF errmsg <> "" ""
CreatemxRecord = false
EXIT FUNCTION
END IF
IF Objinst.count> 0 THEN
Errmsg = "This record already exists"
CreatemxRecord = false
END IF
Set objinst = Nothing
DIM OPARAMS AS New Dictionary
Oparams.Add "ContainerName", Scontainername
If ShostName = "" ""
Oparams.Add "OwnerName", Scontainername
Else
Oparams.Add "OwnerName", Shostname & "." & scontainername
END IF
Oparams.add "preference", Spreference
Oparams.Add "Mailexchange", SmailServer
CreatemxRecord = Create ("MicrosoftDns_Mxtype", "CreateInstanceFromPropertyData", Oparams, Errmsg
Set oparams = Nothing
END FUNCTION
'//
'// Modify MX Record
'//
'// Host full name, www.mglz.net
'// To turn to the mail server
'// priority
Public Function Modifymxrecord (byval SownerName As Variant, Byval SMAILSERVER AS VARIANT, BYVAL SPREGERENCE AS VARIANT, BYREF Errmsg As Variant) AS VARIANT
DIM OPARAMS AS New Dictionary
Oparams.Add "Mailexchange", SmailServer
Oparams.add "preference", Spreference
ModifyMxRecord = Modify ("MicrosoftDns_Mxtype", "Ownername", SownerName, "Modify", Oparams, Errmsg
Set oparams = Nothing
END FUNCTION
'//
'// Delete MX Record
'//
'// Host full name, www.mglz.net
Public Function deletemxrecord (ByVal SownerName As Variant, Byref Errmsg As Variant) AS VARIANT
DeletemxRecord = delete ("MicrosoftDns_Mxtype", "OwnerName", SOWNERNAME, Errmsg) End Function
'//
'// Add alias
'//
'// alias
'// domain name in the area
'// Target Host Name
Public Function CreateCname (Byval SCONTAME AS VARIANT, BYVAL SPRIMARYNAME AS VARIANT, BYREF ERRMSG AS VARIANT) AS VARIANT
If ShostName = "" ""
SownerName = Scontainername
Else
SownerName = ShostName & "." & scontainername
END IF
Set Objinst = SELECTRR ("MicrosoftDns_CNameType", "OwnerName =" & Chr (34) & SownerName & Chr (34), Errmsg)
IF errmsg <> "" ""
CreateCName = false
EXIT FUNCTION
END IF
IF Objinst.count> 0 THEN
Errmsg = "This record already exists"
CreateCName = false
END IF
Set objinst = Nothing
DIM OPARAMS AS New Dictionary
Oparams.Add "ContainerName", Scontainername
If ShostName = "" ""
Oparams.Add "OwnerName", Scontainername
Else
Oparams.Add "OwnerName", Shostname & "." & scontainername
END IF
Oparams.Add "PrimaryName", SprimaryName
CreateCName = Create ("MicrosoftDns_cnameType", "CreateInstanceFromPropertyData", Oparams, Errmsg
Set oparams = Nothing
END FUNCTION
'//
'// Modify alias
'//
'// alias 全 称 方 方 方 方 方
'// Target Host Name
Public function modifycname (byval SownerName As Variant, ByVal SprimaryName As Variant, Byref Errmsg As Variant) AS VARIANT
DIM OPARAMS AS New Dictionary
Oparams.Add "PrimaryName", SprimaryName
ModifyCName = Modify ("MicrosoftDns_CNameType", "OwnerName", SownerName, "Modify", Oparams, Errmsg
Set oparams = Nothingend Function
'//
'// Delete an alias
'//
'// alias 全 称 方 方 方 方 方
Public function deletecname (ByVal SownerName As Variant, byref errmsg as variant) AS VARIANT
DeletecName = delete ("MicrosoftDns_CNameType", "OwnerName", SownerName, Errmsg
END FUNCTION
Private function create (byval stablename as string, byval methodname as difference, byref oparms as dictionary, byref errmsg as variant) as boolean
ON Error Goto LL
Set oprocess = ObjService.Get (stablename)
Set Oinparams = OPROCESS.METHODS_ (MethodName) .inparameters.spawnInstance_ ()
For Each Key in Oparms.Keys
Oinparams.properties_.item (key) .value = cstr (Oparms.Item (key))
NEXT
ObjService.execmethod Stablename, MethodName, Oinparams
Errmsg = ""
Create = TRUE
EXIT FUNCTION
LL:
CREATE = FALSE
Errmsg = err.description
END FUNCTION
Private function modify, byval sfieldname as string, byval sfieldname as string, byval methodname as difference, byref oparams as dictionary, byref errmsg as variant) AS Boolean
DIM SQUERY AS STRING
Squeery = "Select * from" & stablename & "where" & sfieldname & "= '" & sfieldValue & "'"
ON Error Goto LL
Set objinst = ObjService.execQuery (SQUERY)
For Each O in Objinst
Set Oinparams = O.Methods_ (MethodName) .inparameters.spawnInstance_ ()
For Each Key in Oparams.keys
Oinparams.properties_.item (key) .Value = cstr (Oparams.Item (key))
NEXT
O.execmethod_MethodName, Oinparams
NEXT
Errmsg = ""
Modify = TRUE
EXIT FUNCTION
LL:
MODIFY = FALSE
Errmsg = err.description
END FUNCTION
Private function delete (byval sfieldname as string, byval sfieldvalue as string, byref errmsg as variant) as booleandim squery as string
Squeery = "Select * from" & stablename & "where" & sfieldname & "= '" & sfieldValue & "'"
ON Error Goto LL
Set objinst = ObjService.execQuery (SQUERY)
For Each O in Objinst
O.Delete_
NEXT
Errmsg = ""
Delete = true
EXIT FUNCTION
LL:
DELETE = FALSE
Errmsg = err.description
END FUNCTION
Private function selectrr (byval recordtype as string, byval sfilterexpression as string, byref errmsg as variant) AS Object
ON Error Goto LL
Errmsg = ""
SQL = "Select * from" & recordtype
IF sfilterexpression <> "" "" ""
SQL = SQL & "Where" & SfilteRexpression
END IF
SET SELECTR = ObjService.execQuery (SQL)
Errmsg = ""
EXIT FUNCTION
Ll: errmsg = err.description
Set Selectrr = Nothing
Err.clear
END FUNCTION
END CLASS