different:powershell_ssh_scp
Это старая версия документа!
SCP/SSH Powershell password
- | download
Import-Module PowershellGet Set-ExecutionPolicy -ExecutionPolicy RemoteSigned #Yes Install-Module Posh-SSH #Yes
- | download
Import-Module Posh-SSH Import-Module Posh-SSH $servers = @() $servers += @{ name = 'adc01' ip = '192.168.124.11' } #$cred = Get-Credential -Credential nsroot #$cred.Password | ConvertFrom-SecureString | Set-Content ./nsroot.txt $nsUser = "nsroot" $encryptedPass = Get-Content ./nsroot.txt | ConvertTo-SecureString $cred = New-Object System.Management.Automation.PsCredential($nsUser, $encryptedPass) $backupStorePath = "./" $backup_level = "basic", "full" $doFullBackup = $true #or $false $eraseBackup = $false function remove_backup { param ( $server, $backupName ) $sshCommand = "rm system backup " + $backupName +".tgz" Write-Host($sshCommand) $sshSession = New-SSHSession -ComputerName $server.ip -Credential $cred -AcceptKey $result = Invoke-SSHCommand -Command $sshCommand -SSHSession $sshSession if ($result.ExitStatus -eq 0) { Write-Host("remove: ",$server.name,$backupname) } else { Write-Host($result.Host, $result.Output) } Remove-SSHSession -SessionId $sshSession.SessionId } function get_backup { param ( $server, $backupName, $destinationPath = "./", $erase ) $backup_file_path = "/var/ns_sys_backup/$backupName.tgz" Write-Host($backup_file_path) $result = Get-SCPItem ` -ComputerName $server.ip ` -Credential $cred ` -Path $backup_file_path ` -PathType File ` -Destination $destinationPath ` -Force if ($erase -eq $true){remove_backup -server $server -backupName $backupName} return $result } function create_backup { param ( $server, $doFullBackup = $true, $erase = $false ) $backup_name_pattern = $server.name + "_" + $(get-date -format "yyyy_MM_dd") Write-Host($server.name,$backup_name_pattern,$backup_level[$doFullBackup], $erase) $sshCommand = "create system backup " + $backup_name_pattern +" -level " + $backup_level[$doFullBackup] #Write-Host($sshCommand) $sshSession = New-SSHSession -ComputerName $server.ip -Credential $cred -AcceptKey $result = Invoke-SSHCommand -Command $sshCommand -SSHSession $sshSession if ($result.ExitStatus -eq 0) { $result = get_backup -server $server -backupName $backup_name_pattern -destinationPath $backupStorePath -erase $erase } else { Write-Host($result.Host, $result.Output) } Remove-SSHSession -SessionId $sshSession.SessionId } foreach ($server in $servers) { create_backup -server $server -doFullBackup $doFullBackup -erase $eraseBackup }
different/powershell_ssh_scp.1724066037.txt.gz · Последнее изменение: 2024/12/21 19:00 (внешнее изменение)