Programmatically enable (install) IIS - c#

Sometimes there is a PC that doesn't have IIS. Either it disabled or either it not installed. In this case I need to enable it myself according to those steps.
I'm trying to create application that will check if IIS is enabled (installed), and if not it will enable (install) it.
I tried to install IIS using .msi files from here, but it asking me to follow those stpes before the installation.
I tried to use Advanced Installer but apparently it installing the IIS 8.0 Express but still it keeps the IIS disabled.
What I need to do to enable IIS programmatically? It is also acceptable if I'll need to run an IIS installation file to make it done (I didn't find the right one).

You can install IIS via the command line. The following command will install IIS on Windows 8 (you can edit this to add/remove certain features. It's just a command I've used in the past):
PkgMgr:
start /w pkgmgr /iu:IIS-WebServerRole;IIS-WebServer;IIS-CommonHttpFeatures;IIS-StaticContent;IIS-DefaultDocument;IIS-DirectoryBrowsing;IIS-HttpErrors;IIS-ApplicationDevelopment;IIS-ISAPIExtensions;IIS-ISAPIFilter;IIS-NetFxExtensibility45;IIS-ASPNET45;IIS-NetFxExtensibility;IIS-ASPNET;IIS-HealthAndDiagnostics;IIS-HttpLogging;IIS-RequestMonitor;IIS-Security;IIS-RequestFiltering;IIS-HttpCompressionStatic;IIS-WebServerManagementTools;IIS-ManagementConsole;WAS-WindowsActivationService;WAS-ProcessModel;WAS-NetFxEnvironment;WAS-ConfigurationAPI
DISM:
START /WAIT DISM /Online /Enable-Feature /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-ASP /FeatureName:IIS-ASPNET /FeatureName:IIS-BasicAuthentication /FeatureName:IIS-CGI /FeatureName:IIS-ClientCertificateMappingAuthentication /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-CustomLogging /FeatureName:IIS-DefaultDocument /FeatureName:IIS-DigestAuthentication /FeatureName:IIS-DirectoryBrowsing /FeatureName:IIS-FTPExtensibility /FeatureName:IIS-FTPServer /FeatureName:IIS-FTPSvc /FeatureName:IIS-HealthAndDiagnostics /FeatureName:IIS-HostableWebCore /FeatureName:IIS-HttpCompressionDynamic /FeatureName:IIS-HttpCompressionStatic /FeatureName:IIS-HttpErrors /FeatureName:IIS-HttpLogging /FeatureName:IIS-HttpRedirect /FeatureName:IIS-HttpTracing /FeatureName:IIS-IIS6ManagementCompatibility /FeatureName:IIS-IISCertificateMappingAuthentication /FeatureName:IIS-IPSecurity /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-ISAPIFilter /FeatureName:IIS-LegacyScripts /FeatureName:IIS-LegacySnapIn /FeatureName:IIS-LoggingLibraries /FeatureName:IIS-ManagementConsole /FeatureName:IIS-ManagementScriptingTools /FeatureName:IIS-ManagementService /FeatureName:IIS-Metabase /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-ODBCLogging /FeatureName:IIS-Performance /FeatureName:IIS-RequestFiltering /FeatureName:IIS-RequestMonitor /FeatureName:IIS-Security /FeatureName:IIS-ServerSideIncludes /FeatureName:IIS-StaticContent /FeatureName:IIS-URLAuthorization /FeatureName:IIS-WebDAV /FeatureName:IIS-WebServer /FeatureName:IIS-WebServerManagementTools /FeatureName:IIS-WebServerRole /FeatureName:IIS-WindowsAuthentication /FeatureName:IIS-WMICompatibility /FeatureName:WAS-ConfigurationAPI /FeatureName:WAS-NetFxEnvironment /FeatureName:WAS-ProcessModel /FeatureName:WAS-WindowsActivationService
In C#, you can create a Process that executes this command like so:
string command = "the above command";
ProcessStartInfo pStartInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
Process p = new Process();
p.StartInfo = pStartInfo;
p.Start();

You tag your question with InstallShield so I mention that later versions of InstallShield have support for enabling windows features:
Enabling Windows Roles and Features During a Suite/Advanced UI Installation
That said, I don't typically like to do this because you are really be intrusive with the configuration of the PC. I prefer to author a check that the required features are installed and block if they aren't.
Another thought is that ASP.NET 5.0 now supports self hosting as have other technologies such as WCF in the past. It might make sense to simply ditch the need for IIS and kill the problem that way.

