0

I'm trying to manage my IIS instance through PowerShell.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration") | Out-Null
            $Iis = [Microsoft.Web.Administration.ServerManager]::OpenRemote($WebServer)
            $IisApp = $Iis.sites[$SiteName].Applications["/$WebApplicationName"]
            if(-not $IisApp)
            {
                $IisApp = $Iis.sites[$SiteName].Applications.Add("/$WebApplicationName", $WebApplicationFolder)
            }

This fails because it finds the wrong IIS instance on the target machine sometimes. How do I tell Microsoft.Web.Administration to use the real IIS and not IIS Express?

I've also tried fetching the latest Microsoft.Web.Administration through NuGet and it does no better. I've pulled down the assembly into a shared Nuget location (c:\nuget\packages\) and instead I load the assembly from there

add-type -Path "C:\nuget\packages\Microsoft.Web.Administration\lib\netstandard1.5\Microsoft.Web.Administration.dll"

and confirmed that ServerManager is coming from the correct assembly and it's no different.

I've uninstalled IIS Express from my machine and it works on my machine, but I can't uninstall IIS Express from every machine I wish to target.

How do I tell Microsoft.Web.Administration to focus on real IIS instead of IIS Express?

If IIS Express is installed, everything fails. If IIS Express is not installed, everything is fine.

Help?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pxtl
  • 880
  • 8
  • 18
  • 1
    You cannot use that NuGet package. https://blog.lextudio.com/whats-microsoft-web-administration-and-the-horrible-facts-you-should-know-b82f2c974da6 – Lex Li Jan 28 '20 at 16:40
  • Oh FFS. First I tried and failed with the official IIS Powershell module which is an absolute trash-fire, now I tried with the built-in version of Microsoft.Web.Administration but all the guides say "update and it will work" and now I find out that the nuget is also a disaster? I feel like a rat in a maze at this point. Everything *almost* works and then the rug gets yanked out. Should I just use AppCmd? – Pxtl Jan 28 '20 at 17:38
  • 1
    The situation is that IIS lost its place in today's Microsoft landscape, so its documentation is sparse. Your very first option should be the new IISAdministration cmdlets, https://blogs.iis.net/iisteam/introducing-iisadministration-in-the-powershell-gallery But even its installation process is badly written (unless you are on IIS 10). Then appcmd is the second. All other options are either not documented well, or simply won't work as I pointed out in the blog post. Enterprises might get assistance from Microsoft support if that happens to be your case. – Lex Li Jan 28 '20 at 18:28
  • 1
    It sounds like web.administration will connect IIS express if the application is not executed under administrator identity, then powershell will just connect IIS express. So have you tried to execute the powershell code under administrator permission? Besides, have you tried to load Microsoft.Web.Administration from C:\Windows\System32\inetsrv – Jokies Ding Jan 29 '20 at 03:20
  • Yeah, inetsrv solved it but for local only - open remote failed intermittently. Made do with that and PowerShell invoke-command remoting. – Pxtl Jan 30 '20 at 19:35

1 Answers1

1

Loading the Microsoft.Web.Administration from C:\Windows\System32\inetsrv fixed it. Remote invocation using OpenRemote doesn't work, so I had to use Invoke-SqlCommand to administer on remote machine.

Pxtl
  • 880
  • 8
  • 18