0

Specific problem:

I'm calling Restart-WebAppPool with the name of a web application pool that verifiably exists. When I execute that command in the context of a script it errors with Cannot find path 'IIS:\AppPools\AppPoolName' because it does not exist.. When I execute the exact same command in powershell console it runs like a charm.

Context:

I load the WebAdministration module at the beginning of the script using Import-Module WebAdministration. I rely on other commands from that module in a number of spots in the script, all of which operate successfully. I am logging the name of the app pool I am trying to restart into a log file right before calling the command, and the name there is right as well as being correct in error message. I am retrieving the app pool name with (Get-ItemProperty -Path $SitePath).ApplicationPool where $SitePath has been used throughout the script to operate on the site in question.

Extra-annoyingly, immediately before trying to restart the app pool I reset the root directory of the application in question to a new physical path using (Set-ItemProperty -Path $SitePath -Name "PhysicalPath" -Value $fullAppPath ), which works perfectly, so I know WebAdministration is loaded (the most obvious reason for not being able to find the path of the app pool that I'm aware of).

The app pool name that I'm trying to restart (aside from restarting in a powershell command prompt) shows up in Get-ChildItem -Path "IIS:\AppPools"

What else am I missing that would cause WebAdministration not to be able to find that app pool?

Community
  • 1
  • 1
cori
  • 8,666
  • 7
  • 45
  • 81
  • What is the value of the `-Name` parameter you're passing to `Restart-WebAppPool`? A brief snippet actual code being used would be helpful. – Kev Feb 14 '16 at 02:36
  • well, the exact powerhell commands I'm using are in the question. One actual result of `(Get-ItemProperty -Path $SitePath).ApplicationPool` (I'm doing this on numerous web applications) is "WEB-JOBS-QA" so the actual command being executed is `Restart-WebApplication WEB-JOBS-QA`. That command works in the powershell command line but not in script and WEB-JOBS-QA is in the Application pools list returned by `Get-ChildItem -Path "IIS:\AppPools"` – cori Feb 14 '16 at 02:47
  • Strange...I've run pretty much the same code in a script and it works fine (see: https://gist.github.com/kevinkenny/6b482fb8b2fa828c7207) . What version of Windows/IIS? Which PowerShell version? – Kev Feb 14 '16 at 03:02
  • Sorry, also...is this being run over powershell remoting? – Kev Feb 14 '16 at 03:03
  • No problem @kev, in this case no - this is being run on the local machine. I'll be doing the same thing using `Microsoft.Web.Administration.ServerManager` on a remote machine at some point but first am trying to troubleshoot the local changes that seem like they ought to work. – cori Feb 14 '16 at 03:09
  • Ok..have tried this in Windows 2008R2, Windows 2012R2 and Windows 10 and all works fine. I'm somewhat at a loss here as to why you're encountering this. – Kev Feb 14 '16 at 03:48
  • yeah me neither. yesterday, for example, we ran 2 builds each on 4 different web applications. Every one (8 times in total) switched application root directory successfully and then immediately failed to restart the related app pool. Weird. – cori Feb 14 '16 at 04:09
  • I wonder if it's a timing issue where after you switch the physical path of the site the app pool is in some kind of transient state and unavailable momentarily because of this. Might be worth sticking a `sleep -seconds 5` just before the `Restart-WebAppPool` to see what happens....or stop the pool first, swap physical path then start the pool. – Kev Feb 14 '16 at 04:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103422/discussion-between-cori-and-kev). – cori Feb 14 '16 at 13:34
  • I encountered something similar with configuring IIS in powershell yesterday that may be related and be another route of enquiry for you. I was adding a new website and Web app with New-WebSite and New-WebApplication and then later trying to find that Site and Application using the features of Get-IISServerManager I noted that the Sites listed didn't include my newly added site. I noted that It wasn't until I started another session that it would be shown in the list of sites. It may be the case that the information is cached and needs refresh? – schnitty May 08 '19 at 23:33

0 Answers0