使用powershell进行自动站点设置会引发多个错误

时间:2014-04-18 20:38:49

标签: powershell iis-7

首先我不是一个Powershell dev 所以我在这里有点过头了,如果这是BASH它现在已经完成了......

我收到了一些我不太了解的错误或错误,

Errors

我相信也许$ vars不能以我试图使用的方式解释? IDK的... 如果拥有powershell知识的人可以提供帮助并教育这个可怜的linux家伙,我会非常感激......

我希望这会根据提供的输入参数注册并加载一个带有IIS的新(空)站点。

这是我的PS1

# --------------------------------------------------------------------
# Define the variables.
# --------------------------------------------------------------------
Param (
    [string]$InetSiteName = $( Read-Host "Site Name" ),
    [int]$InetSitePort    = $( Read-Host "Site Port" ),
    [string]$InetPhysPath = $( $env:SystemDrive + "\inetpub\wwwroot" )
)


Import-Module "WebAdministration"

# --------------------------------------------------------------------
# Check for empty.
# --------------------------------------------------------------------
if(-not($InetSiteName)) { Throw "Site name cannot be empty..." }
if(-not($InetSitePort)) { Throw "Site port cannot be empty..." }
if(-not($InetPhysPath)) { Throw "Path name cannot be empty..." }

# --------------------------------------------------------------------
# Configure and register.
# --------------------------------------------------------------------
New-Item IIS:\Sites\$InetSiteName -physicalPath $InetPhysPath -bindings @{ protocol="http";bindingInformation=":"+$InetSitePort+":"+$InetSiteName } 
Set-ItemProperty IIS:\Sites\$InetSiteName -name applicationPool -value BenAPI
Start-WebSite $InetSiteName

# --------------------------------------------------------------------
# Run.
# --------------------------------------------------------------------
$webclient = New-Object Net.WebClient 
$webclient.DownloadString("http://localhost:$InetSitePort/");

$ie = New-Object -com InternetExplorer.Application 
$ie.Visible = $true 
$ie.Navigate("http://localhost:$InetSitePort/");

3 个答案:

答案 0 :(得分:0)

这是因为" WebAdministration"模块没有加载。
以管理员身份运行Powershell,以便" WebAdministration"模块导入成功。

enter image description here

然后转到保存脚本以运行脚本的位置

PS C:\> cd 'C:\Users\jDaniel\Documents'
PS C:\Users\jDaniel\Documents> .\iis-site.ps1

步骤指南Here - 如何在PowerShell中创建网站,Web应用程序,虚拟目录和应用程序池。

答案 1 :(得分:0)

这是我创建新网站的方式:

New-WebSite -Name $InetSiteName -port $InetSitePort -id 555 -PhysicalPath "$InetPhysPath" -HostHeader $InetSiteName

如果您需要更多绑定,可以使用:

New-WebBinding -Name $InetSiteName -Port $InetSitePort2 -HostHeader $InetSiteName2

答案 2 :(得分:0)

看起来真正的答案是我没有创建真正的游泳池,这里是我的更新代码......

谢谢大家

# --------------------------------------------------------------------
# Checking Execution Policy
# --------------------------------------------------------------------
#$Policy = "Unrestricted"
$Policy = "RemoteSigned"
If ((get-ExecutionPolicy) -ne $Policy) {
  Write-Host "Script Execution is disabled. Enabling it now"
  Set-ExecutionPolicy $Policy -Force
  Write-Host "Please Re-Run this script in a new powershell enviroment"
  Exit
}

# --------------------------------------------------------------------
# Define the variables.
# --------------------------------------------------------------------
[string]$InetSiteName = $( Read-Host "Site Name" )
[int]$InetSitePort    = $( Read-Host "Site Port" )
[string]$InetPhysPath = $( $env:SystemDrive + "\inetpub" )

$PoolName = "BenAPI"

# --------------------------------------------------------------------
# Loading IIS Modules
# --------------------------------------------------------------------
Import-Module "WebAdministration"

# --------------------------------------------------------------------
# Test or Create App Pool
# --------------------------------------------------------------------
if (!(Test-Path "IIS:\AppPools\$PoolName" -pathType container))
{
    # Create pool
    $appPool = New-Item "IIS:\AppPools\$PoolName"
    $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value "v4.0"
}

# --------------------------------------------------------------------
# Configure and register.
# --------------------------------------------------------------------
$WebRoot = New-Item "$InetPhysPath\$InetSiteName" -type Directory
New-Item IIS:\Sites\$InetSiteName -physicalPath $WebRoot -bindings @{ protocol="http";bindingInformation="*:"+$InetSitePort+":" }

Set-ItemProperty IIS:\Sites\$InetSiteName -name applicationPool -value BenAPI
Set-Content "$WebRoot\default.htm" "Test Page: $InetSiteName"

Start-WebSite $InetSiteName

# --------------------------------------------------------------------
# Run.
# --------------------------------------------------------------------
$ie = New-Object -com InternetExplorer.Application
$ie.Visible = $true
$ie.Navigate("http://localhost:$InetSitePort/");