首页 > 解决方案 > 静默安装软件的 Power shell 脚本

问题描述

    # Source file location
$source1 ="https://ftp.mozilla.org/pub/firefox/releases/11.0/win32/enUS/Firefox%20Setup%2011.0.exe"
$source2 ="https://www.fosshub.com/Code-Blocks.html?dwl=codeblocks-20.03-setup.exe"
$source3 ="https://github.com/x64dbg/x64dbg/releases/download/snapshot/snapshot_2021-07-01_23-17.zip"
$source4 ="https://www.python.org/ftp/python/3.9.6/python-3.9.6-amd64.exe"
$source5 ="https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/win64/nasm-2.15.05-installer-x64.exe"

# Destination to save the file
    $destination1 = "C:\Users\cdac\Desktop\Shravan\Softwares\firefox.exe"
    $destination2 = "C:\Users\cdac\Desktop\Shravan\Softwares\codeblocks.exe"
    $destination3 = "C:\Users\cdac\Desktop\Shravan\softwares\xdbg.zip"
    $destination4 = "C:\Users\cdac\Desktop\Shravan\softwares\python.exe"
    $destination5 = "C:\Users\cdac\Desktop\Shravan\softwares\nasm.exe"


    Invoke-WebRequest -Uri $source1 -OutFile  $destination1
    Invoke-WebRequest -Uri $source2 -OutFile  $destination2
    Invoke-WebRequest -Uri $source3 -OutFile  $destination3
    Invoke-WebRequest -Uri $source4 -OutFile  $destination4
    Invoke-WebRequest -Uri $source5 -OutFile  $destination5

 #Installing one software

    Start-Process -Wait -FilePath 'C:\Users\cdac\Desktop\Shravan\Softwares\codeblocks.exe' -ArgumentList '/silent' -PassThru 

我编写了 Powershell 脚本,它通过 URL 从 Internet 下载软件工具并存储在单独的目录中,但我无法使用 foreach 循环静默安装存储在单独目录中的所有软件。请帮助我编写脚本以使用 foreach 循环安装存储在目录中的所有软件。

感谢您

标签: powershellinstallationautomation

解决方案


没有足够的信息来准确说明这里出了什么问题——你具体遇到了什么错误?当您运行脚本时,您观察到什么行为?我的第一个猜测是您的 foreach 循环是通过安装程序名称运行的,而不是您在示例中发布的完整路径。

就是说,如果我了解您要做什么,那么这是一种快速而肮脏的方法来做我认为您所追求的事情;

# Array of installer details
[Hashtable[]]$Installers = @();
# Firefox
$Installers += @{
    SoftwareName = "Firefox"
    Url = "https://ftp.mozilla.org/pub/firefox/releases/11.0/win32/enUS/Firefox%20Setup%2011.0.exe"
    Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\firefox.exe"
    Arguments = '/s'
}
#Code Blocks
$Installers += @{
    SoftwareName = "CodeBlocks"
    Url = "https://www.fosshub.com/Code-Blocks.html?dwl=codeblocks-20.03-setup.exe"
    Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\codeblocks.exe"
    Arguments = '/silent'
}

function Install-Software([Hashtable]$installer) {
    Write-Host "Installing $($installer.SoftwareName)"
    Write-Host "Invoke-WebRequest -Uri $($installer.Url) -OutFile $($installer.Destination)"
    Write-Host "Start-Process -FilePath $($installer.Destination) -ArgumentList $($installer.Arguments) -Wait"
    
    # Remove Write Host above - uncomment the following lines:
    #Invoke-WebRequest -Uri $installer.Url -OutFile $installer.Destination
    #Start-Process -FilePath $installer.Destination -ArgumentList $installer.Arguments -Wait
}

foreach($installer in $Installers) {
    Install-Software -installer $installer
}

如果您尝试使用 PowerShell 静默打包应用程序,我建议您查看PSAppDeployToolkit - 这在我进行大量 SCCM 打包时帮助了我很多。


推荐阅读