Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
詹金斯&x2B;PowerShell—将Zip文件转换为字节,传输字节,然后将字节重建为Zip_Powershell_Jenkins_Converter_File Transfer - Fatal编程技术网

詹金斯&x2B;PowerShell—将Zip文件转换为字节,传输字节,然后将字节重建为Zip

詹金斯&x2B;PowerShell—将Zip文件转换为字节,传输字节,然后将字节重建为Zip,powershell,jenkins,converter,file-transfer,Powershell,Jenkins,Converter,File Transfer,由于这个复杂的问题现在有了一个可行的解决方案,我想更新它以适应这个解决方案,以便其他人可以从中受益 问题 我正在与Jenkins和PowerShell合作执行一系列操作。其中一个操作是将所有关联日志文件收集并合并到一个名为的文件夹中,以便将其与原始计算机/服务器关联,压缩文件夹,以某种方式将其传输回Jenkins workspace环境,将zip存储到一个主文件夹中,以及将所述主文件夹转换为可在稍后时间/日期下载以进行调试的工件。但是,由于此操作往往涉及两跳交换,因此我遇到了许多阻碍我执行简单文

由于这个复杂的问题现在有了一个可行的解决方案,我想更新它以适应这个解决方案,以便其他人可以从中受益

问题 我正在与Jenkins和PowerShell合作执行一系列操作。其中一个操作是将所有关联日志文件收集并合并到一个名为的文件夹中,以便将其与原始计算机/服务器关联,压缩文件夹,以某种方式将其传输回Jenkins workspace环境,将zip存储到一个主文件夹中,以及将所述主文件夹转换为可在稍后时间/日期下载以进行调试的工件。但是,由于此操作往往涉及两跳交换,因此我遇到了许多阻碍我执行简单文件传输的障碍。因此,我向社会寻求帮助,希望这些人在这方面有更多的经验

到目前为止,我发现执行许多所需操作的唯一方法是通过
Invoke命令连接到目标机器;然而,没过多久,我就开始撞墙了。Jenkins和目标机器之间的文件传输问题就是这样一堵墙。我发现,通过创建一个与调用的命令相等的对象(例如:
$returnMe=Invoke command{}
),我可以从要存储在变量中的操作返回对象。这给了我一个可能的解决方案,通过会议将一个项目返回给Jenkins。。。但现在提出了一个问题:

