Delete ASP.NET Temp files using windows services - asp.net

We are trying to create windows services, which’ll run every day at 3 AM in morning. The service basically, removes/delete ASP.NET Temp files.
We were able to find the command line argument to stop/start IIS and delete Temp files. Also, we created a simple windows services.
However, we have couple of issues:
Temporary Folder permission is reset to Read only and we need to manually change it to remove read only permission.
Based on google search we had, we aren’t sure: how to call DOS command from windows services at regular interval. Do we need to use batch file to execute DOS command and if so, then how to do that.
Below is command to stop/start iis and delete temporary files.
iisreset /stop
C:\WINDOWS\system32>del /S/Q "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temp
orary ASP.NET Files\"
iisreset /start
Can anyone guide us? Do we need to use window services or task scheduler to stop iis, remove folder permission and delete temp folder. Sample example/instruction will be helpful.

A windows service is a bit overkill for this. All you really need is a scheduled task to kick off to run your commands and set it to run under the instance of a user that has access to the local IIS_IUSRS group (or any with elevated permissions to that folder.)
For example with powershell:
invoke-command -scriptblock {iisreset /stop}
Get-ChildItem -Path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files" -Include . -Recurse | foreach { $_.Delete()}
invoke-command -scriptblock {iisreset /start}
The setup a scheduled task like so:
https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler
And set it to run as a user that has elevated permissions to that folder.
You would most likely want to also add a powershell web request to your sites once IIS comes back up or else the first person to hit the site again after you do this is in for an extended initial load time.

Related

Azure Pipelines delete files and rename after IIS publish

Using Azure Build Pipelines i'm trying to publish an ASP.NET WebForms website to IIS.
Everything works, except that I have several images in a folder of my website (e.g. /Images/1.jpg,2.jpg, etc..) and I would like to delete all images from that folder except 2.jpg and rename it to 1.jpg after I've deployed the website (or during the release pipeline if that's possible)
Is there any way to do this in Azure Pipelines?
Here you can take a look at a Catalog of the built-in tasks for build-release.
The Delete Files task states:
"Use this task in a build or release pipeline to delete files or folders from the agent working directory".
This may give you some ideas on how to achieve this.
I hope this help.
Comment on:
The Delete Files task states: "Use this task in a build or release pipeline to delete files or folders from the agent working directory".
and
".. from what I just tried, this can only delete files inside the artifact and not on the IIS Server."
That is correct for default behavior, but it has a parameter called SourceFolder, which has a default as "$(Build.SourcesDirectory)". When you specify the SourceFolder to be "C:" you can enter a pattern to delete anything on the server. Might be wiser to use a bit more elaborate path then just c:\ but it works. Just tried it on a pipeline where a file needed to be deleted not from the workingfolder but from the deployed IIS location.
PS in a release pipeline the default seems to be the $(System.DefaultWorkingDirectory) and not the $(Build.SourcesDirectory). Whether that is because the Build.SourcesDirectory defaults to System.DefaultWorkingDirectory in release pipelines or the task is being smart I don't know.

permission set in UNIX folder are not transmitted correctly to NFS share

we have a NFS share where folder in unix are mounted over a NFS windows server.
even after setting the permission to 775 on unix machine for some folder.
The same does not reflect when files are created in that folder by some java process.
so we have a folder like /nobackup/stream on unix machine mounted on nfs server
permission on unix machine
ls -ald /nobackup/stream
rwxrwxr-x owner group
we have an automation process writing result logs and sub directories to stream folder
for some weird reason the files are getting created with permission
rwxr-xr-x owner group
i.e write access to group is not present.
This is causing our automation to fail when at certain places a process running with group user privilege tries to update the files created with above permission
Initially the suspect was umask
so we set umask to 0002 in the perl process which starts automation
that did not help
Files.mkdir is being used to write the file
here the posix permission is correct ,umask is correct still the new files are not getting created with correct permission
also note that automation runs under cygwin shell if thats causing the trouble
How can I ensure that file permission is always set correctly
The problem is that the automation is running in cygwin. Those files are still written by the Windows NFS client, which has no clue how to interpret the permissions set in cygwin.
You need to set the default permissions in the Windows NFS client. You can do this from the command line with nfsadmin. Something like:
nfsadmin client [ComputerName] fileaccess=664
Source: https://technet.microsoft.com/en-us/library/cc754304(v=ws.11).aspx

