Powershell的IIS管理小結

如今微軟是積極地擁抱Linux,並推出了net core,服務器也提供無UI的版本,提升服務器的性能。
不少雲平臺也提供了無UI版本的windows服務器,因此IIS的命令管理已經很是的重要了。
在網上找了半天沒有很好的教程,那就本身寫一份吧,之後就不用再整理相關知識了,直接看這篇文章應該就能夠了。
獨樂樂不如衆樂樂,也分享給你們。web

目錄:shell

1. Powershell對app pool管理
2. Powershell對web sites管理
3. Powershell對applications管理
4. Powershell對Virtual Directory管理
5. Powershell對IIS的一些其餘管理
6. 本文參考的網上資料windows

準備環境:api

powershell
Import-Module WebAdministration

1. Powershell對app pool管理
1.1 查看: 服務器

Get-ChildItem –Path IIS:\AppPools
Get-ItemProperty –Path IIS:\AppPools\MyAppPool | 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 新建:app

New-Item –Path IIS:\AppPools\MyAppPool

1.3 中止:webapp

Stop-WebAppPool -Name MyAppPool

1.4 運行:ide

Start-WebAppPool -Name MyAppPool

1.5 重啓:性能

ReStart-WebAppPool -Name MyAppPool

1.6 編輯屬性:ui

Get-ItemProperty –Path IIS:\AppPools\MyAppPool | select *
Set-ItemProperty -Path IIS:\AppPools\MyAppPool -Name managedRuntimeVersion -Value v4.0

1.7 重命名:

Set-ItemProperty -Path IIS:\AppPools\MyAppPool -Name name -Value MyAppPool2
Set-ItemProperty -Path IIS:\AppPools\MyAppPool2 -Name name -Value MyAppPool

1.8 移除:

Remove-WebAppPool -Name MyAppPool

2. Powershell對web sites管理
2.1 查看:

get-childitem -path IIS:
Get-IISSite
Get-IISSite MyWebApp
Get-Website
Get-Website MyWebApp
Get-Website –Name MyWebApp
get-Item IIS:\Sites\MyWebApp
Get-ItemProperty –Path IIS:\Sites\MyWebApp | 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 新建:

New-Website –Name MyWebApp –PhysicalPath D:\apidd

2.3 中止:

Stop-Website –Name MyWebApp

2.4 運行:

Start-Website –Name MyWebApp

2.5 重啓:

Stop-Website –Name MyWebApp
Start-Website –Name MyWebApp

2.6 綁定:

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:\localmachine\my
$bindingInfo = "IIS:\SSLBindings\*!445"
$cert | Set-Item -Path $bindingInfo

2.7 編輯屬性:

Get-ItemProperty –Path IIS:\Sites\MyWebApp | select *
Set-ItemProperty -Path IIS:\Sites\MyWebApp -Name enabledProtocols -Value http

2.8 重命名:

Rename-Item 'IIS:\Sites\MyWebApp' 'MyWebApp2'

2.9 移除:

Remove-WebSite -Name MyWebApp2

3. Powershell對applications管理
3.1 查看:

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 新建:

New-WebApplication -Name testApp -Site 'MyWebApp' -PhysicalPath D:\apidd -ApplicationPool DefaultAppPool

3.3 編輯:

Set-WebConfigurationProperty -Filter "//sites/site[@name='MyWebApp']/application[@path='/testApp']/virtualDirectory" -Name "physicalPath" -Value "D:\apidd2"

3.4 重命名:

Set-WebConfigurationProperty -Filter "//sites/site[@name='MyWebApp']/application[@path='/testApp']" -Name "path" -Value "/testApp2"

3.5移除:

Remove-WebApplication -Name testApp2 -Site "MyWebApp"

4. Powershell對Virtual Directory管理
4.1 查看:

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 新建:

New-WebVirtualDirectory -Site "MyWebApp" -Name "ContosoVDir" -PhysicalPath "D:\apidd2"
New-WebVirtualDirectory -Site "MyWebApp" -Application 'MyApp' -Name "ContosoVDir" -PhysicalPath "D:\apidd2"

4.3 編輯:

Set-WebConfigurationProperty -Filter "//sites/site[@name='MyWebApp']/application[@path='/']/virtualDirectory[@path='/']" -Name physicalPath -Value "D:\apidd2"

4.4 刪除:

Remove-WebVirtualDirectory -Site "MyWebApp" -Application "/" -Name "ContosoVDir"
Remove-WebVirtualDirectory -Site "MyWebApp" -Application "MyApp" -Name "ContosoVDir"

5. Powershell對IIS的一些其餘管理
5.1 查看PS的版本

$PSVersionTable.psversion.Major

5.2 判斷當前用戶是否是管理員

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裏

function RegisterAspNet (){
    C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis /ir /enable
    C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis /ir /enable
}

5.4 設置IIS裏面的MIME

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:\Windows\System32\inetsrv\Config\applicationHost.config

謝謝觀看!

相關文章
相關標籤/搜索