I have followed the steps at Cannot read configuration file due to insufficient permissions
I also tried to manually set the rights to related files, but that didn't work, especially on appcmd.exe.
I am trying to update applicationHost.config to set IIS reset time dynamically, when my website loads on IIS. For this I am trying to execute the following code in global.asax file of my project:
string WindowsDir = Server.MapPath("~/Startup");
string command = WindowsDir + #"\Startup.cmd";
string outputFilePath = WindowsDir + #"\log.txt";
string arguments = String.Format(
"/c echo Startup task (Startup.cmd) executed at {0} >>\"{1}\"",
System.DateTime.UtcNow.ToString(),
outputFilePath);
// System.Diagnostics.Process.Start(command, arguments);
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = command;
startInfo.Arguments = arguments;
process.StartInfo = startInfo;
process.Start();
This works perfectly when I run my website in Visual Studio, but when I deploy it on IIS, it produces the following errors:
Prevent the IIS app pools from shutting down due to being idle:
appcmd set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00 /commit:apphost
ERROR ( message:Configuration error
Filename: redirection.config
Line Number: 0
Description: Cannot read configuration file due to insufficient permissions. )
Schedule IIS app pool recycles for 8:00 AM UTC time (1:00 AM MST):
appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.schedule.[value='08:00:00']" /commit:apphost
ERROR ( message:Configuration error Filename: redirection.config
Line Number: 0
Description: Cannot read configuration file due to insufficient permissions. )
Prevent IIS app pool recycles from recycling on the default schedule of 1740 minutes (29 hours):
appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.time:00:00:00 /commit:apphost
ERROR ( message:Configuration error
Filename: redirection.config
Line Number: 0
Description: Cannot read configuration file due to insufficient permissions. )
Then I tried to execute following code:
string WindowsDir = Server.MapPath("~/Startup");
string command = WindowsDir + #"\Startup.cmd";
string outputFilePath = WindowsDir + #"\log.txt";
string arguments = String.Format(
"/c echo Startup task (Startup.cmd) executed at {0} >>\"{1}\"",
System.DateTime.UtcNow.ToString(),
outputFilePath);
// System.Diagnostics.Process.Start(command, arguments);
var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = command;
startInfo.Arguments = arguments;
string name = "Administrator";
string pass = "password";
startInfo.Password = pass.Aggregate(new System.Security.SecureString(), (ss, c) => { ss.AppendChar(c); return ss; });
startInfo.UserName = name;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
process.StartInfo = startInfo;
process.Start();
Then it didn't work even from Visual Studio. I then reverted back to code that was working in Visual Studio (as mentioned above). And added the following tags in Web.config (inside <system.web> tag):
<authentication mode="Windows"/>
<identity impersonate="true" userName="Administrator" password="password"/>
But that didn't work when I ran the website on IIS (7.5). Any ideas how to make this work?
I had the same problem (I was using IIS 8, Windows 8, C#, VS 2013), by code sometimes you have'not permissions. I was executing:
string windir = Environment.GetEnvironmentVariable("windir");
string comando = windir +"\\System32\\inetsrv\\appcmd.exe set site /site.name:test /+bindings.[protocol='http',bindingInformation='*:80:mitest']";
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + comando);
And I get the Error: "Cannot read configuration file due to insufficient permissions"
At the end use the ServerManager class, first I reference this dll in my project:
C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll
then I use the code to manipulate the AppPools, Sites or Bindings:
using (ServerManager serverManager = new ServerManager())
{
if (serverManager.ApplicationPools == null)
return;
for (int i = 0; i < serverManager.Sites.Count; i++)
{
Microsoft.Web.Administration.ApplicationPoolCollection appPoolCollection = serverManager.ApplicationPools[i];
}
if (serverManager.Sites == null)
return;
for (int i = 0; i < serverManager.Sites.Count; i++)
{
Microsoft.Web.Administration.BindingCollection bindingCollection = serverManager.Sites[i].Bindings;
}
}
Related
I've got a dotnet core 6 project where I need to execute SSIS packages via dtexec.
So far I have the following code:
private void ExecutePackage()
{
var processOutput = string.Empty;
var processErrorOutput = string.Empty;
var command = #"/C dtexec /file ""C:\git\star\tests\Star.Shared.UnitTests\test-artifacts\TestPackage.dtsx""";
var process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = command;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
processOutput = process.StandardOutput.ReadToEndAsync().Result;
processErrorOutput = process.StandardError.ReadToEndAsync().Result;
if (processOutput != string.Empty)
{
_logger.LogInformation("{output}", processOutput);
}
if (processErrorOutput != string.Empty)
_logger.LogError("{errors}", processErrorOutput);
}
When running this via my unit test, it just seems to hang and I'm unsure as to why.
On one of my previous attempts to get this file to run I got the following message:
An error occurred trying to start process 'dtexec /file "C:\git\star\tests\Star.Shared.UnitTests\test-artifacts\Test_Package.dtsx' with working directory 'C:\git\star\tests\Star.Shared.UnitTests\bin\Debug\net6.0'. The system cannot find the file specified.
Which is telling me that the last time I ran this, it was looking in my tests bin folder for the package instead of where the package is stored.
Is there a setting that I'm missing / set wrong?
Were using octopus for deployment, the tentacle is running as "local system account" I would like the tentacle to add credentials for a diffrent account. However I have no luck i doing so.
So far i tried creating a c# program which starts a new process as the other user, and the calls the cmdkey.exe
private static void CallCmdKey(string runAsDomain, string runsAsUser, string runAsPass, string target, string user, string pass)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.Arguments = $"/generic:{target} /user:{user} /pass:{pass}";
proc.StartInfo.FileName = Environment.GetEnvironmentVariable("WINDIR") + "\\system32\\cmdkey.exe";
Console.Out.WriteLine(proc.StartInfo.Arguments);
proc.StartInfo.Domain = runAsDomain;
proc.StartInfo.UserName = runsAsUser;
proc.StartInfo.LoadUserProfile = true;
SecureString sec = new SecureString();
runAsPass.ToCharArray().ToList().ForEach(sec.AppendChar);
proc.StartInfo.Password = sec;
proc.StartInfo.WorkingDirectory = ".";
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
Console.Out.WriteLine("done");
}
But it fails with access denied.
Then i tried power shell and psexec like this:
$psexec = "C:\temp\psexec.exe"
Invoke-Command -ScriptBlock{&$psexec -accepteula -u $WEB02AP2User -p $GISWEB02AP2Pass cmd /c cmdkey /generic:ffff /user:mufasa /pass:yoyo}
but it fails with
Access is denied.
PsExec could not start cmd:
The remote script failed with exit code 5
For security reasons Im not allowed to change account for the tentacle service
How can i sovle this issue
I Was unable to find a solutions to this issue. Only workaround was to let the octopusservice run as a specific user account
I'm following the guidance of other SO articles on this topic, but I have been unable to find what I need. I have the following codeblock which launches - in this case - a known corrupted .msi (in order to test the retrieval of the standard error message):
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd.exe", "/c " + thirdPartyApp.Item2.InstallString);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
writeEvent(EventLogEntryType.Information, "Attempting to launch upgrade: " + thirdPartyApp.Item2.InstallString, "Third Party Update");
proc.Start();
string stderror = proc.StandardError.ReadToEnd();
proc.WaitForExit();
When manually launched, this .msi throws error code 1620 with message "This installation package could not be opened. Contact the application vendor to verify that this is a valid Windows Installer package."
While debugging, if I look at proc.ExitCode its value is 1620 - so it's capturing that correctly. I added the line string stderror = proc.StandardError.ReadToEnd(); to attempt to capture the error text, but after the process executes and errors out stderror has a value of "".
How can I grab the actual error text into a variable?
I'm setting local auditing policies from a C# .NET program that reads settings from a file then uses Process.Start() with 'cmd' to execute the commands. This way has worked in the past for everything that I've needed it to do (including this exact situation), but recently it's just started to mysteriously fail to set the policies.
Here's the code: (command is of the form "auditpol /set /subcategory:"blah" /success:enable")
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
string result = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
In debug in VS2013 it's applying the policies just fine and even on the same computer in the full on .exe it's applying just fine, but when it gets transferred to another computer it will not set the policies from the auditpol command. Anyone have any ideas what could be happening?
I want to create a simple application to launch another application under different credentials.
Both application are on my laptop which is not on the domain. I need to run SSMS using a domain user credentials.
When I use the following runas command, it works:
runas.exe /netonly /user:domain\username /password:mypass "C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Ssms.exe"
The following code IS NOT working :
var file = #"C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Ssms.exe";
var sspw = new SecureString();
foreach (var c in "mypass" sspw.AppendChar(c);
var proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(file);
proc.StartInfo.FileName = Path.GetFileName(file);
proc.StartInfo.Arguments = "";
proc.StartInfo.Domain = "domain";
proc.StartInfo.UserName = "username";
proc.StartInfo.Password = sspw;
proc.StartInfo.LoadUserProfile = true;
proc.Start();
The exception message is:
Logon failure: unknown user name or bad password