Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
Java PowerShell脚本-解压缩、复制和删除zip文件无效_Java_Powershell_Powershell 2.0 - Fatal编程技术网

Java PowerShell脚本-解压缩、复制和删除zip文件无效

Java PowerShell脚本-解压缩、复制和删除zip文件无效,java,powershell,powershell-2.0,Java,Powershell,Powershell 2.0,我已经构建了一个powershell脚本,它应该执行以下操作 如果不存在,则创建java版本目录 从内部网下载文本文件 从下载的文本文件中检查java版本 如果版本不同,请在线下载java zip文件 提取javazip文件 将JDK zip文件复制到C:\Program Files\Java 提取后删除zip文件 下面是我的powershell脚本 我的问题是: 当java版本相同时,上面的脚本正在下载我不想要的java zip文件 目的是检查版本是否相同,然后显示“相同版本” 如果没有,请下

我已经构建了一个powershell脚本,它应该执行以下操作

  • 如果不存在,则创建java版本目录
  • 从内部网下载文本文件
  • 从下载的文本文件中检查java版本
  • 如果版本不同,请在线下载java zip文件
  • 提取javazip文件
  • 将JDK zip文件复制到C:\Program Files\Java
  • 提取后删除zip文件
  • 下面是我的powershell脚本

    我的问题是:

  • 当java版本相同时,上面的脚本正在下载我不想要的java zip文件 目的是检查版本是否相同,然后显示“相同版本” 如果没有,请下载zip文件

  • 当版本不相同时,它不会提取zip文件,不会复制文件,也不会删除它
  • 我对power shell完全陌生,不知道如何解决上述问题


    如果仍在下载,则验证未按预期运行

    我做了一些修改,如你所见,一些语法修正,等等

    然而,当然,我不能测试这个,因为我不知道$IntranetTextFile中有什么,也没有类似的环境可供检查

    $JavaVersion                  = 'java.version=11.0.6'
    $Folder                       = "C:\Users\$env:UserName\test\java"
    $IntranetUrl                  = 'http://10.1.48.25/test/version.txt'
    $IntranetTextFile             = "C:\Users\$env:UserName\test\java\version.txt"
    $JavaDownloadPath             = 'http://10.1.48.25/test/install/Inst_20.05.00.01/testDesktop/OpenJDK11U-jdk_x64_windows_hotspot_11.0.6_10.zip'
    $JavaZipFilePath              = "C:\Users\$env:UserName\test\java\openjdk-11+28_windows-x64_bin.zip"
    $ExpandArchivePath            = "C:\Users\$env:UserName\test\java\*.zip"
    $ExpandArchiveDestinationPath = "C:\Users\$env:UserName\test\java"
    
    # create java version directory in C:\Users\$env:UserName\test\java if it does not exist
    
    if (-not (Test-Path $Folder)) 
    {
        try 
        {
            New-Item -Path $Folder -ItemType Directory -ErrorAction Stop | 
            Out-Null #-Force
        }
        catch 
        {
            $WriteErrorSplat = @{
                Message     = "Unable to create directory C:\Program Files\$Folder. Error was: $_"
                ErrorAction = 'Stop'
            }
            Write-Error @WriteErrorSplat
        }
    } 
    
    # Download text file from intranet 
    $InvokeWebRequestSplat = @{
        Uri     = 'http://10.1.48.25/test/version.txt'
        OutFile = "C:\Users\$env:UserName\test\java\version.txt"
    }
    Invoke-WebRequest @InvokeWebRequestSplat
    
    # Check java version from version.txt
    If ((Get-Content $IntranetTextFile) -match $JavaVersion) 
    {Write-Warning -Message 'Java Version already exits. Download not needed.'}
    else {Invoke-WebRequest $JavaDownloadPath -OutFile $JavaZipFilePath}
    
    #Extract the zip file in C:\Users\$env:UserName\test\java\jdk-11.0.6
    
    if (-not (Test-Path "C:\Users\$env:UserName\test\java\jdk-11")) 
    {
        $ExpandArchiveSplat = @{
            Path            = "C:\Users\$env:UserName\test\java\openjdk-11+28_windows-x64_bin.zip" 
            DestinationPath = "C:\Users\$env:UserName\test\java"
        }
    }
    Expand-Archive @ExpandArchiveSplat
    
    # Copy the JDK zip file to C:\Program Files\Java
    Copy-Item -Path '.\*.zip' -Destination $ExpandArchiveDestinationPath -force
    
    #Delete the zip file after extraction
    Remove-Item $JavaZipFilePath -Force
    
    $JavaVersion                  = 'java.version=11.0.6'
    $Folder                       = "C:\Users\$env:UserName\test\java"
    $IntranetUrl                  = 'http://10.1.48.25/test/version.txt'
    $IntranetTextFile             = "C:\Users\$env:UserName\test\java\version.txt"
    $JavaDownloadPath             = 'http://10.1.48.25/test/install/Inst_20.05.00.01/testDesktop/OpenJDK11U-jdk_x64_windows_hotspot_11.0.6_10.zip'
    $JavaZipFilePath              = "C:\Users\$env:UserName\test\java\openjdk-11+28_windows-x64_bin.zip"
    $ExpandArchivePath            = "C:\Users\$env:UserName\test\java\*.zip"
    $ExpandArchiveDestinationPath = "C:\Users\$env:UserName\test\java"
    
    # create java version directory in C:\Users\$env:UserName\test\java if it does not exist
    
    if (-not (Test-Path $Folder)) 
    {
        try 
        {
            New-Item -Path $Folder -ItemType Directory -ErrorAction Stop | 
            Out-Null #-Force
        }
        catch 
        {
            $WriteErrorSplat = @{
                Message     = "Unable to create directory C:\Program Files\$Folder. Error was: $_"
                ErrorAction = 'Stop'
            }
            Write-Error @WriteErrorSplat
        }
    } 
    
    # Download text file from intranet 
    $InvokeWebRequestSplat = @{
        Uri     = 'http://10.1.48.25/test/version.txt'
        OutFile = "C:\Users\$env:UserName\test\java\version.txt"
    }
    Invoke-WebRequest @InvokeWebRequestSplat
    
    # Check java version from version.txt
    If ((Get-Content $IntranetTextFile) -match $JavaVersion) 
    {Write-Warning -Message 'Java Version already exits. Download not needed.'}
    else {Invoke-WebRequest $JavaDownloadPath -OutFile $JavaZipFilePath}
    
    #Extract the zip file in C:\Users\$env:UserName\test\java\jdk-11.0.6
    
    if (-not (Test-Path "C:\Users\$env:UserName\test\java\jdk-11")) 
    {
        $ExpandArchiveSplat = @{
            Path            = "C:\Users\$env:UserName\test\java\openjdk-11+28_windows-x64_bin.zip" 
            DestinationPath = "C:\Users\$env:UserName\test\java"
        }
    }
    Expand-Archive @ExpandArchiveSplat
    
    # Copy the JDK zip file to C:\Program Files\Java
    Copy-Item -Path '.\*.zip' -Destination $ExpandArchiveDestinationPath -force
    
    #Delete the zip file after extraction
    Remove-Item $JavaZipFilePath -Force