Powershell脚本下载+重命名+删除+安装[需要帮助]

siotufzp  于 2023-08-05  发布在  Shell
关注(0)|答案(1)|浏览(149)

我正在运行一个PowerShell脚本,它需要:

  • 从URL链接下载
  • 检查.exe是否已经存在,如果存在,请删除它
  • 检查.exe是否不存在,然后下载到c:\users%usersprofile%\Downloads
  • 最后,在后台运行.exe作为静默

这是我正在尝试但没有得到结果的事情。

[Net.ServicePointManager]::SecurityProtocol = "Tls12"

#Parameter defined
$CurrentFile = "C:\Users\%UserProfile%\Downloads\7zip.exe"
$NewFile = "C:\Users\%UserProfile%\Downloads\7zip_old.exe"
 

#Check if file exists already
If (Test-Path $CurrentFile) {
    #Check if the Target File Exists
    If (Test-Path $NewFile) {
        #Delete the target file
        Remove-Item $NewFile
    }
    Rename-Item -Path $CurrentFile -NewName $NewFile
    Write-host "'$CurrentFile' has been renamed to '$NewFile'" -f Green
}
Else{
    Write-host "'$CurrentFile' does not exists!" -f Yellow
}


## Check the OS architecture and download the correct installer
try {
    if ([Environment]::Is64BitOperatingSystem) {
        $downloadURL = "https://www.7-zip.org/a/7z2301-x64.exe";
    }
    else {
        $downloadURL = "https://www.7-zip.org/a/7z2301.exe";
    }

    $localInstaller = "C:\Users\%UserProfile%\Downloads\7zip.exe";

    Invoke-WebRequest -Uri $downloadURL -OutFile $localInstaller;
    
}
catch {
    Write-Output "Failed to get download the 7zip installer";
    Exit 1;
}

## Attempt install
try {
    & C:\Users\%UserProfile%\Downloads\7zip.exe /s;
}
catch {
    Write-Output "Installation of 7zip has failed";
    Exit 1
}

字符串
我在期待什么:

  • 检查%usersprofile%\downloads中是否存在7zip.exe
  • 如果是,请将其删除并重新下载新.exe文件到%usersprofile%\downloads
  • 如果不存在,则将.exe文件下载到%usersprofile%\downloads

然后,在静默模式下运行%usersprofile%\downloads文件夹中的.exe
错误:
这是我目前得到的:

Action completed: Run Install 7zip Result: FAILURE Output: Action: Run Install 7zip, Result: Failed
'C:\Users\%USERPROFILE%\Downloads\7zip.exe' does not exists!
Failed to get download the 7zip installer


.exe根本没有下载

最新

当指向C:\Users\ToMyUserName\Downloads时,它可以下载文件.exe,但是,为什么使用%USERPROFILE%时不起作用??
另外,当使用$env:UserProfile时,它将指向 C:\WINDOWS\system32\config\systemprofile
解决方法:我可能需要将下载指向C:\Temp...?

tcomlyy6

tcomlyy61#

更新
由于无法将其下载到%USERPROFILE%,因此决定将其下载到C:\Temp文件夹,并且脚本按预期运行:

  • 检查C:\Temp中是否存在.exe
  • 如果是,则重命名
  • 检查old.exe是否存在,如果存在,则将其删除
  • 最后,它以静默模式安装.exe

我唯一不确定的是atm;即使已经在设备上安装了7zip管理器,安装是否会简单地重新运行?我最初的直觉确实说是的,它会重新安装安装程序。有什么想法吗
下面是工作脚本:

[Net.ServicePointManager]::SecurityProtocol = "Tls12"

## Parameters defined
$downloadDirectory = "C:\Temp"
$CurrentFile = "C:\Temp\7zip.exe"
$NewFile = "C:\Temp\7zip_old.exe"
 
## Check & Rename file if exists
If (Test-Path $CurrentFile) {
    #Check if the Target File Exists
    If (Test-Path $NewFile) {
        #Delete the target file
        Remove-Item $NewFile
    }
    Rename-Item -Path $CurrentFile -NewName $NewFile
    Write-host "'$CurrentFile' has been renamed to '$NewFile'" -f Green
}
Else{
    Write-host "'$CurrentFile' has been downloaded and will run in background!" -f Yellow
}

## Check the OS architecture and download the .exe file which is version 2301 at moment, which may change in future!
try {
    if ([Environment]::Is64BitOperatingSystem) {
        $downloadURL = "https://www.7-zip.org/a/7z2301-x64.exe";
    }
    else {
        $downloadURL = "https://www.7-zip.org/a/7z2301.exe";
    }

    $localInstaller = "C:\Temp\7zip.exe"

    Invoke-WebRequest -Uri $downloadURL -OutFile $localInstaller;
    
}
catch {
    Write-Output "Sorry, it has failed to download the 7zip file!";
    Exit 1;
}

## Call the .exe and install it in background silently
try {
    Start-Process -Wait -FilePath $localInstaller -ArgumentList "/S" -PassThru
}
catch {
    Write-Output "The installation of 7zip has failed!";
    Exit 1
}

字符串

相关问题