当前位置: 移动技术网 > 科技>操作系统>windows > 使用PowerShell实现服务器常用软件的无人值守安装

使用PowerShell实现服务器常用软件的无人值守安装

2019年06月18日  | 移动技术网科技  | 我要评论

三三,青岛厚壁钢管,自然风景画图片

操作系统:windows server 2016 , windows server 2019

软件环境:

类型

名称

版本

 

系统功能

telnetclien

  
 

iis

 

启用asp.net 4.7

 

hyper-v

 

启用管理

常用软件

winrar

5.0

 
 

firefox

67 chs

 
 

sqlserver

2016

启用tcp 1436

 

microsoft sql server management studio

14.0.17277.0

 
 

redis

3.2.1

 
 

火绒安全

个人版 4.0

 

运行环境

dotnetframework

4.7.2

 
 

dotnetcoresdk

2.2.104

 
 

dotnetcore-iis-hosting

2.1.7

 
 

vc++ runtime

2017

 
 

防火墙

 

关闭所有防火墙

 

 

ps脚本

@@@code

$installsql = 1

$installssms = 1

$installiis =1

$installhyperv =1

$installdotnet462 =1

$installdotcore220 = 1

$installvcruntime = 1

$installsysdiag = 1

$installredis =1

 

$maindir ="d:\"

$mainurl = "http://192.168.10.36/soft"

 

 

$tmppath =$maindir + "temp\system"

$normalpath = @()

$normalpath += $tmppath

$normalpath += $maindir + "data\sql"

$normalpath += $maindir + "data\web"

$normalpath += $maindir + "app\redis"

$normalpath += $maindir + "app\dotnet"

$normalpath += $maindir + "hyper-v\virtual hard disks"

$normalpath += $maindir + "hyper-v\vm"

 

$firefoxfile = "firefox.exe"

$vcruntimefile = "vc_redist.exe"

$winrarfile = "winrar.exe"

$winrarkeyfile = "rarreg.key"

$sqlfile = "sql2016_deve_sp2.iso"

$sqlservicename = "sql2016"

$ssmsfile ="ssms2017.exe"

$dotnetfile = "ndp472.exe"

$dotnetchsfile = "ndp472chs.exe"

$dotnetcorefile ="dotnetcoresdk-2.2.104.exe"

$dotnetcorehostfile ="dotnet-hosting-2.1.7-win.exe"

$sysdiagfile = "sysdiag.exe"

$redisfile = "redis.msi"

# add path

 

function createdir($arg)

{

if (![system.io.directory]::exists($arg))

{

# [system.io.directory]::createdirectory($dir);

mkdir $arg | out - null

}

}

function down($url , $file, $force)

{

 

$needdown = 1;

if ($force - eq 1){

$needdown = 1;

}else{

if ([system.io.file]::exists($file))

{

write - host "已存在$file,比对中..."

$1 = $url + ".md5.txt"

$2 = $file + ".md5.txt"

#write-host $1,$2

if (![system.io.file]::exists($2))

{

invoke - webrequest - uri $1 - outfile $2

unblock - file $2

}

$md5_2 = [system.io.file]::readalltext($2);

$md5 = (get - filehash $file - algorithm md5).hash

if ( $md5 - eq $md5_2){

$needdown = 0;

write - host "$file 文件hash相同,无需下载"

}else{

write - host "$file hash 为$md5,要求为$md5_2"

}

}

}

if ($needdown - eq 1){

write - host "开始下载$url 到 $file ..."

invoke - webrequest - uri $url - outfile $file

unblock - file $file

}

 

}

 

function downloadandinstall( $url, $file,$name,$para)

{

down $url $file 0

write - host "请等待 $name 安装界面出现并完成交互" - foreground "yellow"

start - process $file $para - wait

write - host "$name 安装完成"

}

 

function changesqlservertcpport( $name , $port )

