1、功能描述

  1. 连接软件源服务器下载.NET Framework 4.0、.NET Framework 4.5。

  2. 检测并判断当前.NET Framework版本是否小于v4.0,如果小于则进行升级。

  3. 安装IIS组件,安装完成后删除软件及脚本。

  在升级.NET Framework时,是先从3.5升级(可以通过执行命令"Add-WindowsFeature As-Net-Framework"直接安装3.5),然后依次升级4.0、4.5。

2、实现

  源码如下:

  1. try {
  2. Import-Module ServerManager -ErrorAction Stop
  3. Import-Module BitsTransfer -ErrorAction Stop
  4. }
  5. catch {
  6. Write-Warning "$_"; exit
  7. }
  8.  
  9. $packages_path = "D:\software" # Packages storage directory
  10.  
  11. function Download() {
  12. $isExists = Test-Path $packages_path
  13. if(!$isExists) {
  14. New-Item -ItemType Directory $packages_path
  15. }
  16. # instantiate a socket object,
  17. # Try connect to download the source
  18. $testConn = New-Object Net.Sockets.TcpClient
  19. $testConn.Connect("$address", 80) # $address need to custom
  20. if($testConn) {
  21. Start-BitsTransfer $address/dotnet4.0.exe $packages_path
  22. Start-BitsTransfer $address/dotnet4.5.exe $packages_path
  23. return $true
  24. } else {
  25. return $false
  26. }
  27. }
  28.  
  29. function CheckVersion {
  30. # To detect the .NET Framework whether exists in the registry
  31. $isExists = Test-Path "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\"
  32. if(!$isExists) {
  33. return $false
  34. } else {
  35. # Returns the current .NET Framework version
  36. $version = gci "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP" | sort pschildname -desc | select -fi 1 -exp pschildname
  37. return $version
  38. }
  39. }
  40.  
  41. function Update {
  42. Add-WindowsFeature As-Net-Framework # Update .NET Framework 3.5
  43. # The first cycle:
  44. # Perfrom CheckVersion function, returns the value assigned to $response
  45. # If $response < 4.0, start install dotnet 4.0 and dotnet 4.5
  46. # Enter the second loop
  47. # The second cycle:
  48. # Again to perfrom CheckVersion function
  49. # If the installation is successful,
  50. # the value of variable $response at this time will be greater than 4.0,
  51. # the output corrent .NET Framework version and returns $true
  52. for($i=0;$i -lt 2;$i++) {
  53. $response = CheckVersion
  54. if($response -lt "v4.0") {
  55. Start-Process -Wait $packages_path\dotnet4.0.exe -ArgumentList "/quiet"
  56. Start-Process -Wait $packages_path\dotnet4.5.exe -ArgumentList "/quiet"
  57. } else {
  58. Write-Host "DotNET current version is: $response"
  59. return $true
  60. }
  61. }
  62. # Above cycle without entering the return statement,
  63. # then .NET Framework update failed, this function will return the $false
  64. return $false
  65. }
  66.  
  67. function Install {
  68. $features = Get-WindowsFeature Web-Server,Web-Static-Content,Web-Default-Doc,Web-Http-Errors,Web-Http-Redirect,Web-Asp-Net,Web-Net-Ext,Web-ISAPI-Ext,Web-ISAPI-Filter,Web-Http-Logging,Web-Request-Monitor,Web-Filtering,Web-IP-Security,Web-Stat-Compression,Web-Mgmt-Console,Web-WHC
  69. # Install IIS features
  70. foreach($item in $features) {
  71. Add-WindowsFeature $item
  72. }
  73. Remove-WindowsFeature Web-Dir-Browsing # Delete "Web-Dir-Browsing" function
  74. }
  75.  
  76. function Registry {
  77. $is64bit = [IntPtr]::Size -eq 8 # To determine whether a system is 64-bit
  78. $isapiPath_32 = "$env:windir\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll"
  79. Set-Location "$env:windir\Microsoft.NET\Framework\v4.0.30319\"; .\aspnet_regiis.exe -i
  80. if($is64bit) {
  81. $isapiPath_64 = "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll"
  82. Set-Location "$env:windir\Microsoft.NET\Framework64\v4.0.30319\"; .\aspnet_regiis.exe -i
  83. }
  84. }
  85.  
  86. try {
  87. $chkGet_result = Download
  88. $chkUp_result = Update
  89. if($chkUp_result) {
  90. Install; Registry
  91. } else {
  92. Write-Warning "Update .NET Framework error."
  93. }
  94. }
  95. catch {
  96. Write-Warning "$_"; exit
  97. }
  98. finally {
  99. Remove-Item $packages_path -Recurse
  100. Remove-Item $MyInvocation.MyCommand.Path -Force
  101. }

