Automatic deployment of Powershell

Source: Internet
Author: User
Tags decrypt sessions



Work in a repetitive version of the transplant, 10 times a day on-line, let me pain, frequent operation may also occur fatigue errors, leading to serious production failure. Thus, in my spare time, I began to study the use of PowerShell automatic deployment program to a Linux server.

The following tools are involved in the script:

1. WINCP: Complete program deployment with its own command-line mode

2. PowerShell ssh-session module, connect to Linux server by loading this module, execute related shell command

3,. Net


Start on the code


#Public Environment Configure
$script: linuxpath= "D:\test\newpath.txt"
$script:p arentpath= "D:\test\"
$script:p arentpath2= $parentPath-replace "\", "/"
$script:p rojectconfigurefile= "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|? {$_.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
}
}

#解密函数, mainly 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 = [Text.encoding]::utf8. GetBytes ($salt)
$r. Key = (new-object Security.Cryptography.PasswordDeriveBytes $pass, $salt, "SHA1", 5). GetBytes (+) #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 $cs
Write-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 $parentPath 2, ""}
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 "There is no project information required to operate in the $parentPath, please confirm and then execute"
Pause
Break
}
if ($projectNames. GetType (). IsArray) {
Write-warning in order to avoid misoperation, please project-by-item backup porting, thank you!
Pause
Break
}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 Backup information has been stitched up, whether to log on to the server for backup (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 _command
Recycle-session $computers
if ($?) {
Write-host file backup completed-foregroundcolor Green
Remove-item $linuxPath-force-erroraction Stop
}
Break
}
N{return $false}
Default{return $false}
}
#准备移植
Write-host "$projectNames project configuration information is as follows:"-foregroundcolor Yellow
$projectConfigureInfo |select project,iplist,port,account,webpath|ft
$RH =read-host Please confirm whether to start uploading files (y/n)
Switch ($RH) {
y{
if ($computers. GetType (). IsArray) {
foreach ($computer in $computers) {
$OPENSTR = "Open" + "sftp://" + $user + ":" + $passwd + "@" + $computer + ":" + $port
D:\tools\winscp520\winscp.exe/console/command "option Batch Continue" "option confirm off" $openstr "option transfer bin ary "Put $parentPath *" # "Exit"
}
}else{
$OPENSTR = "Open" + "sftp://" + $user + ":" + $passwd + "@" + $computers + ":" + $port
D:\tools\winscp520\winscp.exe/console/command "option Batch Continue" "option confirm off" $openstr "option transfer bin ary "Put $parentPath *" # "Exit"
}
Break
}
N{return $false}
Default{return $false}
}

}else{
Write-warning "No configuration information matched to the project $projectNames, check the configuration file $script:p rojectconfigurefile"
Pause
Break
}
}
}


Upload

#>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Automatic deployment of Powershell

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.