Get remote application pool state using Microsoft.Web.Administration - iis

I'm trying to get the state of an IIS 7.5 application pool using Microsoft.Web.Administration API, but get an exception:
System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2147020584
HResult=-2147020584
Message=The object identifier does not represent a valid object. (Exception from HRESULT: 0x800710D8)
Source=Microsoft.Web.Administration
I connect to a remote machine in a different domain using the following code:
string appHostConfigFile = "\\\\" + serverName + "\\c$\\windows\\system32\\inetsrv\\config\\applicationHost.config";
UNCAccess unc = new UNCAccess(#"\\" + serverName + "\\c$\\windows\\system32\\inetsrv\\config", <USER_NAME>, <DOMAIN>, <PASSWORD>);
server = new ServerManager(appHostConfigFile);
and then try to iterate through all application pools:
ApplicationPoolCollection applicationPools = server.ApplicationPools;
foreach (ApplicationPool pool in applicationPools)
{
Console.WriteLine(“Name: ” + pool.Name + “ State: “ + pool. State);
}
Now, the strange thing is that I do get the Name property correctly (and many other properties too) but the State property thoughts an exception.
Only when I tried connecting to the local machine (127.0.0.1), everything worked as expected.
So, can anyone tell me what I’ve been missing here? Is there any other way to connect to a remote IIS server on a machine outside of mine domain?
Thanks a lot!

I have successfully used Windows Impersonation to access a remote IIS instance using ServerManager.
using (ServerManager serverManager = new ServerManager($#"\\{computerName}\c$\windows\system32\inetsrv\config\applicationHost.config"))
{
ApplicationPoolCollection appPools = serverManager.ApplicationPools
}
This works with IIS 8 and IIS 7.5. I have not tested with other versions. I am using SimpleImpersonation (available via Nuget).

Related

Opening SqlConnection inside TransactionScope error

We have been trying to use a SqlConnection within a TransactionScope. When we build the site and try this database call we run into an error:
A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)
The error involved occurs on the line cnn.Open().
using (var scope = new TransactionScope())
using (var cnn = new SqlConnection(connectionString))
{
cnn.Open();
int result = cnn.QuerySingle<int>("SELECT 1");
Console.WriteLine(result);
}
We created a console application to figure out what was wrong and discovered that by changing our connection string keyword 'Pooling' from 'false' to 'true' allows this to run in the console application and successfully return our result.
We made the same change to our site connection string, the same error as before returns.
Is there any reason this code is not working?
I was under the assumption that the web.config was law, as viewing the file through the Kudu service showed my expected connection string, but apparently this isn't the case in Azure.
I discovered that Azure Publish Profile is overriding our web.config connection string, this override still contained 'Pooling=false'.
Removing this now allows our code to run as intended.
This blog post explains more:
"When this code runs on a developer’s local machine, the value returned will be the one from the web.config file. However when this code runs in Windows Azure Web Sites, the value returned will instead be overridden with the value entered in the portal"

Can't Connect to IIS 7.5 from another 7.5

I am trying to execute this command from a web application on sourceServer:
var mgr = ServerManager.OpenRemote(destServer)
but I receive this error:
UnAuthorized access wth a detail error: "Retrieving the COM class factory for remote component with CLSID {2B72133B-3F5B-4602-8952-803546CE3344} from machine failed due to the following error: 80070005"
I have full administrative rights on both servers.
I can issue that command from a console application no problem, but when I try it from the web application, I get the error!
I have tried enabling the remote management checkbox and started teh remote access auto connection manager and also tried updating the load user profile on the applicaiton pool from false to true.
I have searched so much to the point that all of my links are pink in color!
Any input is greatly appreciated.
I've decided to ditch the use of ServerManager.OpenRemote() and use the DirectoryEntry way:
DirectoryEntry root = new DirectoryEntry("IIS://server/W3SVC", username, password)
it is much simpler and straightforward.

SSIS package from ASP.net does not work on client but does on server

Description Of Environment
I have an SSIS task which executes from asp.net, the package gets data from excel files over UNC path and puts the data into a sql server databaase . I have deployed the SSIS to file system, it has windows authentication database connections and the IIS user has database access. I can log in as the AppPoolUser I have used to host the web application and open/modify the files in question so those basic permissions are there. Web App is compiled in x86.
When it works:
When run from web app in browser on server under my user or appPool user
When logged in as me on server running through BIDS
When logged in as AppPoolUser on server running through BIDS
When run from scheduled task on server using AppPoolUser
When it doesn't work:
When run from client browser i.e. not the machine that has IIS hosting the asp.net app
My Question
What is different about the client and server and how do I make it work? I was under the impression that when running the web app all connections go through the AppPool User so it should behave the same on any machine server included?
The Error returned to client browser
SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005. An OLE DB record is available. Source: "Microsoft Office Access Database Engine" Hresult: 0x80004005 Description: "The Microsoft Office Access database engine cannot open or write to the file '\\myserver\My folder\myfile.xlsx'. It is already opened exclusively by another user, or you need permission to view and write its data.". SSIS Error Code DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER. The AcquireConnection method call to the connection manager "MyExcelFileConnection" failed with error code 0xC0202009. There may be error messages posted before this with more information on why the AcquireConnection method call failed. component "Excel Source" (1) failed validation and returned error code 0xC020801C. One or more component failed validation. There were errors during task validation.
c# Code running SSIS Package
string pkgLocation;
Package pkg;
Application app;
DTSExecResult pkgResults;
string result = string.Empty;
string dtsErrors = string.Empty;
try
{
pkgLocation = packagePath;
app = new Application();
pkg = app.LoadPackage(pkgLocation, null);
pkgResults = pkg.Execute();
foreach (DtsError local_DtsError in pkg.Errors)
dtsErrors += " " + local_DtsError.Description;
result = pkgResults.ToString();
}
catch (Exception exception)
{
result = exception.Message;
}

Impersonation exception invoking DirectoryEntry.Invoke method

I have a frustrating problem trying to use the System.DirectoryServices.DirectoryEntry.Invoke() method to recycle app pools on a remote IIS server.
The basic context is two client machines and an IIS 7.0 server machine (Windows 2008 Server), myServer, all in the same Windows domain. I want to run code on the client machines to
recycle an AppPool on the IIS server.
Here's the relevant code snippet:
DirectoryEntry directoryEntry = new DirectoryEntry("IIS://myServer/W3SVC/AppPools/SomeAppPool", domainUserid, password, AuthenticationTypes.Secure);
directoryEntry.Invoke("Recycle", null);
From one client machine, the code runs successfully, but on the other client machine, the code throws an exception relating to impersonation (see below).
I'm logged in as the same domain user on both client machines, and use the same domain user information in the code.
I've checked the server-side Event Viewer and other logs to see if there's some obvious difference in how the request is processed on the server, and done a significant amount of googling without success.
Can anyone give a clue as to what to look for or what diagnostics I can run (on either the client machines or on the server machine) to determine why this is happening?
Thanks for any help! Martin
2011-08-10 22:35:39,478 [10] WARN - ActionRestartIIS: Exception restarting IIS System.Reflection.TargetInvocationException: Exception has been
thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x80070542): Either a required impersonation level was not provided,
or the provided impersonation level is invalid. (Exception from HRESULT: 0x80070542)
--- End of inner exception stack trace ---
at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)
In that fine tradition of answering one's own questions, here's what I learnt. I modified my code to use System.Management classes and these seem to work better across domains. Sample code below:
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Authority = "ntlmdomain:" + this.domain;
connectionOptions.Username = this.username;
connectionOptions.Password = this.password;
connectionOptions.EnablePrivileges = true;
connectionOptions.Authentication = AuthenticationLevel.PacketPrivacy;
ManagementScope managementScope = new ManagementScope(#"\" + this.iisserver + #"\root\microsoftiisv2", connectionOptions);
managementScope.Connect();
ManagementObject appPool = new ManagementObject(managementScope, new ManagementPath("IISApplicationPool.Name='W3SVC/AppPools/" + apppool + "'"), null);
appPool.InvokeMethod("Recycle", null, null);

ASP 80004005 error on IIS 7.5

We are trying to run an ASP application on windows 2008 R2 standard (64 bit processor & iis 7.5). ASP application connects to MS Access database. IIS is running fine and Asp is running fine. But when ASP code tries to connect to MS-Access DB, its giving 500 error.
FilePath C:\INETPUB\WWWROOT\XXXXXX\XXXXX_SECTIONS.ASP
LineNumber 14
CurrentStatement LevelTop.Open()
ErrorCode 80004005
Error is coming exactly while opening connection to MS Access DB.
We checked with Process Monitor utility. I have attached the log file of process monitor. We assumed its a permission issue and granted all permissions, but still we are getting same error.
We even tried giving administration group user as IIS user. But still getting same issue.
[Edit]
Ok thanks joel. I am newbie to this environment.
I checked this link and installed this http://www.microsoft.com/downloads/en/details.aspx?FamilyID=C06B8369-60DD-4B64-A44B-84B371EDE16D.
Now I am getting this error
*ErrorCode: 800a0e7a
Description: Provider cannot be found. It may not be properly installed *
this is my connection string
"Provider=Microsoft.ACE.OLEDB.12.0;DBQ=C:\inetpub\wwwroot\xxxxx\App_Data\xxxxx.mdb;Persist Security Info=False;"
Set ThisSection = Server.CreateObject("ADODB.Recordset")
ThisSection.ActiveConnection = ConnectionString
ThisSection.Source = "SELECT * FROM table WHERE ID = " + Replace(ThisSection__MMColParam, "'", "''") + ""
ThisSection.CursorType = 0
ThisSection.CursorLocation = 2
ThisSection.LockType = 1
ThisSection.Open() ->** getting error exactly at this line**
The error message is that the provider cannot be found.
Two sources of this error could be:
Access is not installed
Access 32bit is installed and IIS is looking for the 64bit version, or the other way around
There should be an error in your eventlog.
Also you could try a restart after the installation of the Access driver.

Resources