PowerShell自动部署IIS站点(Windows Server 2008 R2)的更多相关文章

  1. Windows Server 2008 R2 + IIS 环境部署Asp.Net Core App

    Windows + IIS 环境部署Asp.Net Core App   环境:Windows Server 2012, IIS 8, Asp.Net Core 1.1. 不少人第一次在IIS中部署A ...

  2. 在Windows Server 2008 R2上安装IIS服务

    一.Windows Server 2008 R2 介绍 1.Windows Server 2008 R2 基本概念 2.Windows Server 2008 R2 家族系列 二.VMware虚拟机安 ...

  3. Windows Server 2008 R2之一活动目录服务部署

    测试环境: 服务器:计算机名Win2008R2CNDC,已安装Windows Server 2008 R2.IPV4:192.168.1.13,255.255.255.0,网关地址192.168.1. ...

  4. SQLite 在Windows Server 2008 R2 部署问题FAQ汇总[轉]

    轉自:http://www.steveluo.name/sqlite-windows-server-2008-r2-deploy-faq/ 今天花了一天的时间研究了一下SQLite,以取代一些轻量级项 ...

  5. windows server 2008 R2 Enterprise 间实时同步之FreeFileSync 部署过程

    WindowsServer间实时同步之FreeFileSync 部署过程 1. 实验主机信息 IP 操作系统 源目录 目标目录 10.155.0.80 Windows Server 2008 R2 D ...

  6. windows server 2008 R2域中的DC部署 分类: AD域 Windows服务 2015-06-06 21:09 68人阅读 评论(0) 收藏

    整个晚上脑子都有点呆滞,想起申请注册好的博客还从来都不曾打理,上来添添生机.从哪里讲起呢,去年有那么一段时间整个人就陷在域里拔不出来,于是整理了一些文档,害怕自己糊里糊涂的脑子将这些东西会在一觉醒来全 ...

  7. windows server 2008 R2中建立ftp站点

    在windows server 2008 R2中建立ftp站点,要遵循以下步骤: (1) 开启IIS中的ftp服务: (2) 在IIS中建立ftp站点. 具体过程如下: (1) 开启IIS中的ftp服 ...

  8. Windows Server 2008 R2 部署服务

    Windows Server 2008 R2 部署服务 部分参考: Windows Server 2008 R2 部署服务 - 马睿的技术博客 - 51CTO技术博客http://marui.blog ...

  9. Windows Server 2008 R2 IIS7.5 部署 MVC HTTP 404.0 Not Found 错误

    如图 在Windows Server 2008 R2 IIS7.5 部署 MVC HTTP 404.0 Not Found 错误,在Win7环境下测试正常,在百度中查找相关解决方法,如修改配置文件等, ...

随机推荐

  1. Oracle Sales Cloud:管理沙盒(定制化)小细节1——利用公式创建字段并显示在前端页面

    Oracle Sales Cloud(Oracle 销售云)是一套基于Oracle云端的CRM管理系统.由于 Oracle 销售云是基于 Oracle 云环境的,它与传统的管理系统相比,显著特点之一便 ...

  2. Java中的经典算法之冒泡排序(Bubble Sort)

    Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...

  3. 20-C语言结束

    我回去看了下5号写的.虽然今天已经21号了~,花了16天. 复习完C专题,接下来我大概会用C做一些操作系统/信息安全的小东西,会发到博客里,敬请期待! ----2016/11/21

  4. mac终端命令

    目录操作 命令名 功能描述 使用举例 mkdir 创建一个目录 mkdir dirname rmdir 删除一个目录 rmdir dirname mvdir 移动或重命名一个目录 mvdir dir1 ...

  5. 【转】JVM 架构解读

    每个Java开发人员都知道字节码由JRE(Java运行时环境)执行.但许多人不知道JRE是Java Virtual Machine(JVM)的实现,它分析字节码,解释代码并执行它.作为开发人员,我们应 ...

  6. [python]set集合学习

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...

  7. mysql max_allowed_packet 设置过小导致记录写入失败

    mysql根据配置文件会限制server接受的数据包大小. 有时候大的插入和更新会受max_allowed_packet 参数限制,导致写入或者更新失败. 查看目前配置 show VARIABLES ...

  8. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  9. [LeetCode] Bulb Switcher 灯泡开关

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  10. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...