{

[system.reflection.assembly]::loadwithpartialname("microsoft.sqlserver.sqlwmimanagement") | out-null

$server_name = (get-wmiobject -class win32_computersystem -property name).name

$machine = new-object 'microsoft.sqlserver.management.smo.wmi.managedcomputer' $server_name

 

$instance = $machine.serverinstances[ $name];

 

$ipall = $instance.serverprotocols['tcp'].ipaddresses['ipall'];

$ipall.ipaddressproperties['tcpport'].value = "${port}"

$instance.serverprotocols['tcp'].alter();

}

 

function openfirewall( $name , $port , $tcp )

{

remove - netfirewallrule - displayname "$name" - erroraction silentlycontinue

new - netfirewallrule - displayname "$name" - direction inbound - localport ${ port}

-protocol ${ tcp}

-action allow | out - null

write - host "开放服务${name}端口${port},${tcp}"

}

 

function disableallfirewallrule( $port )

{

set - netfirewallprofile - profile public - enabled false

set - netfirewallprofile - profile private - enabled false

get - netfirewallrule | foreach{

$flag = $_.enabled

if ( $flag - eq 1 ){

$name =$_.displayname

set - netfirewallrule - displayname $name - enabled false - erroraction silentlycontinue

write - host "禁用防火墙 $name "

}

}

openfirewall "运维专用" $port tcp

set - netfirewallprofile - profile public - enabled true

set - netfirewallprofile - profile private - enabled true

}

 

function getregkey($key,$name)

{

 

$result = (get - itemproperty - path "$key" - erroraction silentlycontinue).$name

return $result

#write-host $result

}

 

function hasdotnetversion($iscore,$version)

{

if ($iscore - eq 0){

$dotversion = getregkey "hklm:software\microsoft\net framework setup\ndp\v4\full" "release"

if ( $dotversion - ge $version)

{

return getregkey "hklm:software\microsoft\net framework setup\ndp\v4\full" "version"

}

}

else{

$x = get - command dotnet - erroraction silentlycontinue

if ($?){

return (dir(get - command dotnet).path.replace('dotnet.exe', 'sdk') - erroraction ignore | where - object {$_.name - eq "$version"} | select name ).name

}

}

return "";

}

 

write-host "文件夹处理"

$normalpath | foreach {

createdir $_

}

 

$winrarversion = getregkey "hklm:\software\winrar" "exe64"

if ($winrarversion.length -gt 0){

write-host "winrar已安装 $winrarfile"

}

else{

downloadandinstall "$mainurl/system/$winrarfile" "$tmppath\$winrarfile" "winrar" " /install /passive /norestart"

$winrarversion = getregkey "hklm:\software\winrar" "exe64"

down "$mainurl/system/$winrarkeyfile" "$tmppath\$winrarkeyfile" 0

[system.io.file]::copy("$tmppath\$winrarkeyfile", [system.io.path]::combine([system.io.path]::getdirectoryname("$winrarversion") , $winrarkeyfile) , 1)

}

 

write-host "安装火狐浏览器"

$installfirefox = 1

$1 =get-item -path "hkcu:\software\mozilla\firefox\launcher" -erroraction silentlycontinue

if ($?)

{

$firefoxversion=$1.getvaluenames()[0]

#= getregkey "hkcu:\software\mozilla\mozilla firefox" "currentversion"

if ($firefoxversion.length -gt 0){

write-host "winrar已安装在 $firefoxversion"

$installfirefox = 0

}

}

if ($installfirefox -eq 1) {

downloadandinstall "$mainurl/system/$firefoxfile" "$tmppath\$firefoxfile" "firefox" " /install /passive /norestart"

}

 

write-host "redis处理"

if ($installredis -eq 1){

$redisservice = get-service redis -erroraction silentlycontinue

if($?){

write-host "redis 已安装"

$installredis = 0

}else{

downloadandinstall "$mainurl/system/$redisfile" "$tmppath\$redisfile" "redis" " "

 

}

}

 

write-host "防火墙处理"

disableallfirewallrule 3389

openfirewall sqlserver 1436 tcp

openfirewall bandservice 3410-3415 tcp

openfirewall logserverweb 5881 tcp

openfirewall logserver 5880 udp

openfirewall redis 6379 tcp

 

