Automatic deployment of Powershell

Source: Internet
Author: User

Automatic deployment of Powershell
Zookeeper

The repetitive version migration at work has been launched more than 10 times a day, which makes me uncomfortable. Frequent operations may also lead to fatigue errors, resulting in serious production faults. In my spare time, I started to study how to use powershell to automatically deploy programs on Linux servers.

The script involves the following tools:

1. Wincp: use its own command line mode to deploy the program

2. the ssh-session module of powershell connects to the Linux server by loading the module and runs the shell command.

3.. net

Start code


 

# Public environment configure $ script: linuxPath = "D: \ test \ newpath.txt" $ script: parentPath = "D: \ test \" $ script: parentPath2 = $ parentPath-replace "\", "/" $ script: projectConfigureFile = "D: \ test \ projectConfigureFile.csv" # execute the 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 ($ compu Ters. 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} # Recycle session function Recycle-Session {param ($ computers) if ($ computers. getType (). isArray) {foreach ($ c in $ computers) {Remove-SshSession-ComputerName $ c | out-nul L }} else {Remove-SshSession-ComputerName $ computers | out-null }# decryption function, used to Decrypt the Server login password 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 = [Te Xt. 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 = ne W-Object IO. streamReader $ csWrite-Output $ sr. readToEnd () $ sr. close () $ cs. close () $ ms. close () $ r. clear ()} # create the backup information 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 = ""} # Upload operation function Upload {$ projectNames = dir-Directory $ parentPath | % {$ _. name} if ($ projectNames-eq $ null) {Write-Warning "$ parentPath does not have the project information to be operated on. Please confirm and then execute" pausebreak} if ($ projectNames. getType (). isArray) {Write-Warning in order to avoid misoperation, please back up and transplant projects one by one. Thank you! 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 # create backup information Creat-Path $ question = Read-Host the backup information has been spliced and whether to log on to the server for backup (Y/N) switch ($ question) {Y {# Connect to the server and execute the BACKUP command 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 file backup completed-ForegroundColor GreenRemove-Item $ linuxPath-Force-ErrorAction Stop} break} N {return $ false} default {return $ false} # Prepare to port Write -Host "$ projectNames project configuration information is as follows: "-ForegroundColor Yellow $ projectConfigureInfo | select project, iplist, port, account, webpath | ft $ rh = Read-Host check whether to start uploading files (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 "does not match the configuration information of project $ projectNames, check the configuration file $ script: projectConfigureFile "pausebreak }}upload #>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.