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、实现
源码如下:
# eastmoney public tools# version: v1.0.2# create by xuhoo, 2016-9-27#try {
import-module servermanager -erroraction stop
import-module bitstransfer -erroraction stop
}
catch {
write-warning "$_"; exit
}$packages_path = "d:\software" # packages storage directoryfunction download() { $isexists = test-path $packages_path
if(!$isexists) {
new-item -itemtype directory $packages_path
} # instantiate a socket object,
# try connect to download the source
$testconn = new-object net.sockets.tcpclient $testconn.connect("$address", 80) # $address need to custom
if($testconn) {
start-bitstransfer $address/dotnet4.0.exe $packages_path
start-bitstransfer $address/dotnet4.5.exe $packages_path
return $true
} else { return $false
}
}function checkversion { # to detect the .net framework whether exists in the registry
$isexists = test-path "hklm:\software\microsoft\net framework setup\"
if(!$isexists) { return $false
} else { # returns the current .net framework version
$version = gci "hklm:\software\microsoft\net framework setup\ndp" | sort pschildname -desc | select -fi 1 -exp pschildname return $version
}
}function update {
add-windowsfeature as-net-framework # update .net framework 3.5
# the first cycle:
# perfrom checkversion function, returns the value assigned to $response
# if $response < 4.0, start install dotnet 4.0 and dotnet 4.5
# enter the second loop
# the second cycle:
# again to perfrom checkversion function
# if the installation is successful,
# the value of variable $response at this time will be greater than 4.0,
# the output corrent .net framework version and returns $true
for($i=0;$i -lt 2;$i++) { $response = checkversion if($response -lt "v4.0") {
start-process -wait $packages_path\dotnet4.0.exe -argumentlist "/quiet"
start-process -wait $packages_path\dotnet4.5.exe -argumentlist "/quiet"
} else {
write-host "dotnet current version is: $response"
return $true
}
} # above cycle without entering the return statement,
# then .net framework update failed, this function will return the $false
return $false}function install { $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 # install iis features
foreach($item in $features) {
add-windowsfeature $item
}
remove-windowsfeature web-dir-browsing # delete "web-dir-browsing" function}function registry { $is64bit = [intptr]::size -eq 8 # to determine whether a system is 64-bit
$isapipath_32 = "$env:windir\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll"
set-location "$env:windir\microsoft.net\framework\v4.0.30319\"; .\aspnet_regiis.exe -i if($is64bit) { $isapipath_64 = "$env:windir\microsoft.net\framework64\v4.0.30319\aspnet_isapi.dll"
set-location "$env:windir\microsoft.net\framework64\v4.0.30319\"; .\aspnet_regiis.exe -i
}
}
try { $chkget_result = download $chkup_result = update if($chkup_result) {
install; registry
} else {
write-warning "update .net framework error."
}
}
catch {
write-warning "$_"; exit
}finally {
remove-item $packages_path -recurse
remove-item $myinvocation.mycommand.path -force
}
以上就是windows server 2008 r2 powershell自动部署iis站点的代码详解的详细内容。
