现在微软积极地拥抱Linux平台,并推出了net core以实现跨平台技术,服务器也提供无UI的版本,以减少服务器的开销,从而提高服务器的处理能力。这系列的举动都说明微软的服务器端的改革以及来了。传统的windows平台主要还是以UI界面为中心,管理着IIS的配置。但是现在这条路已经行不通了,很多云平台也提供了无UI版本的windows服务器,因为无UI的版本更加便宜,所以这几乎成为我们的首选。是时候要掌握用命令行管理IIS了。在网上找了半天没有很好的教程,那就自己写一份吧,以后就不用再整理相关知识了,直接看这篇文章应该就可以了。
首先IIS里面管理的对象大概有:Application pool,Web site,Application,Virtual directory等等。我们就以这几个对象为核心进行分析。一般我们最常用的操作有:查看、新建、停止、运行、重启、编辑属性、重命名、移除等。所以我们就大概实现这些操作,应该可以完成我们日常的维护。命令行管理IIS的方式现在比较流行的是Powershell,还另外一种方式,我这里就不讨论了。今天我主要还是分享使用Powershell来管理IIS。
准备环境:安装powershell
代码语言:javascript复制powershell
Import-Module WebAdministration
1. Powershell对app pool管理
1.1 查看:
代码语言:javascript复制Get-ChildItem –Path IIS:AppPools
Get-ItemProperty –Path IIS:AppPoolsMyAppPool | select *
Get-WebAppPoolState MyAppPool
try{
$list = @()
foreach ($webAppPool in get-childitem IIS:AppPools)
{
$name = "IIS:AppPools" $webapp.name
$item = @{}
$IISPoolPath = "IIS:AppPools" $webAppPool.name
$item.WebAppPoolName = $webAppPool.name
$item.IISPoolPath = $IISPoolPath
$item.Version = (Get-ItemProperty $name managedRuntimeVersion).Value
$item.State = (Get-WebAppPoolState -Name $webAppPool.name).Value
$item.UserIdentityType = $webAppPool.processModel.identityType
$item.Username = $webAppPool.processModel.userName
$item.Password = $webAppPool.processModel.password
$obj = New-Object PSObject -Property $item
$list = $obj
}
$list | Format-Table -a -Property "WebAppPoolName", "IISPoolPath", "Version", "State", "UserIdentityType", "Username", "Password"
}catch
{
$ExceptionMessage = "Error in Line: " $_.Exception.Line ". " $_.Exception.GetType().FullName ": " $_.Exception.Message " Stacktrace: " $_.Exception.StackTrace
$ExceptionMessage
}
1.2 新建:
代码语言:javascript复制New-Item –Path IIS:AppPoolsMyAppPool
1.3 停止:
代码语言:javascript复制Stop-WebAppPool -Name MyAppPool
1.4 运行:
代码语言:javascript复制Start-WebAppPool -Name MyAppPool
1.5 重启:
代码语言:javascript复制ReStart-WebAppPool -Name MyAppPool
1.6 编辑属性:
代码语言:javascript复制Get-ItemProperty –Path IIS:AppPoolsMyAppPool | select *
Set-ItemProperty -Path IIS:AppPoolsMyAppPool -Name managedRuntimeVersion -Value v4.0
1.7 重命名:
代码语言:javascript复制Set-ItemProperty -Path IIS:AppPoolsMyAppPool -Name name -Value MyAppPool2
Set-ItemProperty -Path IIS:AppPoolsMyAppPool2 -Name name -Value MyAppPool
1.8 移除:
代码语言:javascript复制Remove-WebAppPool -Name MyAppPool
2. Powershell对web sites管理
2.1 查看:
代码语言:javascript复制get-childitem -path IIS:
Get-IISSite
Get-IISSite MyWebApp
Get-Website
Get-Website MyWebApp
Get-Website –Name MyWebApp
get-Item IIS:SitesMyWebApp
Get-ItemProperty –Path IIS:SitesMyWebApp | select *
try{
$allWebSites = Get-Website
$websitelist=@()
foreach($website in $allWebSites){
$websitepath="IIS:sites" $website.Name
$siteItem = @{}
$siteItem.SiteName = $website.Name
$siteItem.AppPool = (Get-ItemProperty $websitepath | select *).applicationPool
$siteItem.ElementTagName = (Get-ItemProperty $websitepath | select *).ElementTagName
$obj = New-Object PSObject -Property $siteItem
$websitelist = $obj
}
$websitelist | Format-Table -a -Property "SiteName", "AppPool", "ElementTagName"
}catch
{
$ExceptionMessage = "Error in Line: " $_.Exception.Line ". " $_.Exception.GetType().FullName ": " $_.Exception.Message " Stacktrace: " $_.Exception.StackTrace
$ExceptionMessage
}
2.2 新建:
代码语言:javascript复制New-Website –Name MyWebApp –PhysicalPath D:apidd
2.3 停止:
代码语言:javascript复制Stop-Website –Name MyWebApp
2.4 运行:
代码语言:javascript复制Start-Website –Name MyWebApp
2.5 重启:
代码语言:javascript复制Stop-Website –Name MyWebApp
Start-Website –Name MyWebApp
2.6 绑定:
代码语言:javascript复制Get-Website -Name MyWebApp
Get-WebBinding -Name MyWebApp
(Get-Website -Name MyWebApp).bindings.Collection
Set-WebBinding -Name 'MyWebApp' -BindingInformation "*:80:" -PropertyName Port -Value 81
New-WebBinding -Name MyWebApp -Protocol http -Port 82
//SSL bindings ??不确定
get-childItem IIS:SslBindings
$cert = Get-ChildItem cert:localmachinemy
$bindingInfo = "IIS:SSLBindings*!445"
$cert | Set-Item -Path $bindingInfo
2.7 编辑属性:
代码语言:javascript复制Get-ItemProperty –Path IIS:SitesMyWebApp | select *
Set-ItemProperty -Path IIS:SitesMyWebApp -Name enabledProtocols -Value http
2.8 重命名:
代码语言:javascript复制Rename-Item 'IIS:SitesMyWebApp' 'MyWebApp2'
2.9 移除:
代码语言:javascript复制Remove-WebSite -Name MyWebApp2
3. Powershell对applications管理
3.1 查看:
代码语言:javascript复制Get-WebApplication
Get-WebApplication -Site MyWebApp
Get-WebApplication -Site MyWebApp| select *
Get-WebApplication -Name testApp
Get-WebApplication -Name testApp| select *
Get-WebConfigurationProperty -Filter "//sites/site[@name='MyWebApp']/application" -Name *
Get-WebConfigurationProperty -Filter "//sites/site[@name='MyWebApp']/application" -Name path
Get-WebConfigurationProperty -Filter "//sites/site[@name='MyWebApp']/application" -Name path | select value
3.2 新建:
代码语言:javascript复制New-WebApplication -Name testApp -Site 'MyWebApp' -PhysicalPath D:apidd -ApplicationPool DefaultAppPool
3.3 编辑:
代码语言:javascript复制Set-WebConfigurationProperty -Filter "//sites/site[@name='MyWebApp']/application[@path='/testApp']/virtualDirectory" -Name "physicalPath" -Value "D:apidd2"
3.4 重命名:
代码语言:javascript复制Set-WebConfigurationProperty -Filter "//sites/site[@name='MyWebApp']/application[@path='/testApp']" -Name "path" -Value "/testApp2"
3.5移除:
代码语言:javascript复制Remove-WebApplication -Name testApp2 -Site "MyWebApp"
4. Powershell对Virtual Directory管理
4.1 查看:
代码语言:javascript复制Get-WebVirtualDirectory -site 'MyWebApp'
Get-WebVirtualDirectory -site 'MyWebApp' -Application 'MyApp'
Get-WebConfigurationProperty -Filter "//sites/site[@name='MyWebApp']/application[@path='/']/virtualDirectory" -Name *
Get-WebConfigurationProperty -Filter "//sites/site[@name='MyWebApp']/application[@path='/']/virtualDirectory[@path='/']" -Name *
Get-WebConfigurationProperty -Filter "//sites/site[@name='MyWebApp']/application[@path='/']/virtualDirectory[@path='/']" -Name physicalPath | select value
4.2 新建:
代码语言:javascript复制New-WebVirtualDirectory -Site "MyWebApp" -Name "ContosoVDir" -PhysicalPath "D:apidd2"
New-WebVirtualDirectory -Site "MyWebApp" -Application 'MyApp' -Name "ContosoVDir" -PhysicalPath "D:apidd2"
4.3 编辑:
代码语言:javascript复制Set-WebConfigurationProperty -Filter "//sites/site[@name='MyWebApp']/application[@path='/']/virtualDirectory[@path='/']" -Name physicalPath -Value "D:apidd2"
4.4 删除:
代码语言:javascript复制Remove-WebVirtualDirectory -Site "MyWebApp" -Application "/" -Name "ContosoVDir"
Remove-WebVirtualDirectory -Site "MyWebApp" -Application "MyApp" -Name "ContosoVDir"
5. Powershell对IIS的一些其他管理
5.1 查看PS的版本
代码语言:javascript复制$PSVersionTable.psversion.Major
5.2 判断当前用户是不是管理员
代码语言:javascript复制function IsRunAsAdministrator {
$wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp=new-object System.Security.Principal.WindowsPrincipal($wid)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$isAdmin=$prp.IsInRole($adm)
return $isAdmin
}
5.3 把ASP.NET注册在IIS里
代码语言:javascript复制function RegisterAspNet (){
C:WindowsMicrosoft.NETFrameworkv4.0.30319aspnet_regiis /ir /enable
C:WindowsMicrosoft.NETFramework64v4.0.30319aspnet_regiis /ir /enable
}
5.4 设置IIS里面的MIME
代码语言:javascript复制function SetCustomMIME (){
Add-Webconfigurationproperty //staticContent -name collection -value @{fileExtension='.otf'; mimeType='application/octet-stream'}
}
5.5 设置Response header,参考下面的链接
https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Add-custom-HTTP-786d9dd2
6. 本文参考的网上资料
https://docs.microsoft.com/en-us/powershell/module/webadminstration/?view=winserver2012-ps
最后提一句Get-WebConfigurationProperty和Set-WebConfigurationProperty是非常强大的命令,需要配合IIS的配置文件进行使用。IIS的配置文件:
C:WindowsSystem32inetsrvConfigapplicationHost.config
谢谢观看!