Powershell(Windows)下单片机的研究进展

eit6fx6z  于 2023-02-13  发布在  Windows
关注(0)|答案(1)|浏览(156)

我有一个用powershell编写的脚本,它使用scp将文件从远程系统传输到本地系统。

$results = & scp -v -T "user@host:"$remote_directory" "$local_directory" 2>&1

Write-Progress -Activity "SCP file transfer" -PercentComplete 0

这似乎给予了我存储在$results中的连接信息,而不是与正在传输的文件的进度相关的信息。我在某个地方读到,在Linux上,使用script命令可以绕过省略进度的问题,但我不知道如何在Windows上实现这一点,因为我没有“script”命令。我还希望继续使用scp,而不是rsync或任何东西。
即使在迭代$结果的时候,我所寻找的信息也找不到。任何帮助都是非常感谢的。如果需要任何进一步的信息,请告诉我。
我已经尝试过多次迭代运行scp,并使用从结果中派生的许多不同变量更新-PercentComplete,我希望能够相应地更新-PercentComplete,以通知用户文件传输的进度。

tkclm6bt

tkclm6bt1#

Install-Module -Name Posh-SSH
 Set-SCPItem -Path "$localPath" -Destination $destPathParent -NewName $destFolder -ComputerName $server -KeyFile $KeyFile -Credential $cred -AcceptKey -Force

您需要将密钥转换为openssh(如果使用密钥,则需要“-credential”来传递用户名)。进度条在模块中实现。示例:

Param (
[Parameter(Mandatory)]
$user,
[Parameter(Mandatory)]
$server,
[Parameter(Mandatory)]
$KeyFile,
[Parameter(Mandatory)]
$localPath,
[Parameter(Mandatory)]
$destPath,
$backupPath

)

$Root = $PSScriptRoot



if (!($backupPath)) {$backupPath = "$PSScriptRoot\backup"}
#"Install-Module -Name Posh-SSH"

function installModule($module){

Start-Process Powershell -ArgumentList "Install-Module -Name $module -force" -Verb RunAs -Wait

}


Function pause ($message)
{
# Check if running Powershell ISE
if ($psISE)
{
Add-Type -AssemblyName System.Windows.Forms
( $FrmMain = New-Object 'System.Windows.Forms.Form' ).TopMost = $True
[System.Windows.Forms.MessageBox]::Show($FrmMain,"$message")
}
else
{
Write-Host "$message" -ForegroundColor Yellow
$x = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
}

    

function sshShort ($sshCommand,$server,$cred,$keyfile){

$SSHsesion = New-SSHSession -ComputerName $server -KeyFile $KeyFile -Credential $cred -AcceptKey
Invoke-SSHCommand -SSHSession $SSHsesion -Command $sshCommand -TimeOut 600
$SSHsesion | Remove-SSHSession | Out-Null
}

function sshShortWithCheck ($sshCommand,$server,$cred,$keyfile){

$console = sshShort $sshCommand $server $cred $keyfile
if ($console.ExitStatus -ne 0){

write-host "Return code $sshCommand $server not 0, $console.output, script execution terminated "

pause "Return code $sshCommand $server not 0, $console.output, script execution terminated"

exit $console.ExitStatus
}
$console
}


function checkLocalPath($localPath) {

$CheckPath = Test-Path -Path $localPath -PathType Container

if (!($CheckPath)){

$message = "The local path is not a directory, the script is shutting down"
write-host $message -ForegroundColor red
pause $message
exit 2
}
$CheckPath

}

function checkDestPath($destPath){

$destPathMassiv = $destPath -split "/"

$check = ($destPathMassiv -ge 2) -or ($destPath -eq "/")
if ($check){

write-host "The destination path cannot be empty or equal to /" -ForegroundColor red
exit 2
}
$check
}

function copyFolder ($server,$cred,$keyfile,$localPath,$destPath,$backupPath){

$localPath = Split-Path $(gci $localPath).FullName[0] -Parent
$localPath
$destFolder = Split-Path $destPath -Leaf
$destPathParent = $(Split-Path $destPath -Parent) -replace "\\","/"
$moveFullPath = "\tmp\" + "$destFolder" + "_old"

$ChecklocalPath = checkLocalPath $localPath
$checkDestPath = checkDestPath $destPath

backupDestFolder $server $cred $keyfile $localPath $destPath $backupPath

$moveDestPath = sshShort "mv -f $destPath $moveFullPath" $server $cred $keyfile
$moveDestPath
$moveDestPath.output

$console = sshShort "ls $destPath" $server $cred $keyfile

if (($console.ExitStatus -ne 0) -and ($ChecklocalPath)) {

$console = sshShort "mkdir $destPath" $server $cred $keyfile
write-host "Creating the $destPath directory" -ForegroundColor Green
}

$ConsoleScp = Set-SCPItem -Path "$localPath" -Destination $destPathParent -NewName $destFolder -ComputerName $server -KeyFile $KeyFile -Credential $cred -AcceptKey -Force
$ConsoleScp
}

function backupDestFolder($server,$cred,$keyfile,$localPath,$destPath,$backupPath){
if (!(Test-Path $backupPath)){New-Item -Path $backupPath -ItemType Directory}

$destFolder = Split-Path $destPath -Leaf
$destPathParent = $(Split-Path $destPath -Parent) -replace "\\","/"
$data = get-date -Format dd_MM_yyyy_HH_mm

$console = sshShort "ls $destPath" $server $cred $keyfile
$ChecklocalPath = checkLocalPath $localPath

if (($console.ExitStatus -eq 0) -and ($ChecklocalPath)) {
try{
$ConsoleScp = Get-SCPItem -Path $destPath -Destination $backupPath -PathType Directory -ComputerName $server -KeyFile $KeyFile -Credential $cred -AcceptKey -Force
$temp = Compress-Archive -Path $("$backupPath\" + "$destFolder") -DestinationPath $("$backupPath\" + "$destFolder" + "_" +"$data.zip")
Remove-Item $("$backupPath\" + "$destFolder") -Force -Recurse
write-host "Backup of $destPath to $backupPath has been performed" -ForegroundColor Green
}
catch {
pause "The $destPath backup was not executed with the error $error[0], the script execution was terminated"
write-host "Backup $destPath was not executed with error $error[0], script execution terminated" -ForegroundColor Red
exit 2
}
}
else {
write-host "The $destPath directory is missing, no backup was made" -ForegroundColor Yellow
pause "The $destPath directory is missing, the backup has not been made, the script will continue to work"
}

}

if (!(Get-Module -Name Posh-SSH)){

$webclient=New-Object System.Net.WebClient
$webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[Net.ServicePointManager]::SecurityProtocol = "tls12"
installModule Posh-SSH
#Install-Module -Name Posh-SSH -Force
}

#$localPath = "$root\test"
#$destPath = "/tmp/test"
$pwd = ConvertTo-SecureString "empty" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $pwd

try {
copyFolder $server $cred $keyfile $localPath $destPath $backupPath
}
catch{
write-host $Error[0] -ForegroundColor red
exit 1
}

相关问题