Regarding your experience with Advanced Installer. You ended up with IIS Express installed because you used our predefined support for prerequisites. You should have been using the predefined support to install Windows Feature Bundles.
Using this support you can easily select which OS feature should be enabled and also set custom conditions. On our YouTube channel you can find examples/tutorials:
in the following example you see exactly how IIS is configured for enabling
here is also a more generic video, with a walkthrough over the built-in support from Advanced Installer for enabling Windows Features

You can install IIS from command line. First you need to enable ASP.NET 3.5:
IIS-ASPNET;IIS-NetFxExtensibility;NetFx4Extended-ASPNET45
or 4.5:
IIS-ASPNET45;IIS-NetFxExtensibility45;NetFx4Extended-ASPNET45
After that you can install IIS8, basically like IIS7. Checkout the IIS7 instalation http://www.iis.net/learn/install/installing-iis-7/installing-iis-from-the-command-line

Related

C# LSP autocompletion client for neovim / vim8

I've tried several ways to make it work but there seems to be no easy way. Yes, there are a ton of plugins, configurations. But they do not work right as per Oct 2019.
OmniSharp-Vim client needs configuration, it covers only C# and it lists plugins integration that does not work anymore (try choosing it for linting in ALE).
YouCompeleteMe should work but it is large and seems bloated.
Deoplete don't have source for C# and configurations I found are out of date.
Coc.nvim does not even list C# and 'unofficial' configurations have issues (like this). Besides Coc.nvim seems to be an alien from VS Code.
LanguageClient-neovim I didn't find sensible configuration and it seems because C# LSP server needs .sln file.
So this seems that csharpers should go to VS (or Rider) and that is when MS proposed LSP. How do you make IDE like from nvim to work with C#?
Basically the client should start server like this and use LSP.
~/.cache/omnisharp-vim/omnisharp-roslyn/run -s <PATH TO SLN OR DIR>
I just got omnisharp / ale working successfully with a clean install. You may want to completely uninstall omnisharp (~\AppData\Local\omnisharp-vim or ~/.omnisharp) just in case you have old versions.
You didn't mention your OS; I have this working in both Windows 10 and Mac OS. If you're using Mac OS make sure you brew install libuv first.
My Environment
Windows 10 (v1903) and Mac OS 10.14.6
Vim 8.1.2244
dotnet core 3.1 - I'd expect 3.0 to work as well
Instructions
First off, I'm using vim-plug as my plugin manager to handle installation. I installed it in both Windows and Mac OS using the bash/powershell snippets in vim-plug's README.
Then I added the following to my vimrc (~\_vimrc on Windows, ~/.vimrc on Mac OS):
"vim-plug config
call plug#begin()
Plug 'OmniSharp/omnisharp-vim'
Plug 'dense-analysis/ale'
call plug#end()
" plugin config
let g:OmniSharp_server_stdio = 1
Restart vim, and run :PlugInstall. It will clone omnisharp and ale for you.
Next, find some C# solution, and ensure the solution builds at the commandline (e.g. dotnet build should complete without errors). You also need a SLN file if you don't already have one (dotnet new sln and then dotnet sln add MyProj.csproj)
Choose a C# file and open it in vim. You should see the following notification:
If the install doesn't autostart, you can start it with :OmniSharpInstall. The install takes a minute or two of downloading in a terminal window. After the installation is complete, reopen vim and execute :cd \path\to\my\solution to ensure the working directory inside vim is correct. Then open a file with e.g. :e MyProj\Program.cs.
The server will be started automatically; don't manually start it. I get a lot of syntax errors for the first few seconds while the server is starting, after that I don't have any errors.
To pull up the autocomplete, type something like Console. then hit Ctrl-x o:
The above screenshot has vim-airline for the bottom bar -- that's not part of omnisharp and isn't required.
The above screenshots are Windows, but it's also working fine in Mac OS:
My full vimrc is available here and the source code I'm testing with is available here.
So far here is my setting for this using Deoplete, OmniSharp and ALE (full config at https://github.com/artkpv/dotfiles/blob/master/.config/nvim/vimrc) :
" Install Deoplete and OmniSharp:
" - OmniSharp/omnisharp-vim " for LSP support (like start OmniSharp server) and code actions, etc
" - Shougo/deoplete.nvim " for better autocompletion
" - dense-analysis/ale " for highlights
function SetCSSettings()
" Use deoplete.
call deoplete#enable()
" Use smartcase.
call deoplete#custom#option('smart_case', v:true)
" Use OmniSharp-vim omnifunc
call deoplete#custom#source('omni', 'functions', { 'cs': 'OmniSharp#Complete' })
" Set how Deoplete filters omnifunc output.
call deoplete#custom#var('omni', 'input_patterns', {
\ 'cs': '[^. *\t]\.\w*',
\})
" ... then goes your mappings for :OmniSharp* functions, see its doc
endfunction
augroup csharp_commands
autocmd!
" Use smartcase.
" call deoplete#custom#option('smart_case', v:true)
autocmd FileType cs call SetCSSettings()
augroup END

