I have a question regarding restarting MS SQL Service under a different user, in other words, re-starting MSSQLService as a "Log on as" user instead of "Local System" account. By default MSSQLService starts as a local system account.
Now my problem is, that I install MSDE 2000 for my application. And I install it as a seperate instance with name "MyInstance". So after Install my service is called MSSQL$MyInstance.
so my process basically creates an NT user on the machine with administartors rights, stops the service, change the service configuration to my NT user account credentials and then starts it using scm. The whole process works fine on my machine running Win XP Pro v.2002 SP1. But it fails to work on Win XP Pro v2002 SP2.
on SP2 machine, service gets reconfigured because I can see the Log on as account being changed from Local System to MyNTUser. But when I try to start it every time error is "Log on failure". But when I go to service properties and change the Password to my NT users password service starts properly.
So i'm not sure what exactly is going on, because if reconfiguration process works for my machine, typically it should work on SP2 machine as well, as the notes on ChangeServiceConfig winapi method(requires Advapi32.dll) suggests that it will work for XP, NT and win 2002.
Does anyone has any idea why is that.
Code Snippets:
Create User:
DirectoryEntry AD = new DirectoryEntry("WinNT://" + machineName + ", computer");
DirectoryEntry newUser;
try {
newUser = AD.Children.Find(userName, "user");
DeleteNTUserAccount(userName);
newUser = AD.Children.Add(userName, "user");
}
catch (Exception) {
newUser = AD.Children.Add(userName, "user");
}
newUser.Invoke("Put", new object[] {"Description", userDescription});
newUser.Invoke("SetPassword", new object[] {userPWD});
newUser.CommitChanges();
DirectoryEntry group;
group = AD.Children.Find("Administrators", "group");
if (group != null)
group.Invoke("Add", new object[] {newUser.Path.ToString()});
group.Close();
newUser.Close();
AD.Close();
Code to Stop Service:
ServiceController sc;
TimeSpan timeout = new TimeSpan(0, 0, 60);
OperatingSystem os;
os = Environment.OSVersion;
if (os.Platform != PlatformID.Win32NT) {
txtInstallInfo.Text += "\r\n\tApplication is built only for NT, 2000, XP or Later";
this.Refresh();
retVal = false;
}
os = null;
sc = new ServiceController(serviceName);
if (sc.Status == ServiceControllerStatus.Running) {
if (sc.CanStop) {
txtInstallInfo.Text += "\r\n\tRestarting the " + serviceName + " service";
this.Refresh();
sc.Stop();
try {
sc.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
txtInstallInfo.Text += "\r\n\t" + serviceName + " service stopped successfully";
this.Refresh();
}
catch(TimeoutException) {
txtInstallInfo.Text += "\r\n\t" + serviceName + " service did not respond to the stop command in a timely fashion. Please Restart Installer at later time.";
this.Refresh();
retVal = false;
}
}
else {
txtInstallInfo.Text += "\r\n\t" + serviceName + " service cannot be stopped. Please Wait make sure no application is running.";
this.Refresh();
retVal = false;
}
}
else {
txtInstallInfo.Text += "\r\n\t" + serviceName + " service was not running.";
this.Refresh();
}
sc.Close();
sc = null;
Code to Re-Configure Service:
string UserName = "MyNTUser";
string UserPwd = "abc123";
int HandleToScm = OpenSCManager( null, null, ServiceControlAccessRights.AllAccess );
int HandleToSvc = OpenService( HandleToScm, ServiceName, ServiceSpecificAccessType.ChangeConfig );
if ( ChangeServiceConfig(
HandleToSvc,
ServiceControlType.OwnProcess,
ServiceStartType.AutoStart,
ServiceNoChange,
null,
null,
IntPtr.Zero,
null,
@.".\" + UserName,
UserPwd,
ServiceName ) ) {
CloseServiceHandle( HandleToSvc );
CloseServiceHandle( HandleToScm );
}
Code to Start Service:
scm -Action 1 -Service MSSQL$MyInstance -Silent 1 -SvcAccount .\MYNTUserAccount -SvcPwd MYNTAccountPassword
I've never seen this post before, yet it is dated May 2005. Bumping message...sql
No comments:
Post a Comment