<p>I have an unusual situation in which I need a SharePoint timer job to both have local administrator windows privileges and to have <code>SHAREPOINT\System</code> SharePoint privileges.</p>

<p>I can get the windows privileges by simply configuring the timer service to use an account which is a member of local administrators. I understand that this is not a good solution since it gives SharePoint timer service more rights then it is supposed to have. But it at least allows my SharePoint timer job to run <code>stsadm</code>.</p>

<p>Another problem with running the timer service under local administrator is that this user won't necessarily have <code>SHAREPOINT\System</code> SharePoint privileges which I also need for this SharePoint job. It turns out that <code>SPSecurity.RunWithElevatedPrivileges</code> won't work in this case. Reflector shows that <code>RunWithElevatedPrivileges</code> checks if the current process is <code>owstimer</code> (the service process which runs SharePoint jobs) and performs no elevation this is the case (the rational here, I guess, is that the timer service is supposed to run under <code>NT AUTHORITY\NetworkService</code> windows account which which has <code>SHAREPOINT\System</code> SharePoint privileges, and thus there's no need to elevate privileges for a timer job).</p>

<p>The only possible solution here seems to be to run the timer service under its usual NetworkService windows account and to run stsadm as a local administrator by storing the administrator credentials somewhere and passing them to System.Diagnostics.Process.Run() trough the StarInfo's Username, domain and password.</p>

<p>It seems everything should work now, but here is another problem I'm stuck with at the moment. Stsamd is failing with the following error popup (!) (Winternals filemon shows that stsadm is running under the administrator in this case):</p>

<p><code>The application failed to initialize properly (0x0c0000142).</code> <br />
<code>Click OK to terminate the application.</code></p>

<p>Event Viewer registers nothing except the popup.</p>

<p>The local administrator user is my account and when I just run <code>stsadm</code> interactively under this account everything is ok. It also works fine when I configure the timer service to run under this account.</p>

<p>Any suggestions are appreciated :)</p>

[{'answer_id': 3741, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m not at work so this is off the top of my head, but: If you get a reference to the Site, can you try to create a new SPSite with the SYSTEM-UserToken?</p>\n\n<pre><code>SPUserToken sut = thisSite.RootWeb.AllUsers["SHAREPOINT\\SYSTEM"].UserToken;\n\nusing (SPSite syssite = new SPSite(thisSite.Url,sut)\n{\n    // Do what you have to do\n}\n</code></pre>\n'}, {'answer_id': 4566, 'author': 'axk', 'author_id': 578, 'author_profile': 'https://Stackoverflow.com/users/578', 'pm_score': 0, 'selected': False, 'text': '<p>Other applications if run this way (i.e. from a timer job with explicit credentials) are failing the same way with "The application failed to initialize propely". I just worte a simple app which takes a path of another executable and its arguments as parameres and when run from that timer job it fails the same way.</p>\n\n<pre><code>internal class ExternalProcess\n{\n    public static void run(String executablePath, String workingDirectory, String programArguments, String domain, String userName,\n                           String password, out Int32 exitCode, out String output)\n    {\n        Process process = new Process();\n\n        process.StartInfo.UseShellExecute = false;\n        process.StartInfo.RedirectStandardError = true;\n        process.StartInfo.RedirectStandardOutput = true;\n\n        StringBuilder outputString = new StringBuilder();\n        Object synchObj = new object();\n\n        DataReceivedEventHandler outputAppender =\n            delegate(Object sender, DataReceivedEventArgs args)\n                {\n                    lock (synchObj)\n                    {\n                        outputString.AppendLine(args.Data);\n                    }\n                };\n\n        process.OutputDataReceived += outputAppender;\n        process.ErrorDataReceived += outputAppender;\n\n        process.StartInfo.FileName = @"C:\\AppRunner.exe";\n        process.StartInfo.WorkingDirectory = workingDirectory;\n        process.StartInfo.Arguments = @"""" + executablePath + @""" " + programArguments;\n\n        process.StartInfo.UserName = userName;\n        process.StartInfo.Domain = domain; \n        SecureString passwordString = new SecureString();\n\n        foreach (Char c in password)\n        {\n            passwordString.AppendChar(c);\n        }\n\n        process.StartInfo.Password = passwordString;\n\n        process.Start();\n\n        process.BeginOutputReadLine();\n        process.BeginErrorReadLine();\n\n        process.WaitForExit();\n\n        exitCode = process.ExitCode;\n        output = outputString.ToString();\n    }\n}\n</code></pre>\n\n<p>AppRunner basically does the same as the above fragment, but without username and password</p>\n'}, {'answer_id': 5938442, 'author': 'Partha', 'author_id': 744886, 'author_profile': 'https://Stackoverflow.com/users/744886', 'pm_score': 0, 'selected': False, 'text': '<p>The SharePoint Timer jobs runs with the SharePoint Firm Admin credentials since, the information get into the SharePoint Config Database. Thus the application pool will not have the access.</p>\n\n<p>For testing the timer job in dev environment, we can temporarily change the application pool account to the application pool account being used for Central Administration.</p>\n'}]
['https://Stackoverflow.com/questions/3739', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']