Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

iis 7 - "Cannot create a file" error when running Start-Website command in Powershell

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:TESTMyWebsite"

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"
question from:https://stackoverflow.com/questions/11162857/cannot-create-a-file-error-when-running-start-website-command-in-powershell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...