gpt4 book ai didi

powershell - 通过PSSession下载PS <5的文件

转载 作者:行者123 更新时间:2023-12-02 23:38:09 26 4
gpt4 key购买 nike

如何通过PSSession从远程服务器下载文件?我知道PS5引入了Copy-Item -FromSession,但是本地和远程都可能未运行PS5。我的文件也很大,因此简单的Get-Content可能会出现问题。

最佳答案

您可以将远程文件读取为字节数组序列,通过远程处理将其流式传输,然后将它们组合回本地文件:

function DownloadSingleFile {
param(
[System.Management.Automation.Runspaces.PSSession] $FromSession,
[string] $RemoteFile,
[string] $LocalFile,
[int] $ChunkSize = 1mb
)
Invoke-Command -Session $FromSession -ScriptBlock {
param(
[string] $FileName,
[int] $ChunkSize
)
$FileInfo = Get-Item -LiteralPath $FileName
$FileStream = $FileInfo.OpenRead()
try {
$FileReader = New-Object System.IO.BinaryReader $FileStream
try {
for() {
$Chunk = $FileReader.ReadBytes($ChunkSize)
if($Chunk.Length) {
,$Chunk
} else {
break;
}
}
} finally {
$FileReader.Close();
}
} finally {
$FileStream.Close();
}
} -ArgumentList $RemoteFile, $ChunkSize | ForEach-Object {
$FileName = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($LocalFile)
$FileStream = [System.IO.File]::Create($FileName)
} {
$FileStream.Write($_, 0, $_.Length)
} {
$FileStream.Close()
}
}

关于powershell - 通过PSSession下载PS <5的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49754758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com