使用Powershell创建具有特定属性的IIS站点

时间:2018-04-19 18:53:07

标签: powershell iis

我必须使用相同的配置重复创建工作站点。目前,这是一个手动过程并且是时间密集的。我试图简化使用Powershell,但我几乎没有使用该语言的经验。我拼凑了一个脚本,该脚本由我在网络上找到的片段组成,但我似乎无法使安装正常工作。

我很高兴能够提供任何帮助。

" Set-WebConfigurationProperty -Filter" /system.webServer/security/authentication/windowsAuthentication" -Name Enabled -Value True -PSPath IIS:\"默认网站\ $ iisAppName"写入主机" Windows身份验证已启用$ iisAppName""它试图写一条非常奇怪的道路。

除此之外,该网站无法正常运行。从方向上有一些选择,我无法弄清楚如何完成。该网站似乎只是部分部署。

以下是安装步骤:

1   Extract the contents of TestSite.zip from the folder of the Install CD to the File Location recorded in Appendix I.
2   Launch IIS Manager.
3   Select Application Pools from the Connections section of the IIS Manager.
4   Double Click ASP.NET v4.5 application pool.
5   Select .NET Framework v4.0.30319 from the .NET Framework version: dropdown.
6   Click OK.
7   Right-click the ASP.NET v4.5 application pool and select Set Advanced Settings… from the context menu.
8   Select True from the Enable 32-Bit Applications dropdown.
9   Click OK.
10  Expand the Default Web Site. 
11  Right-click TestSite and select Convert to Application from the context menu.
12  Click Connect as...
13  Select the Specific user radial button and click Set…
14  Enter the Domain User Account as recorded in Appendix I into the User Name: textbox.
15  Enter the Domain User Password as recorded in Appendix I into the Password: textbox.
16  Enter the Domain User Password as recorded in Appendix I into the Confirm Password: textbox.
17  Click OK.
18  Click OK.
19  Click Select...
20  Select ASP.NET v4.5 from the Application Pool dropdown.
21  Click OK.
22  Click OK.
23  Double Click Default Document.
24  Move default.aspx to the top of the priority list.
25  Click the Back button.
26  Double Click Authentication.
27  Enable Windows Authentication.

这是我到目前为止的Powershell:

Import-Module WebAdministration
$iisAppPoolName = ".NET v4.5"
$iisAppPoolDotNetVersion = "v4.0"
$iisAppName = "TestSite"
$zipPath = "C:\Temp\Filename.Version_General.zip"
$directoryPath = "C:\inetpub\wwwroot\TestSite"
$appPath = "IIS:\Sites\Default Web Site\"
$ADDomain = "Domain"
$CmsUser = "Username"
$ADPassword = "Password"

#Unzip application to wwwroot folder
Write-Host "Unzipping $iisAppName Application to $directoryPath"
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
    param([string]$zipfile, [string]$outpath)

    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}

Unzip "$zipPath" "$directoryPath"
Write-Host "Zip file ($ZipPath) extracted to ($directoryPath)"

#Create Virtual Directory and then Convert to Application
Write-Host "Creating Virtual Directory and Converting to IIS Application"
New-WebVirtualDirectory -Site "Default Web Site" -Name "$iisAppName" -physicalPath "$directoryPath" 
ConvertTo-WebApplication "IIS:\Sites\Default Web Site\$iisAppName" -ApplicationPool "$iisAppPoolName"
Write-Host "$iisAppName has been Converted to an Application"


#navigate to the app pools root
cd IIS:\AppPools\

#check if the app pool exists
Write-Host "Verify if $iisAppPoolName already exists"
if (!(Test-Path $iisAppPoolName -pathType container))
{
    #create the app pool
    $appPool = New-Item $iisAppPoolName
    $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value "$iisAppPoolDotNetVersion"
}
Write-Host "$iisAppPoolName Verified"

#check if the site exists
Write-Host "Checking if $iisAppName already exists, if Not creating Site"
if (Test-Path $iisAppName -pathType container)
{
    return
}

#create the site
$iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=":80:" + $iisAppName} -physicalPath "$directoryPath"
$iisApp | Set-ItemProperty -Name "applicationPool" -Value "$iisAppPoolName"
Write-Host "$iisAppName Successfully Created"

#set Windows Authentication for the site
Write-Host "Enable windows authentication"
Set-WebConfigurationProperty -Filter "/system.webServer/security/authentication/windowsAuthentication" -Name Enabled -Value True -PSPath IIS:\"Default Web Site\$iisAppName"
Write-Host "Windows Authentication Enabled for $iisAppName"

#test for adding Username and Password to the Site
set-itemproperty "$AppPath$iisAppName" -name userName -value "$ADDomain/$CmsUser"
set-itemproperty "$AppPath$iisAppName" -name password -value "$ADPassword"

0 个答案:

没有答案