Powershell的自動部署

來源:互聯網
上載者:User

Powershell的自動部署


工作中重複性的版本移植,一天上線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-sessionsif($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|?{$_.computername -eq $computers}if($sshsession -eq $null){New-SshSession -ComputerName $computers -Username $user -Password $pwd -ErrorAction Stop}Invoke-SshCommand -ComputerName $computers -Quiet -Command $Linux_command}Remove-Item $linuxPath}#回收session函數function Recycle-Session{param($computers)if($computers.GetType().IsArray){foreach($c in $computers){Remove-SshSession -ComputerName $c | out-null}}else{Remove-SshSession -ComputerName $computers | out-null}}#解密函數,主要用於解密伺服器登入密碼function Decrypt-String($Encrypted, $Passphrase, $salt="SaltCrypto", $init="IV_Password"){if($Encrypted -is [string]){$Encrypted = try{[Convert]::FromBase64String($Encrypted)}catch{return $false}}$r = new-Object System.Security.Cryptography.RijndaelManaged$pass = [Text.Encoding]::UTF8.GetBytes($Passphrase)$salt = [Text.Encoding]::UTF8.GetBytes($salt)$r.Key = (new-Object Security.Cryptography.PasswordDeriveBytes $pass, $salt, "SHA1", 5).GetBytes(32) #256/8$r.IV = (new-Object Security.Cryptography.SHA1Managed).ComputeHash( [Text.Encoding]::UTF8.GetBytes($init) )[0..15]$d = $r.CreateDecryptor()$ms = new-Object IO.MemoryStream @(,$Encrypted)$cs = new-Object Security.Cryptography.CryptoStream $ms,$d,"Read"$sr = new-Object IO.StreamReader $csWrite-Output $sr.ReadToEnd()$sr.Close()$cs.Close()$ms.Close()$r.Clear()}#在Windows環境下建立備份資訊function Creat-Path{Clear-Content $linuxPath -ErrorAction SilentlyContinue$toLinuxPath=dir -r $parentPath |%{$_.versioninfo}|select filename$n=""foreach($p in $toLinuxPath){$newPath=$p.filename -replace "\\","/"|%{$_ -replace $parentPath2,""}foreach($newP in $newPath){$n=$n + ' ' + $newp}}$n=$n -replace 'newpath.txt',''$n > $linuxPath$n=""}#上傳操作function Upload{$projectNames=dir -Directory $parentPath|%{$_.Name}if($projectNames -eq $null){Write-Warning "$parentPath 中沒有需要操作的項目資訊,請確認後再執行"pausebreak}if($projectNames.GetType().IsArray){Write-Warning 為避免誤操作,請逐個項目備份移植,謝謝!pausebreak}else{$projectConfigureInfo=Import-Csv $projectConfigureFile|?{$_.project -eq $projectNames}if($projectConfigureInfo -ne $null){$project=$projectConfigureInfo.project$computers=$projectConfigureInfo.iplist$port=$projectConfigureInfo.port$user=$projectConfigureInfo.account$passwd=Decrypt-String $projectConfigureInfo.passwd "MyStrongPassword"$webpath=$projectConfigureInfo.webpath#建立備份資訊Creat-Path$question=Read-Host 備份資訊已拼接完成,是否登入到伺服器進行備份(Y/N)switch($question){Y{#串連到伺服器執行備份命令if($computers.IndexOf(',') -gt 0){$computers=$computers -split ","}$sourcePath=gc $linuxPath$linux_command="cd $webpath;/sbin/backup $sourcePath"#Exec-Bash $computers[0] $user $passwd $linux_commandRecycle-Session $computersif($?){Write-Host 檔案備份已完成 -ForegroundColor GreenRemove-Item $linuxPath -Force -ErrorAction Stop}break}N{return $false}default{return $false}}#準備移植Write-Host "$projectNames 項目配置資訊如下:" -ForegroundColor Yellow$projectConfigureInfo|select project,iplist,port,account,webpath|ft$rh=Read-Host 請確認是否開始上傳檔案(Y/N)switch($rh){Y{if($computers.gettype().isarray){foreach($computer in $computers){$openstr="open"+" sftp://"+$user+":"+$passwd+"@"+$computer+":"+$portD:\Tools\winscp520\WinSCP.exe /console /command "option batch continue" "option confirm off" $openstr "option transfer binary" "put $parentPath*" #"exit"}}else{$openstr="open"+" sftp://"+$user+":"+$passwd+"@"+$computers+":"+$portD:\Tools\winscp520\WinSCP.exe /console /command "option batch continue" "option confirm off" $openstr "option transfer binary" "put $parentPath*" #"exit"}break}N{return $false}default{return $false}}}else{Write-Warning "沒有匹配到項目 $projectNames 的配置資訊,請檢查設定檔 $script:projectConfigureFile"pausebreak}}}Upload#>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.