问题:
  • 使用PowerShell,是否可以将文件夹压缩,然后转换 将该zip文件转换为要传递的对象,然后重新构建该对象 使用对象内容的zip文件?如果是,怎么做
  • 决议: 向@ArcSet发出一个特别的呼喊,以帮助它正常工作

    好的,所以这是非常可能的;然而,它需要一系列关键步骤才能实现。这是一个四到五步的过程:

    1) 将文件收集到目标文件夹并压缩文件夹 2) 将zip文件转换为易于传输的字节 3) 通过特殊的自定义对象传递这些字节 4) 解析对象以确保多余的字节不会进入文件。 5) 将字节转换回Jenkins上的zip文件

    下面是代码(带注释),说明了如何实现这一点:

    # Parse Through Each Server One By One
    ForEach ($server in $servers) {
        # Create a Variable to Take in the Invoke-Command and Receive what is Returned
        $packet = Invoke-Command -ComputerName $server -ArgumentList $server -Credential $creds -ScriptBlock {
            # Function for Creating a Zip File
            Function Create-ZipFromFolder ([string]$Source, [string]$SaveAs) {
                Add-Type -Assembly "System.IO.Compression.FileSystem"
                [IO.Compression.ZipFile]::CreateFromDirectory($Source, $SaveAs)
            }
    
            # Function for Converting Zip to Bytes For File Transfer
            Function Get-BytesFromFile($Path) {
                return [System.IO.File]::ReadAllBytes($Path)
            }
    
            # Absorb and Maske Server IP for the Server to Use For Logging Purposes and FileNaming
            $maskedAddress = $args[0] -Replace ('.+(?=\.\d+$)', 'XX.XX.XX')
    
            # Construct the Path For Consolidated Log Files
            $masterLogFolderPath = ("C:\Logs\RemoteLogs\$maskedAddress")
    
            # Series of Code to Create Log Folder, Consolidate Files, And Delete Unnecessary Data For Cleanup
    
            # Establish what to Save the Zip As. You Will Want The Path Included to Prevent it From Finding Path of Least Resistance Upon Saving
            $zipFile = ($masterLogFolderPath + ".zip")
    
            Try {
               # Here is Where We Call Our Compression Function
               Create-ZipFromFolder -Source $masterLogFolderPath -SaveAs ZipFile
    
               # Next We Convert the Zip to Bytes
               [byte[]]$FileAsBytes = Get-BytesFromFile -Path $zipFile
            }
            Catch {
                Write-Error $_.Exception.Message
            }
    
            # Now We Return the New Object to Jenkins
            return New-Object -TypeName PSCustomObject -Property @{Response=$FileAsBytes}
        }
    
        # Function to Convert the Bytes Back Into a Zip File
        Function Create-FileFromBytes([byte[]]$Bytes, [string]$SaveAs) {
            # It was Discovered that Depending Upon the Environment, Extra Bytes Were Sometimes Added
            # These Next Lines Will Help to Remove Those
            For (($k = 0), ($kill = 0); $kill -lt 1; $k++) {
                If ($Bytes[$k] -gt 0) {
                    $kill = 1
                    # Truncate the Excess Bytes
                    $newByteArray = ($Bytes)[$k..(($Bytes).Length -1)]
                }
            }
    
            # Reconstruct the Zip File
            [System.IO.File]::WriteAllBytes($SaveAs, $NewByteArray)
        }
    
        Try {
            # Call the Function to Begin Reconstruction
            Create-FileFromBytes -SaveAs "$env:Workspace\MasterLog\$maskedAddress.zip" -Bytes $packet.response
        }
        Catch {
            Write-Error $_.Exception.Message
        }
    
    现在,这只是一个基本骨架,没有所有沉重的东西,但这些是把所有东西放在一起的关键部分。在大多数情况下,遵循此公式将返回所需的结果。我希望这能帮助那些发现自己处于类似情况的人


    再次感谢您的帮助

    ,因此首先您需要创建一个zip文件。 您需要先使用添加类型加载部件系统。 使用CreateFromDirectory方法构建并保存zip文件。 使用System.IO.File类从zip读取所有字节。此时,您可以将此数据发送到另一台计算机。最后使用System.IO.File类将字节数组转换回文件

    function Create-ZipFromFrolder([string]$Source,[string]$SaveAs){
        Add-Type -assembly "system.io.compression.filesystem"
        [io.compression.zipfile]::CreateFromDirectory($Source, $SaveAs)
        return $SaveAs
    }
    
    function Get-BytesFromFile([string]$File){
        return [byte[]]$ZipFileBytes = [System.IO.File]::ReadAllBytes($File)
    }
    
    function Create-FileFromBytes([byte[]]$Bytes, [string]$SaveAs){
        return [System.IO.File]::WriteAllBytes($SaveAs, $Bytes)
    }
    
    [byte[]]$FileAsBytes = Get-BytesFromFile -File (Create-ZipFromFrolder -Source "C:\ZipFolder" -SaveAs "C:\Test\test.zip")
    
    Create-FileFromBytes -SaveAs "C:\ZipFolder\Test.zip" -Bytes $FileAsBytes
    

    因此,首先需要创建一个zip文件。 您需要先使用添加类型加载部件系统。 使用CreateFromDirectory方法构建并保存zip文件。 使用System.IO.File类从zip读取所有字节。此时,您可以将此数据发送到另一台计算机。最后使用System.IO.File类将字节数组转换回文件

    function Create-ZipFromFrolder([string]$Source,[string]$SaveAs){
        Add-Type -assembly "system.io.compression.filesystem"
        [io.compression.zipfile]::CreateFromDirectory($Source, $SaveAs)
        return $SaveAs
    }
    
    function Get-BytesFromFile([string]$File){
        return [byte[]]$ZipFileBytes = [System.IO.File]::ReadAllBytes($File)
    }
    
    function Create-FileFromBytes([byte[]]$Bytes, [string]$SaveAs){
        return [System.IO.File]::WriteAllBytes($SaveAs, $Bytes)
    }
    
    [byte[]]$FileAsBytes = Get-BytesFromFile -File (Create-ZipFromFrolder -Source "C:\ZipFolder" -SaveAs "C:\Test\test.zip")
    
    Create-FileFromBytes -SaveAs "C:\ZipFolder\Test.zip" -Bytes $FileAsBytes
    

    1) 您可以将zip文件读取为字节数组,然后将其导出。或者您可以
    将内容|输出文件
    每个单独的日志文件放入工作区,然后压缩。但是就我个人而言,我会努力解决文件传输问题<代码>复制项目
    从机器到工作区会更干净。@BenH我尝试了几十种方法让
    复制项目
    通过Jenkins工作;然而,我遇到了许多障碍。1) 在
    调用命令
    中,PowerShell失去了Jenkins工作区的作用域,2)由于PowerShell插件处于当前阶段,
    复制项
    不支持
    到会话
    ,3)由于工作环境,我无法在这些机器上使用PowerShell与IP地址通信;这个名单还有很多。虽然我很想
    Copy Item
    来完成它的设计目的,但它并不在stars1)您可以将zip文件读取为字节数组,然后将其导出。或者您可以
    将内容|输出文件
    每个单独的日志文件放入工作区,然后压缩。但是就我个人而言,我会努力解决文件传输问题<代码>复制项目从机器到工作区会更干净。@BenH我尝试了几十种方法让
    复制项目
    通过Jenkins工作;然而,我遇到了许多障碍。1) 在
    调用命令
    中,PowerShell失去了Jenkins工作区的作用域,2)由于PowerShell插件处于当前阶段,
    复制项
    不支持
    到会话
    ,3)由于工作环境,我无法在这些机器上使用PowerShell与IP地址通信;这个名单还有很多。虽然我非常希望复制项来完成它的设计目的,但它不在star中。我收到一个错误,指出“异常调用”ReadAllBytes并带有“1”参数:“空路径名是不合法的。“.是否有一个步骤遗漏了?我注意到,在函数
    Get BytesFromFile
    中,它接受了
    $File
    的参数,但使用变量
    $destination
    ,但没有显示该变量来自何处。当你运行它时,这可能是问题所在。我通过了空pat