write-host ""telnetclient处理

$telnet = get-windowsfeature "telnet-client" | select installstate

if($telnet.installstate -eq "installed"){

write-host "telnetclient 已安装"

}

else{

install-windowsfeature "telnet-client" | out-null

}

 

 

 

if ($installdotnet462 -eq 1){

write-host "dotnet处理"

$dotversion =hasdotnetversion 0 461814 #394802

if( $dotversion.length -gt 0 )

{

write-host "已安装dotnet $dotversion"

}else{

downloadandinstall "$mainurl/system/$dotnetfile" "$tmppath\$dotnetfile" "dotnet" " /install /passive /norestart"

downloadandinstall "$mainurl/system/$dotnetchsfile" "$tmppath\$dotnetchsfile" "dotnetchs" " /install /passive /norestart"

}

}

 

if( $installiis -eq 1){

write-host "iis处理"

install-windowsfeature web-server -erroraction continue

 

 

add-windowsfeature web-server,web-webserver,web-security,web-filtering,web-common-http,web-http-errors,web-static-content,web-default-doc,web-dir-browsing

add-windowsfeature web-performance,web-stat-compression,web-health,web-http-logging,web-app-dev,web-net-ext45,web-asp-net45,web-isapi-ext,web-isapi-filter,web-mgmt-tools,web-mgmt-console

write-host "web 服务器(iis) 安装完成"

}

 

if ($installdotcore220 -eq 1){

write-host "dotnetcore处理"

$dotversion =hasdotnetversion 1 "2.2.104"

if( $dotversion.length -gt 0 )

{

write-host "已安装dotnetcore $dotversion"

}else{

downloadandinstall "$mainurl/system/$dotnetcorefile" "$tmppath\$dotnetcorefile" "dotnetcoresdk" " /install /passive /norestart"

downloadandinstall "$mainurl/system/$dotnetcorehostfile" "$tmppath\$dotnetcorehostfile" "dotnetcore iis hosting" " /install /passive /norestart"

 

}

}

 

write-host "处理c++2017运行时"

if ($installvcruntime -eq 1){

$vcruntimeversion = getregkey "hklm:\software\microsoft\devdiv\vc\servicing\14.0\runtimeminimum" "version"

if ($vcruntimeversion.length -gt 0){

if([system.version]::parse($vcruntimeversion).minor -ge 16){

$installvcruntime = 0

write-host "visual c++ redistributable $vcruntimeversion 已安装" #14.12.25810 14.16.27024

}

}

if($installvcruntime -eq 1){

downloadandinstall "$mainurl/system/$vcruntimefile" "$tmppath\$vcruntimefile" "visual c++ redistributable" " /install /passive /norestart"

}

}

write-host "计算机即将重启,请在重启后继续执行脚本" -foregroundcolor red

restart-computer

 

if ($installhyperv -eq 1){

write-host "hyperv处理"

$1 = (get-windowsfeature hyper-v).installstate

if ($1 -eq "installed"){

write-host "hyperv已安装"

set-vmhost -computername $env:computername -virtualharddiskpath "d:\hyper-v\virtual hard disks" -virtualmachinepath "d:\hyper-v\vm"

}

else{

install-windowsfeature hyper-v -includemanagementtools -restart

}

}

 

if ($installsql -eq 1){

write-host "$sqlservicename 处理"

$sqlservice = get-service mssql`$${sqlservicename} -erroraction silentlycontinue

if($?){

write-host "$sqlservicename 已安装"

$installsql = 0

#changesqlservertcpport $sqlservicename 1436

#restart-service mssql`$${sqlservicename} -force

}

 

 

 

