欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Powershell的IIS管理小结

程序员文章站 2022-07-02 12:02:11
现在微软是积极地拥抱Linux,并推出了net core,服务器也提供无UI的版本,提高服务器的性能。很多云平台也提供了无UI版本的windows服务器,所以IIS的命令管理已经非常的重要了。在网上找了半天没有很好的教程,那就自己写一份吧,以后就不用再整理相关知识了,直接看这篇文章应该就可以了。独乐 ......

现在微软是积极地拥抱linux,并推出了net core,服务器也提供无ui的版本,提高服务器的性能。
很多云平台也提供了无ui版本的windows服务器,所以iis的命令管理已经非常的重要了。
在网上找了半天没有很好的教程,那就自己写一份吧,以后就不用再整理相关知识了,直接看这篇文章应该就可以了。
独乐乐不如众乐乐,也分享给大家。

目录:

1. powershell对app pool管理
2. powershell对web sites管理
3. powershell对applications管理
4. powershell对virtual directory管理
5. powershell对iis的一些其他管理

准备环境:

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

new-item –path iis:\apppools\myapppool

1.3 停止:

stop-webapppool -name myapppool

1.4 运行:

start-webapppool -name myapppool

1.5 重启:

restart-webapppool -name myapppool

1.6 编辑属性:

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 (){
    set-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

谢谢观看!