I have a script that I am using to both backup and restore zip files from a network share and I am using the command line 7zip to do it. The first step in the restore process is that I copy to files from the backup location locally to the machine I am going to be restoring the files to. I then run the 7ip command using the remote machine's C:\temp folder as the beginning path and the folder it needs to go to (C:\users\username...) for the ending path. I am using the "-bsp1" to give the percentage output and if I run it directly from my machine to the UNC path, I can control the cursor to stay put and the percentage to continue going up. If I do it remotely, the percentages go down in a line and I have no way of taking the output as a stream and making the percentage stay put.
Here is the code when I do it directly from my machine:
& "C:\program files\7-zip\7z.exe" "x" $CopiedFolder -o"$RemoteFolder" -aoa "-bsp1" | out-string -stream | Select-String -Pattern "\d{1,3}%" -AllMatches | ForEach-Object { $_.Matches.Value } | foreach {
[System.Console]::SetCursorPosition(0, [System.Console]::CursorTop)
Write-Host -NoNewline "Unzipping [$fold]..."$_
}
Write-Host -ForegroundColor Green " Completed"
This produces the following:
Unzipping [Backup Folder]...10%
and it goes up to 100% while staying put.
The issue I have is that it is very slow running 7-zip from my machine to the UNC path of 2 machines and being the "middle man". I would like to run 7-zip directly from the machine that is getting the files moved so that it speeds up the process. I would like the output to look the same, but can't figure out how to make this work with invoke-command. Here is what I have so far and the percentages just go down in a line
$Script = {& "C:\program files\7-zip\7z.exe" "x" $using:CopiedFolder -o"$using:RemoteFolder" -aoa "-bsp1" | Select-String -Pattern "\d{1,3}%" -AllMatches | ForEach-Object { $_.Matches.Value } }
Invoke-command -Computer $Adcomp -Scriptblock $Script
This is the output:
Unzipping [Backup Folder]...0%
0%
1%
2%
3%
4%
4%
4%
5%
6%
7%
8%
9%
10%
11%
12%
13%
How do I make it so that the percentage stays right next to the "..."?
Thanks!
Just pipe the output of Invoke-Command
to the foreach
block you already have in your local code:
Invoke-command -Computer $Adcomp -Scriptblock $Script | foreach {
[System.Console]::SetCursorPosition(0, [System.Console]::CursorTop)
Write-Host -NoNewline "Unzipping [$fold]..."$_
}