PowerShell创建IIS网站
声明fuction
function AddMime($mime)
{}
调用function
注意没有括号,现声明函数再调用
AddMime 参数
声明变量
$waveapp = 'waveformdb'
输出文本变量
新行直接写字符串
"输出内容"
$变量
比较符号
-eq # 相等equal
-ne # 不等not equal
Examples
# 以管理员身份运行
#导入IIS管理模块
Import-Module WebAdministration
#添加扩展名 $mime为哈希表类型 如$mimes = @{".a"="application/stream";".b"="application/stream";".c"="application/stream";}
function AddMime($mime)
{
try
{
if($mimes -eq $null -or $mimes.count -le 0)
{
return
}
foreach($item in $mimes.Keys)
{
Write-Host "add MIME:$item"
$extension = get-webconfigurationproperty //staticcontent -name collection | ?{$_.fileExtension -eq $item}
if($extension -ne $null)
{
write-host "exist MIME"
}
else
{
add-webconfigurationproperty //staticcontent -name collection -value @{fileExtension=$item;mimeType=$mimes[$item]}
}
}
}
catch
{
Write-Error $_.Exception.Message
}
}
try{
#网站名称
$waveapp = 'waveformdb'
#物理地址
$physicalpath=read-host 'Enter waveformdb directory'
if([String]::IsNullOrEmpty($physicalpath)){
$physicalpath = 'C:\ProgramData\db'
}
"waveformdb directory $physicalpath"
#端口
$port = 8020
$waveWebsites = Get-Website $waveapp
if($waveWebsites -ne $null){
'exist webSite waveformdb'
'remove webSite waveformdb'
#删除waveformdb
Remove-WebSite -Name $waveapp
#'remove webAppPoolwaveformdb'
#删除应用程序池
#Remove-WebAppPool -Name $waveapp
}
'new website waveformdb'
New-Website -Name $waveapp -PhysicalPath $physicalpath -Port $port
'directory browse true'
#启用目录浏览
Set-WebConfigurationProperty -PSPath "IIS:\" -Filter /system.webServer/directoryBrowse -Name enabled -Value "true"
#开启网站
'start website waveformdb'
Start-Website -Name $waveapp
# 添加mime类型
$mimes = @{".dat"="application/octet-stream";".cfg"="application/octet-stream";}
AddMime $mimes
}catch
{
$ExceptionMessage = "Error in Line: " + $_.Exception.Line + ". " + $_.Exception.GetType().FullName + ": " + $_.Exception.Message + " Stacktrace: " + $_.Exception.StackTrace
$ExceptionMessage
}