44

Currently I'm trying to run this powershell script:

Param (
    $websiteName,
    $physicalPath
)

import-module WebAdministration
$website = get-website | where-object { $_.name -eq "$websiteName" }
if($website -eq $null){
    New-webapppool -Name "$websiteName"
    Set-WebConfiguration -filter "/system.applicationHost/applicationPools/add[@name='$websiteName']" -PSPath IIS:\ -value (@{managedRuntimeVersion="v4.0"})
    New-website -Name "$websiteName" -PhysicalPath "$physicalPath" -ApplicationPool "$websiteName"
    start-website "$websiteName"
}

I use the following command to run the script:

& .\InstallWebsite.ps1 -websiteName "MyWebsite" -physicalPath "C:\TEST\MyWebsite"

Everything works fine, except for the last command inside of the if statement:

start-website "$websiteName"

When that command runs, I receive the following error, which I haven't been able to successfully troubleshoot:

Start-Website : Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7)
At line:1 char:14
+ start-website <<<<  "MyWebsite"
    + CategoryInfo          : InvalidOperation: (:) [Start-Website], COMException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.IIs.PowerShell.Provider.StartWebsiteCommand

I also have the following script to remove the website if it matters:

Param (
    $websiteName,
    $appPoolName
)

import-module WebAdministration
$website = get-website | where-object { $_.name -eq "$websiteName" }
if($website -ne $null){
    remove-website -Name "$websiteName"
    remove-webapppool -Name "$websiteName"
}

I run it with the following command:

& .\UninstallWebsite.ps1 -websiteName "MyWebsite" -appPoolname "MyWebsite"
Jarrett Coggin
  • 1,290
  • 2
  • 13
  • 14

4 Answers4

74

The error message:

Start-Website : Cannot create a file when that file already exists.

...usually means that you have a bindings conflict where there is already a site running on the same IP address, port and if you're using them, host header.

I'd check your new site bindings in IIS MMC then find out if something else is using exactly the same bindings.

Kev
  • 118,037
  • 53
  • 300
  • 385
  • I added a parameter to the script for the port, then passed that into the new-website command. I also removed the start-website command since it was no longer necessary. The script now works without issue. Thanks for this tip, Kev. – Jarrett Coggin Jun 25 '12 at 18:28
  • This was exactly my problem. In my case, it was the host headers; a misspelled variable name caused them all to be blank (and thus, duplicate). Thank you. – JamesQMurphy Feb 05 '15 at 05:45
  • I had this problem with IIS Express and changing the port number in Visual Studio was the easiest solution. – jpvantuyl Jul 05 '18 at 16:10
2

Without any websites in the IIS configuration, the same error occurred. Reason were additional, old records at the end of applicationHost.config. None of these were visible in IIS Manager. After removing these XML records, the problem was solved.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
cskwg
  • 790
  • 6
  • 13
  • please provide file paths etc. applicationHost.config appears in multiple places so nobody knows which one you were referring to. – K-Dawg Nov 22 '21 at 11:48
  • 2
    When talking about IIS Manager, it only can be the global one at C:\Windows\System32\inetsrv\Config\applicationHost.config – cskwg Nov 25 '21 at 07:18
  • Ah, I'm using both IIS and IIS Express so it makes sense that the multiple paths were due to that. Could you edit your answer? Then I can remove the downvote as it seems unfair now that you've explained and given the path. SO doesn't let me change the vote unless the answer is changed. – K-Dawg Nov 25 '21 at 08:07
  • 1
    @K-Dawg I've done the edit for you if you'd like to remove your downvote – Danny Beckett Jul 09 '23 at 03:24
1

I've got a problem with the same error. This happens when i'm trying to Add-WebBinding to my IIS site remotely, using Invoke-Command from different agent machines at time.

It's worked for me, maybe it helps someone too:

$Mutex = New-Object -TypeName System.Threading.Mutex($false, "Global\Mutex")    
if ($Mutex.WaitOne(300000)) {
   #For example
   #$Command = {
        #New-WebBinding -name $iis_site_name -Protocol https -HostHeader             
           #$iis_host_name -Port 443 -SslFlags 1
   #}
   #Invoke-Command -Command $Command
} else {
   Write-Warning "Timed out acquiring mutex!"
}
    
$Mutex.Dispose()
0

Enable detailed error trace to find the reason. In my case, it was a mimeType element that duplicated a default one.

Der_Meister
  • 4,771
  • 2
  • 46
  • 53