Listing/Modifying Server Information with Visual Basic.NET and VBScript Using WMI - How to change your computer name using WMI
(Page 5 of 5 )
You can change your computer name using VB.NET and WMI as shown in the following:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Try
Dim classInstance As New ManagementObject( _
"root\CIMV2", _
"Win32_ComputerSystem.Name='SERVER'", _
Nothing)
Dim inParams As ManagementBaseObject = _
classInstance.GetMethodParameters("Rename")
inParams("Name") = "server2" 'rename to
inParams("Password") = "admin"
inParams("UserName") = "administrator"
Dim outParams As ManagementBaseObject = _
classInstance.InvokeMethod("Rename", inParams,
Nothing)
MessageBox.Show("ReturnValue: {0}", outParams
("ReturnValue"))
Catch err As ManagementException
MessageBox.Show("Error: " & err.Message)
End Try
End Sub
Note that I am using “InvokeMethod” to execute a WMI method (in this case “Rename”) dynamically. “inParams” (an object of ManagementBaseObject) is specifically used to pass parameters to the WMI method “Rename”. It mainly contains the required information to penetrate through the authentication and authorization (username and password) of security, along with the new computer name. We can get the result after executing the WMI method “Rename” into “outParams”.
You can achieve the same result using the following VBScript code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set objShare = objWMIService.Get("Win32_ComputerSystem.Name='SERVER'")
Set objInParam = objShare.Methods_("Rename").inParameters.SpawnInstance_()
objInParam.Properties_.Item("Name") = "server2"
objInParam.Properties_.Item("Password") = "admin"
objInParam.Properties_.Item("UserName") = "administrator"
Set objOutParams = objWMIService.ExecMethod("Win32_ComputerSystem.Name='SERVER'", "Rename", objInParam)
Wscript.echo "ReturnValue: " & objOutParams.ReturnValue
Summary
You can further delve into the classes of WMI and design your own application to manage almost every important aspect of the Windows OS. If you extend your skills to ASP.NET, then you can develop a web application which can manage your server information on the fly, without your presence in the office itself! I think you can imagine the same, if you further extend it to the Pocket PC level! Good luck.
Anyway, once again, I must be thankful to all the readers who appreciated and encouraged me to further expand the series in a much more useful and efficient manner. Thank you very much.
Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |