工作中重复性的版本移植,一天上线10几次,让我痛不欲生,频繁的操作也可能出现疲劳性失误,导致严重的生产故障。于是乎,闲暇时间,我开始研究使用powershell自动部署程序到Linux服务器。
脚本中涉及到以下工具:
1、Wincp:借助其自身的命令行模式完成程序部署
2、powershell的ssh-session模块,通过加载该模块连接到Linux服务器,执行相关shell命令
3、.net
开始上代码
#Public environment configure
$script:linuxPath="D:\test\newpath.txt"
$script:parentPath="D:\test\"
$script:parentPath2=$parentPath -replace ""
$script:projectConfigureFile="D:\test\projectConfigureFile.csv"
#执行shell函数
function Exec-Bash($computers,$user,$pwd,$linux_command){
#Check SshSessions Module
$modules=Get-Command -Module ssh-sessions
if($modules -eq $null){
Import-Module ssh-sessions
}
if($computers.GetType().IsArray){
foreach($computer in $computers){
$sshsession=Get-SshSession|?{$_.computername -eq $computer}
if($sshsession.Connected -ne "true"){
New-SshSession -ComputerName $computer -Username $user -Password $pwd -ErrorAction Stop
}
Invoke-SshCommand -ComputerName $computer -Quiet -Command $Linux_command
}
}else{
$sshsession=Get-SshSession|?{$_.