hsimah hsimah - 2 months ago 23
C# Question

Computer name from ServerManager Site and ApplicationPool

I am writing some PowerShell cmdlets in C# to access our production servers and report on the IIS/Service state.

The

ServiceController
class has a
MachineName
property, but neither
Site
nor
ApplicationPool
classes have this.

How would I go about returning the computer name alongside these objects (i.e.
Get-Service | select MachineName,Status,DisplayName
)? I considered extending the classes, but they are sealed and I don't wish to spend time implementing this solution.

Does anyone with more skill in writing cmdlets have an idea how I can easily accomplish this?

Answer

I simply wrote a small wrapper for the classes I wanted. It's not perfect but it's also not important enough to spend time on.

public class MyApplicationPool
{
    public MyApplicationPool(ApplicationPool appPool, string machineName)
    {
        Name = appPool.Name;
        MachineName = machineName;
        State = appPool.State;
        Cpu = appPool.Cpu;
        QueueLength = appPool.QueueLength;

        ApplicationPool = appPool;
    }

    public string MachineName { get; }
    public string Name { get; }
    public ObjectState State { get; }
    public long QueueLength { get; }
    public ApplicationPoolCpu Cpu { get; }

    public ObjectState Start()
    {
        return ApplicationPool.Start();
    }

    public ObjectState Stop()
    {
        return ApplicationPool.Stop();
    }

    public ObjectState Recycle()
    {
        return ApplicationPool.Recycle();
    }

    private ApplicationPool ApplicationPool { get; }
}
Comments