Handles to the web application's physical directory and the parent site's physical directory are left open after running the New-WebApplication command. This results in not being able to manipulate the directories due to access is denied errors. Below is a transaction of commands that illustrates the issue. Handle is being used to detect that the handles are left open.
PS C:\Source\api\Tools\Deployment> c:\handle api
Handle v3.42
Copyright (C) 1997-2008 Mark Russinovich
Sysinternals - www.sysinternals.com
System pid: 4 3C8: C:\Windows\System32\setupapi.dll
System pid: 4 480: C:\Windows\System32\advapi32.dll
System pid: 4 4B0: C:\Windows\System32\dnsapi.dll
java.exe pid: 1608 374: C:\Program Files\apache-activemq-5.1.0\lib\web\jsp-api-2.1-6.1.9.jar
java.exe pid: 1608 384: C:\Program Files\apache-activemq-5.1.0\lib\web\slf4j-api-1.5.0.jar
java.exe pid: 1608 3D4: C:\Program Files\apache-activemq-5.1.0\lib\jaxb-api-2.0.jar
java.exe pid: 1608 3E0: C:\Program Files\apache-activemq-5.1.0\lib\stax-api-1.0.jar
java.exe pid: 1608 564: C:\Program Files\Java\jre6\lib\ext\sunmscapi.jar
handle.exe pid: 6856 8: C:\Source\api\Tools\Deployment
PS C:\Source\api\Tools\Deployment> New-WebSite "Api" -HostHeader "*" -PhysicalPath "D:\www\api"
Name ID State Physical Path Bindings
---- -- ----- ------------- --------
Api 3 D:\www\api http *:80:*
New-WebSite : The object identifier does not represent a valid object. (Exception from HRESULT: 0x800710D8)
At line:1 char:12
+ New-WebSite <<< c:\handle api
Handle v3.42
Copyright (C) 1997-2008 Mark Russinovich
Sysinternals - www.sysinternals.com
System pid: 4 3C8: C:\Windows\System32\setupapi.dll
System pid: 4 480: C:\Windows\System32\advapi32.dll
System pid: 4 4B0: C:\Windows\System32\dnsapi.dll
java.exe pid: 1608 374: C:\Program Files\apache-activemq-5.1.0\lib\web\jsp-api-2.1-6.1.9.jar
java.exe pid: 1608 384: C:\Program Files\apache-activemq-5.1.0\lib\web\slf4j-api-1.5.0.jar
java.exe pid: 1608 3D4: C:\Program Files\apache-activemq-5.1.0\lib\jaxb-api-2.0.jar
java.exe pid: 1608 3E0: C:\Program Files\apache-activemq-5.1.0\lib\stax-api-1.0.jar
java.exe pid: 1608 564: C:\Program Files\Java\jre6\lib\ext\sunmscapi.jar
handle.exe pid: 7132 8: C:\Source\api\Tools\Deployment
PS C:\Source\api\Tools\Deployment> New-WebApplication -Site "Api" -Name "v0" -PhysicalPath "D:\www\api\v0"
Name ApplicationPool EnabledProtocols PhysicalPath
---- --------------- ---------------- ------------
v0 DefaultAppPool http D:\www\api\v0
PS C:\Source\api\Tools\Deployment> c:\handle api
Handle v3.42
Copyright (C) 1997-2008 Mark Russinovich
Sysinternals - www.sysinternals.com
System pid: 4 3C8: C:\Windows\System32\setupapi.dll
System pid: 4 480: C:\Windows\System32\advapi32.dll
System pid: 4 4B0: C:\Windows\System32\dnsapi.dll
java.exe pid: 1608 374: C:\Program Files\apache-activemq-5.1.0\lib\web\jsp-api-2.1-6.1.9.jar
java.exe pid: 1608 384: C:\Program Files\apache-activemq-5.1.0\lib\web\slf4j-api-1.5.0.jar
java.exe pid: 1608 3D4: C:\Program Files\apache-activemq-5.1.0\lib\jaxb-api-2.0.jar
java.exe pid: 1608 3E0: C:\Program Files\apache-activemq-5.1.0\lib\stax-api-1.0.jar
java.exe pid: 1608 564: C:\Program Files\Java\jre6\lib\ext\sunmscapi.jar
powershell.exe pid: 492 55C: D:\www\api
powershell.exe pid: 492 7EC: D:\www\api
powershell.exe pid: 492 8DC: D:\www\api\v0
handle.exe pid: 5284 8: C:\Source\api\Tools\Deployment
PS C:\Source\api\Tools\Deployment>