if ($installsql -eq 1){

down "$mainurl/system/$sqlfile" "$tmppath\$sqlfile" 0

 

$sqlini = "${tmppath}\${sqlfile}.ini"

down $mainurl/system/${sqlfile}.ini $sqlini 0

$sqlfile="$tmppath\$sqlfile"

write-host "从配置文件 $sqlini 安装sqlserver,使用了静默安装开发版本,后续需要更换正式的授权序列号"

 

 

 

$1 = (get-diskimage -imagepath $sqlfile).attached

if (!$1){

write-host "加载 $sqlfile "

mount-diskimage -imagepath $sqlfile | out-null

}

$1 = (get-diskimage -imagepath $sqlfile | get-volume ).driveletter

$exe = $1 + ":\setup.exe "

$para ="/configurationfile=${sqlini}"

# & $exe $para

 

write-host "请等待sqlserver 安装界面出现并完成交互"

start-process $exe $para -wait

changesqlservertcpport $sqlservicename 1436

restart-service mssql`$${sqlservicename} -force

}

}

if ($installssms -eq 1){

write-host "ssms处理"

 

$ssmskey = getregkey 'hkcu:\software\microsoft\sql server management studio\14.0_config' 'installdir'

if( $ssmskey ){

write-host "ssms已安装在 $ssmskey "

$installssms = 0

}else{

$ssmskey = getregkey 'hklm:\software\wow6432node\microsoft\appenv\14.0\apps\ssms_14.0' 'stubexepath'

if ( $ssmskey ){

write-host "ssms已安装在 $ssmskey "

$installssms = 0

}

}

 

if ($installssms -eq 1)

{

downloadandinstall "$mainurl/system/$ssmsfile" "$tmppath\$ssmsfile" "microsoft sql server management studio" " /install /passive "

}

}

 

 

 

if ($installsysdiag -eq 1){

write-host "火绒安全处理"

$key = getregkey 'hklm:\software\huorong\sysdiag' 'installpath'

if( $key.length -gt 0 ){

write-host "火绒安全已安装在 $key "

$installsysdiag = 0

}

if ($installsysdiag -eq 1)

{

write-host "火绒安全安装完成后请先退出一次" -foregroundcolor red

downloadandinstall "$mainurl/system/$sysdiagfile" "$tmppath\$sysdiagfile" "火绒安全" " /install /passive "

}

}

 

@@#

 

 

数据库配置文件

@@@code

;sql server 2016 configuration file

[options]

 

; 指定安装程序的工作流,如 install、uninstall 或 upgrade。这是必需的参数。

 

action="install"

 

; 指定从命令行运行时,sql server 安装程序不应显示隐私声明。

 

suppressprivacystatementnotice="true"

;将在安装程序用户界面中显示许可条款,供您审核审阅和接受,指定从命令行运行时,设置为true自动接受许可

iacceptsqlserverlicenseterms="true"

 

; 指定此参数并接受 microsoft r open 和 microsoft r server 条款即表明你确认已阅读并了解使用条款。

 

iacceptropenlicenseterms="true"

 

 

 

; 使用 /enu 参数可在本地化的 windows 操作系统上安装英语版本的 sql server。

 

enu="false"

 

; 安装程序将不会显示任何用户界面。

 

quiet="false"

 

; 安装程序将只显示进度,而不需要任何用户交互。

 

quietsimple="true"

 

; 用于控制用户界面行为的参数。有效值对于完整 ui 为 normal,对于简化的 ui 为 autoadvance,为 enableuionservercore 则跳过 server core 安装程序 gui 块。

;使用了quietsimple或quiet之后不能使用uimode

;uimode="normal"

 

; 指定 sql server 安装程序是否应发现和包括产品更新。有效值是 true 和 false 或者 1 和 0。默认情况下,sql server 安装程序将包括找到的更新。

 

updateenabled="0"

 

; 如果提供了此参数,则此计算机将使用 microsoft 更新检查更新。

 

usemicrosoftupdate="false"

 

; 指定要安装、卸载或升级的功能。顶级功能列表包括 sql、as、rs、is、mds 和工具。sql 功能将安装数据库引擎、复制、全文和 data quality services (dqs)服务器。工具功能将安装共享组件。

 

features=sqlengine,replication

 

; 指定 sql server 安装程序将获取产品更新的位置。有效值为 "mu" (以便搜索产品更新)、有效文件夹路径以及 .\myupdates 或 unc 共享目录之类的相对路径。默认情况下,sql server 安装程序将通过 window server update services 搜索 microsoft update 或 windows update 服务。

 

updatesource="mu"

 

; 显示命令行参数用法

 

help="false"

 

; 指定应将详细的安装程序日志传送到控制台。

 

indicateprogress="false"

 

; 指定安装程序应该安装到 wow64 中。ia64 或 32 位系统不支持此命令行参数。

 

x86="false"

 

; 指定默认实例或命名实例。mssqlserver 是非 express 版本的默认实例,sqlexpress 则是 express 版本的默认实例。在安装 sql server 数据库引擎(sql)、analysis services (as)或 reporting services (rs)时,此参数是必需的。

 

instancename="sql2016"

 

; 指定共享组件的安装根目录。在已安装共享组件后,此目录保持不变。

 

installshareddir="c:\program files\microsoft sql server"

 

; 指定 wow64 共享组件的安装根目录。在已安装 wow64 共享组件后,此目录保持不变。

 

installsharedwowdir="c:\program files (x86)\microsoft sql server"

 

; 为您已指定的 sql server 功能指定实例 id。sql server 目录结构、注册表结构和服务名称将包含 sql server 实例的实例 id。

 

instanceid="sql2016"

 

; telemetryusernameconfigdescription

 

sqltelsvcacct="nt service\sqltelemetry$sql2016"

 

; telemetrystartupconfigdescription

 

sqltelsvcstartuptype="automatic"

 

; 指定安装目录。

 

instancedir="c:\program files\microsoft sql server"

 

; 代理帐户名

 

agtsvcaccount="nt service\sqlagent$sql2016"

 

; 安装后自动启动服务。

 

agtsvcstartuptype="automatic"

 

; cm 程序块 tcp 通信端口

 

commfabricport="0"

 

; 矩阵如何使用专用网络

 

commfabricnetworklevel="0"

 

; 如何保护程序块间的通信

 

commfabricencryption="0"

 

; cm 程序块使用的 tcp 端口

 

matrixcmbrickcommport="0"

 

; sql server 服务的启动类型。

 

sqlsvcstartuptype="automatic"

 

; 启用 filestream 功能的级别(0、1、2 或 3)。

 

filestreamlevel="2"

 

; 要为 filestream 文件 i/o 创建的 windows 共享的名称。

 

filestreamsharename="sql2016"

 

; 设置为 "1" 可为 sql server express 启用 ranu。

 

enableranu="false"

 

; 指定要用于数据库引擎的 windows 排序规则或 sql 排序规则。

 

sqlcollation="chinese_prc_ci_as"

 

; sql server 服务的帐户: 域\用户或系统帐户。

 

sqlsvcaccount="nt service\mssql$sql2016"

 

; 设置为 "true" 以启用 sql server 服务的即时文件初始化。如果已启用,安装程序将授予数据库引擎服务 sid"执行卷维护任务"特权。这可能会导致信息泄漏,因为这会允许未经授权的主体访问已删除的内容。

 

sqlsvcinstantfileinit="false"

 

; 要设置为 sql server 系统管理员的 windows 帐户。

 

sqlsysadminaccounts=".\administrator"

 

; 默认值为 windows 身份验证。使用 "sql" 表示采用混合模式身份验证。

 

securitymode="sql"

sapwd="pa4432^22"

 

; 数据库引擎 tempdb 文件数。

 

sqltempdbfilecount="8"

 

; 指定数据库引擎 tempdb 数据文件的初始大小(mb)。

 

sqltempdbfilesize="8"

 

; 指定每个数据库引擎 tempdb 数据文件的自动增长增量(mb)。

 

sqltempdbfilegrowth="64"

 

; 指定数据库引擎 tempdb 日志文件的初始大小(mb)。

 

sqltempdblogfilesize="8"

 

; 指定数据库引擎 tempdb 日志文件的自动增长增量(mb)。

 

sqltempdblogfilegrowth="64"

 

; 数据库引擎根数据目录。

 

installsqldatadir="d:\data\sql"

 

; 将当前用户设置为 %sql_product_short_name% express 的数据库引擎系统管理员。

 

addcurrentuserassqladmin="false"

 

; 指定 0 禁用 tcp/ip 协议,指定 1 则启用该协议。

 

tcpenabled="1"

 

; 指定 0 禁用 named pipes 协议,指定 1 则启用该协议。

 

npenabled="0"

 

; browser 服务的启动类型。

 

browsersvcstartuptype="disabled"

@@#

 

输出

@@@code

文件夹处理

开始下载http://192.168.10.36/system/winrar.exe 到 d:\temp\system\winrar.exe ...

请等待 winrar 安装界面出现并完成交互

winrar 安装完成

开始下载http://192.168.10.36/system/rarreg.key 到 d:\temp\system\rarreg.key ...

安装火狐浏览器

开始下载http://192.168.10.36/system/firefox.exe 到 d:\temp\system\firefox.exe ...

请等待 firefox 安装界面出现并完成交互

firefox 安装完成

redis处理

开始下载http://192.168.10.36/system/redis.msi 到 d:\temp\system\redis.msi ...

请等待 redis 安装界面出现并完成交互

redis 安装完成

防火墙处理

禁用防火墙 connected user experiences and telemetry

禁用防火墙 delivery optimization (tcp-in)

禁用防火墙 delivery optimization (udp-in)

…..

禁用防火墙 网络发现(upnp-in)

禁用防火墙 firefox (c:\program files (x86)\mozilla firefox)

禁用防火墙 firefox (c:\program files (x86)\mozilla firefox)

禁用防火墙 redis

开放服务运维专用端口3389,tcp

开放服务sqlserver端口1436,tcp

开放服务bandservice端口3410-3415,tcp

开放服务logserverweb端口5881,tcp

开放服务logserver端口5880,udp

开放服务redis端口6379,tcp

telnetclient处理

dotnet处理

开始下载http://192.168.10.36/system/ndp472.exe 到 d:\temp\system\ndp472.exe ...

请等待 dotnet 安装界面出现并完成交互

dotnet 安装完成

开始下载http://192.168.10.36/system/ndp472chs.exe 到 d:\temp\system\ndp472chs.exe ...

请等待 dotnetchs 安装界面出现并完成交互

dotnetchs 安装完成

iis处理

success restart needed exit code feature result

------- -------------- --------- --------------

true yes successrest... {常见 http 功能, 默认文档, 目录浏览, 请求筛选...}

警告: 必须重新启动此服务器才能完成安装过程。

true yes nochangeneeded {}

true yes successrest... {asp.net 4.6, 应用程序开发, asp.net 4.6, isapi 扩...

警告: 必须重新启动此服务器才能完成安装过程。

web 服务器(iis) 安装完成

dotnetcore处理

开始下载http://192.168.10.36/system/dotnetcoresdk-2.2.104.exe 到 d:\temp\system\dotnetcoresdk-2.2.104.exe ...

请等待 dotnetcoresdk 安装界面出现并完成交互

已安装dotnetcore 2.2.104

处理c++2017运行时

visual c++ redistributable 14.16.27024 已安装

sql2016 处理

已存在d:\temp\system\sql2016_deve_sp2.iso,比对中...

d:\temp\system\sql2016_deve_sp2.iso hash 为e352c8a057bf78fa0348757716f46aa0,要求为cf399685c44b7988f911195401c01b12

开始下载http://192.168.10.36/system/sql2016_deve_sp2.iso 到 d:\temp\system\sql2016_deve_sp2.iso ...

从配置文件 d:\temp\system\sql2016_deve_sp2.iso.ini 安装sqlserver,使用了静默安装开发版本,后续需要更换正式的授权序列号

加载 d:\temp\system\sql2016_deve_sp2.iso

请等待sqlserver 安装界面出现并完成交互

 

火绒安全处理

已存在d:\temp\system\sysdiag.exe,比对中...

d:\temp\system\sysdiag.exe 文件hash相同,无需下载

请等待 火绒安全 安装界面出现并完成交互

火绒安全 安装完成

 

 

ps c:\windows\system32>

@@#

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网