My DISM command appears to run successfully but nothing installs

I'm trying to install IIS via DISM. When executing the command either via code or directly in cmd prompt, it appears to run fine, but IIS is never installed. I'm unsure where I went wrong. Sorry for the block of text in the code. I've tried running just one of the features with the same result and I need all of these installed for my purposes.
START /WAIT DISM /Online /Enable-Feature /FeatureName:IIS-IIS6ManagementCompatibility /FeatureName:IIS-ManagementConsole /FeatureName:IIS-Metabase /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-ASP /FeatureName:IIS-ASPNET /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-ISAPIFilter /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-DefaultDocument /FeatureName:IIS-IIS6ManagementConsole /FeatureName:IIS-StaticContent /FeatureName:IIS-WebServer /FeatureName:IIS-WebServerRole
Try running dism.exe without Start /wait, you will actually see the errors, IIS-IIS6ManagementConsole is not a known feature, there is no IIS6 management console in IIS7+. The next problem I got is with IIS-NetFxExtensibility, it is missing parent features, try adding the /all switch to also enable required features.

Running an installer from within C#

I need to write code to download and run a program, e.g. notepad++ (npp.5.9.3.Installer.exe) this can be found on the web.
I run it with the ProcessStartInfo class. However when I normally execute the notepad++ installer, it will show me a few steps before actually installing, like choose language, path etc.
Is there any way to programatically skip these steps, and install the software? I hope my question is clear. If it helps, I also attach the method that so far only starts the installer
private int RunFile()
{
ProcessStartInfo psi = new ProcessStartInfo(GetFileFullPath());
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.CreateNoWindow = true;
using (Process process = Process.Start(psi))
{
process.WaitForExit();
if (process.HasExited)
return process.ExitCode;
}
}
Shall I pass some arguments for this to work?
Thank you in advance.
Regards,
Use npp.5.9.3.Installer.exe /S for unattended installation of notepad++, and %ProgramFiles%\Notepad++\uninstall.exe /S for uninstall.
There are some installers which supports -s or -silent switches which means that when you install a software by passing -s switch to installer and it will silently install with default options. Try to find out whether your installer supports that or not
you have to drive the installation emulating the user. It is possible send kind of command(message) to the other window from a C# application
have a look at the below
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/345d85e8-cc5f-4508-b3f2-74ee43521169/
Interact with other desktop-applications in windows using C# winforms
A wellwritten installer have options for silent installs with no user interface. If the installer is an .msi file there are options that can be passed to msiexec to make a silent install.
For other install systems there are sometimes options to. Automating installations without user involvement is a common task for system administrators, so if you have questions on a specific installation package I would suggerst asking at ServerFault or AppDeploy. Unfortunately there are many bad installation programs out there that doesn't support silent install.
This will ONLY depend on the installer (npp.5.9.3.Installer.exe). You have to search if the installer provides options that can be used in command line, such as silentinstall.
EDIT: You can use the /S (capital S) option for Notepad++ to perform a silent install.

Management Studio 2008 Express Install issue

