英文:
PowerShell script - Extract, Copy and delete zip file not working
问题
# 创建 Java 版本目录,如果不存在的话,位于 C:\Users\$env:UserName\test\java
if (-not (Test-Path $Folder)) {
try {
New-Item -Path $Folder -ItemType Directory -ErrorAction Stop | Out-Null
}
catch {
Write-Error -Message "无法创建目录 'C:\Program Files\test'。错误信息: $_" -ErrorAction Stop
}
}
# 从内网下载文本文件
Invoke-WebRequest http://10.1.48.25/test/version.txt -OutFile "C:\Users\$env:UserName\test\java\version.txt"
# 从 version.txt 文件中检查 Java 版本
If (Get-Content $IntranetTextFile | %{$_ -match $JavaVersion}) {
echo "版本相同"
}
else {
# 下载 Java 文件
Invoke-WebRequest $JavaDownloadPath -OutFile $JavaZipFilePath
}
# 在 C:\Users\$env:UserName\test\java\jdk-11.0.6 中解压缩 zip 文件
if (-not (Test-Path "C:\Users\$env:UserName\test\java\jdk-11")) {
Expand-Archive "C:\Users\$env:UserName\test\java\openjdk-11+28_windows-x64_bin.zip" -DestinationPath "C:\Users\$env:UserName\test\java"
}
# 将 JDK zip 文件复制到 C:\Program Files\Java
Copy-Item -Path .\*.zip -Destination $ExpandArchiveDestinationPath -Force
# 在解压缩后删除 zip 文件
Remove-Item $JavaZipFilePath -Force
问题:
-
当 Java 版本相同时,脚本会下载 Java zip 文件,但实际上我不想要这个行为!
目标是检查版本是否相同,如果相同,则显示 "相同版本";如果不同,则下载 zip 文件。 -
当版本不同时,脚本没有执行解压缩、复制和删除操作。
我对 PowerShell 完全不熟悉,请问如何解决上述问题?
英文:
> I have build a Power Shell script which should do the following
>
- create java version directory if not exists
- Download text file from intranet
- Check java version from downloaded text file
- If version not same download java zip file online
- Extract the java zip file
- Copy the JDK zip file to C:\Program Files\Java
- Delete the zip file after extraction
> Below is my Power Shell script
>
$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 {
Write-Error -Message "Unable to create directory "C:\Program Files\test". Error was: $_" -
ErrorAction Stop
}
}
# Download text file from intranet
Invoke-WebRequest http://10.1.48.25/test/version.txt -OutFile
"C:\Users\$env:UserName\test\java\version.txt"
# Check java version from version.txt
If (Get-Content $IntranetTextFile | %{$_ -match $JavaVersion})
{
echo Version Same
}
else
{
# Download Java file
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")) {
Expand-Archive "C:\Users\$env:UserName\test\java\openjdk-11+28_windows-x64_bin.zip" -DestinationPath
"C:\Users\$env:UserName\test\java"
}
# 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
>
> My problem is:
- when java version is same above script is downloading java zip file which I didn't want!
> The aim was to check if version same then display "Same Version"
> if not, download zip file
- When version not same it is not extracting the zip file, not copying the file and not deleting it.
> I'm completely new to power shell any idea about how I can solved above issues.
答案1
得分: 0
如果无论如何都在下载,那么验证功能未按预期运行。
我进行了一些更改,如您所见,一些语法修复等。
然而,当然,我无法测试这个,因为我不知道您的$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"
# 如果不存在,则在C:\Users\$env:UserName\test\java中创建java版本目录
if (-not (Test-Path $Folder))
{
try
{
New-Item -Path $Folder -ItemType Directory -ErrorAction Stop |
Out-Null #-Force
}
catch
{
$WriteErrorSplat = @{
Message = "无法创建目录C:\Program Files\$Folder。错误是:$_"
ErrorAction = 'Stop'
}
Write-Error @WriteErrorSplat
}
}
# 从内部下载文本文件
$InvokeWebRequestSplat = @{
Uri = 'http://10.1.48.25/test/version.txt'
OutFile = "C:\Users\$env:UserName\test\java\version.txt"
}
Invoke-WebRequest @InvokeWebRequestSplat
# 检查version.txt中的java版本
如果 ((Get-Content $IntranetTextFile) -match $JavaVersion)
{Write-Warning -Message 'Java版本已存在。不需要下载。'}
else {Invoke-WebRequest $JavaDownloadPath -OutFile $JavaZipFilePath}
# 在C:\Users\$env:UserName\test\java\jdk-11.0.6中提取zip文件
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
# 将JDK zip文件复制到C:\Program Files\Java
Copy-Item -Path '.\*.zip' -Destination $ExpandArchiveDestinationPath -force
# 提取后删除zip文件
Remove-Item $JavaZipFilePath -Force
英文:
If it is downloading anyway, then the validation is not functioning as expected.
I made a few changes, as you can see, some syntax fixups, etc.
Yet, of course, I can't test this as I don't know what is in your $IntranetTextFile, nor do I have a similar environment to check against.
$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
专注分享java语言的经验与见解,让所有开发者获益!
评论