How to find the physical directory of an application on IIS with NSIS

I'm building an installer with NSIS that will install multiple ASP.NET applications at the same time. I only need to find the physical directory of these applications on the server to install my files.
NsisIIS provides functions to return: web sites, virtual directory and app pools, but nothing to return a list of applications and their properties.
I already know the name of these applications, but the physical directories varies from server to server.
I was wondering if someone had a solution to this problem.
Thx
To solve this problem, you can use the APPCMD.exe, which can do the job for you. Just call it from NSIS like this:
Section "Test"
nsExec::ExecToStack 'C:\WINDOWS\system32\inetsrv\appcmd list vdir "Default Web Site/virtual_path" /text:physicalpath'
Pop $0 ;return value
Pop $1 ;printed text
DetailPrint "output from command: $1"
SectionEnd
You must after all call Pop twice, because ExecToStack stores the return value (%errorlevel%) above the text returned from executed command.

How to retain folder permission during website deployment?

I have a production website that, once built in TFS is re-deployed and updated using xcopy. The entire site (excluding the root directory) it deleted then the new site copied in. This works well.
We use a 3rd party charting package that creates images at runtime and then renders a link to them. In order to do this it needs write permissions to a browsable folder.
Unfortunately, every time we update the website the write permissions of IIS_USRS is lost. Is there any way to retain this?
I guess it depends on what operating system the server is running, and whether you are building on the same server as you're deploying to, or a remote one.
The simplest thing to do is to put your xcopy command into a batch file, and include something like the following after the xcopy:
cacls c:\[PathToWebsite]\[ChartImagesFolder] /E /G [AccountSiteRunsUnder]:C
Or a more up to date option (I've not used this, so my parameters may be off):
icacls c:\[PathToWebsite]\[ChartImagesFolder] /grant [AccountSiteRunsUnder]:M
Basically, either of those should give the user account that the site is running under modify (change) rights in the folder specified. This should be in addition to any existing rights on the folder, there are modifiers or switches to replace the existing rights.
If you are deploying to a remote server, you'll need some mechanism to run commands on there, we've found that PSExec works a treat (part of the PS tools from SysInternals).

How can I shell out of an ASP.NET page and execute a git command?

I want my ASP.NET page to shell out and execute git commands. I put the commands in a bat file which works:
REM cd to the git repo folder
cd c:\temp\mygitrepo
"c:\Program Files\Git\Bin\git.exe" show c090dc4b8b1b3512c1b5363c371e21d810d02f54 -- myfile.txt
If I run my .bat file from a cmd prompt, no problem.
If I run it using System.Diagnostics.Process.Start, I get this error:
RUNTIME_PREFIX requested, but prefix computation failed. Using static fallback
The error is coming from this git file: http://github.com/git/git/blob/master/exec_cmd.c
I use exactly the same technique to run svn.exe commands, no problem.
EDIT 1: From the thread here I've learned that msysgit installs some files in a location associated with the current user, me, instead of all users. The IIS web server is running under another user account. I tried copying some of the git files that caught my eye, like .gitconfig, to other users (in Documents and Settings). No luck. So, I have switched my focus to getting IIS to impersonate me when it launches the git command.
Is it actually an error, or is it just notifying you of a fallback? Based on the code linked, it appears that the command should still execute successfully, only using the static PREFIX rather than the RUNTIME_PREFIX.
If the command is indeed failing, you might want to make sure that any required environment variables are available when starting the process from .NET. You might also need to impersonate a different windows identity to run the command with proper permissions (I am assuming that the ASP.NET identity is restricted as much as possible.) The ProcessStartInfo object provides a few ways to configure the process, including the windows identity, the process verb, and environment variables. Make sure you are providing the proper context that your batch file requires before running it.

Resources