I have some trouble installing Management Studio 2008 Express through C#-Code.
The code looks like this:
using (Process MMSInstall = new Process())
{
var psi = new ProcessStartInfo(PathExe.FullName, "/qs /Features=SSMS /Action=Install");
MMSInstall.StartInfo = psi;
MMSInstall.Start();
MMSInstall.WaitForExit();
}
PathExe is a FileInfo-Instance.
But the installation always fails:
Exception type: Microsoft.SqlServer.Setup.Chainer.Workflow.NoopWorkflowException
Message:
No features were installed during the setup execution. The requested features may already be installed. Please review the summary.txt log for further details.
When installing via command prompt
C:\>SQLMANAGEMENTSTUDIO_X86_DEU.EXE /qs /Features=SSMS /Action=Install
everything works fine.
I looked through the logfiles (Detail.txt), and spottet a difference:
When running from the command prompt, 'Setting: MEDIALAYOUT' is set to 'Advanced' (pastebin.org/36222), when installing from my little C#-App it's set to 'Core' (pastebin.org/36221)
I tried to append /MEDIALAYOUT=Advanced to the ProcessStartInfo-Arguments in my code, but this options is ignored. I don't know what this parameter does, and I could not find any documentation about it.
Any ideas how to solve this or where to look for?
I am testing on Windows Vista Ultimate SP1
instead of calling the executable directly call %windir%\system32\cmd.exe
Cmd has a /C switch which allows you to pass in a command to run. So you'd pass in '/c "SQLMANAGEMENTSTUDIO_X86_DEU.EXE /qs /Features=SSMS /Action=Install"'
as a parameter.

Uninstall Command Fails Only in Release Mode

I'm able to successfully uninstall a third-party application via the command line and via a custom Inno Setup installer.
Command line Execution:
MSIEXEC.exe /x {14D74337-01C2-4F8F-B44B-67FC613E5B1F} /qn
Inno Setup Command:
[Run]
Filename: msiexec.exe; Flags: runhidden waituntilterminated;
Parameters: "/x {{14D74337-01C2-4F8F-B44B-67FC613E5B1F} /qn";
StatusMsg: "Uninstalling Service...";
I am also able to uninstall the application programmatically when executing the following C# code in debug mode.
C# Code:
string fileName = "MSIEXEC.exe";
string arguments = "/x {14D74337-01C2-4F8F-B44B-67FC613E5B1F} /qn";
ProcessStartInfo psi = new ProcessStartInfo(fileName, arguments)
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true
};
Process process = Process.Start(psi);
string errorMsg = process.StandardOutput.ReadToEnd();
process.WaitForExit();
The same C# code, however, produces the following failure output when run as a compiled, deployed Windows Service:
"This action is only valid for products that are currently installed."
Additional Comments:
The Windows Service which is issuing
the uninstall command is running on
the same machine as the code being
tested in Debug Mode. The Windows
Service is running/logged on as the
Local system account.
I have consulted my application logs
and I have validated that the
executed command arguments are thhe
same in both debug and release mode.
I have consulted the Event Viewer
but it doesn't offer any clues.
Thoughts? Any help would be greatly appreciated. Thanks.
Step 1: Check the MSI error log files
I'm suspicious that your problem is due to running as LocalSystem.
The Local System account is not the same as a normal user account which happens to have admin rights. It has no access to the network, and its interaction with the registry and file system is quite different.
From memory any requests to read/write to your 'home directory' or HKCU under the registry actually go into either the default user profile, or in the case of temp dirs, c:\windows\temp
I've come across similar problems in the past with installation, a customer was using the SYSTEM account to install and this was causing all sorts of permission problems for non-administrative users.
MSI log files aren't really going to help if the application doesn't appear "installed", I'd suggest starting with capturing the output of MSIINV.EXE under the system account, that will get you an "Inventory" of the currently installed programs (or what that user sees installed) http://blogs.msdn.com/brada/archive/2005/06/24/432209.aspx
I think you probably need to go back to the drawing board and see if you really need the windows service to do the uninstall. You'll probably come across all sorts of Vista UAC issues if you haven't already...
Thanks to those offering help. This appears to be a permissions issue. I have updated my service to run under an Administrator account and it was able to successfully uninstall the third-party application. To Orion's point, though the Local System account is a powerful account that has full access to the system -- http://technet.microsoft.com/en-us/library/cc782435.aspx -- it doesn't seem to have the necessary rights to perform the uninstall.
[See additional comments for full story regarding the LocalSystem being able to uninstall application for which it installed.]
This is bizarre. LocalSystem definitely has the privileges to install applications (that's how Windows Update and software deployment in Active Directory work), so it should be able to uninstall as well.
Perhaps the application is initially installed per-user instead of per-machine?
#Paul Lalonde
The app's installer is wrapped within a custom InnoSetup Installer. The InnoSetup installer, in turn, is manually executed by the logged in user. That said, the uninstall is trigged by a service running under the Local System account.
Apparently, you were on to something. I put together a quick test which had the service running under the LocalSystem account install as well as uninstall the application and everything worked flawlessly. You were correct. The LocalSystem account has required uninstall permissions for applications in which it installs. You saved the day. Thanks for the feedback!

Categories

Resources