Can We Manage SQL Server 2000 With WMI and Visual Basic.NET? - How do you change the password of an SQL Server Login using WMI?
(Page 4 of 5 )
The previous section gave you only the list of logins available through your SQL Server 2000 instance. Now, I would like to extend this to modifying the password of a particular SQL Server login using WMI. Do you find this very interesting? Good, then let us follow the code now:
Private Sub btnChangePassword4Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangePassword4Login.Click
Dim PrivateLateBoundObject As ManagementObject
PrivateLateBoundObject = New ManagementObject(Nothing, New ManagementPath("\\server\root\MicrosoftSQLServer:
MSSQL_Login.SQLServerName=""(local)"
",Name=""sa"""), Nothing)
Dim inParams As System.Management.ManagementBaseObject = Nothing
inParams = PrivateLateBoundObject.GetMethodParameters("SetPassword")
inParams("OldPassword") = "sa"
inParams("NewPassword") = ""
Dim outParams As System.Management.ManagementBaseObject = PrivateLateBoundObject.InvokeMethod("SetPassword", inParams, Nothing)
MessageBox.Show("Password changed succesfully")
End Sub
From the above code, if you observe the “ManagementPath”, I included even the “system administrator” login, which is necessary for the privileges (as part of SQL Server security). To change the password of an existing login, we need to provide the “new password” along with the “old password” as part of the parameters to “SetPassword” (which is a WMI method to modify the password of SQL Server login).
“inParams” (which is of type System.Managment.ManagmentBaseObject) is mainly used to pass parameters (input parameters) to the “SetPassword” method (WMI method) dynamically. Similarly, “outParams” generally contains the result of method execution.
In the above script, I again worked with “InvokeMethod”, which is used to execute a WMI method dynamically.
Next: Summary >>
More MS SQL Server Articles
More By Jagadish Chaterjee