-1
    enter code here
# Step 1) install Chocolatey when needed
if (-not (Test-Path -Path "$env:ProgramData\Chocolatey\choco.exe" -PathType Leaf)) {
   # from https://chocolatey.org/install
   Set-ExecutionPolicy Bypass -Scope Process -Force
   [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
   Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) 
}

# Step 2) define the array of packages you are offering
$Packages = 'googlechrome','firefox','codeblocks','windbg','nasm',
            'explorersuite','pestudio','vscode','sysinternals','python'

# Step 3) define the Show-Menu function
function Show-Menu {
    Clear-Host
    Write-Host "**********************************************"
    Write-Host "LIST OF SOFTWARES"
    # write the options using the array of packages
    for ($i = 0; $i -lt $Packages.Count; $i++) {
        # {0,2} means right align with spaces to max 2 characters
        Write-Host ('{0,2}. {1}' -f ($i + 1), $Packages[$i])
    }
    Write-Host " q. Exit the script"
    Write-Host "*************************************************"
    Write-Host
}

# Step 4) enter an endless loop you only exit if the user enters 'q'
while ($true) {
    Show-Menu

    $UserInput = Read-Host "Enter the software number to be installed"
    # test if the user wants to quit and if so, break the loop
    if ($UserInput -eq 'q') { break }

    # test if the user entered a number between 1 and the total number of packages (inclusive)
    if ([int]::TryParse($UserInput,[ref]$null) -and 1..$Packages.Count -contains [int]$UserInput) {
        # here you install the chosen package using the array index number (= user input number minus 1)
        $packageIndex = [int]$UserInput - 1
        Write-Host "Installing $($Packages[$packageIndex])"
        choco install $Packages[$packageIndex] -y
    }
    else {
        $availableOptions = 1..$Packages.Count -join ','
        Write-Host "Error in selection, choose $availableOptions or q" -ForegroundColor Red
    }

    $null = Read-Host "Press Enter to continue"
}

Problem Statement: I am writing power shell script using chocolatey for software downloading and installing automatically. My script will display a menu with the list of software's whenever users enter the number the corresponding software will download and install. When i run the script the executable files are not showing in control panel and desktop icon is also not creating but i am getting the message in power-shell terminal that the software is installed but i am not able to see that application. I need to put some conditions in my script they are:

  1. If the software is already installed then the software should not be downloaded
  2. When the software is installed it has to shown in control panel and desktop icon should be created.
  3. If any upgrade is their like for googlechrome and firefox generally up gradation takes place then the script has to upgrade. Please help me and edit my script if anything is wrong

Thanks in advance

  • Are you running this in the context of the user or the context of some service user which has the permission to actually touch all the paths? – Seth Sep 01 '21 at 11:32
  • As I [commented yesterday](https://stackoverflow.com/questions/68981535/powershell-script-for-software-automation#comment121939890_68984703) you need to study the chocolatey switches and config file, so choco.exe would know what you want installed/updated, what features you want for that piece of software, etc. If you want choco to do the installation, you need to instruct **it** how to do it. You can also install everything using pure PowerShell and then the code I gave you should handle all that. In this case, you're calling an external application to do the job.. – Theo Sep 01 '21 at 20:25
  • No sir, it is not like that i am new to this scripting that's why i am not able to do it. whatever the code you have given i understood but i am not able to put the logic so i am facing difficulty that's why i am repeatedly asking because i have to submit the assignment sorry sir. – Shravan Meghavath Sep 02 '21 at 05:11
  • It sounds like you are asking someone to finish your script for you. That isn't what Stack Overflow is about and asking for such is likely to see your question closed. I've provided an answer with general advice for how to achieve what you want with Chocolatey but Stack Overflow is not a code writing service. – codewario Sep 03 '21 at 04:00
  • I am getting the following message when i run my script : Software installed as 'MSI', install location is likely default but it is not showing in control panel and when i put try an catch block to check if the software is already installed or not , in my case software is already installed then also software is downloding. I will post my script Please give information regarding this problem – Shravan Meghavath Sep 03 '21 at 06:12

1 Answers1

0

Referencing this answer, check to see if the relevant software is installed by checking the following registry keys:

$InstalledSoftware = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*
$InstalledSoftware += Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*

If it's installed, you can either skip the installation altogether, or...

You can get the package managed under Chocolatey by "installing" the package anyways, but provide the -n (--skippowershell) parameter:

choco install -n packageName

This will download the package metadata but skip the actual PowerShell code (chocolateyInstall.ps1, which is embedded into all Chocolatey packages). This is the piece which runs MSIEXEC, an EXE installer, extracts zip archives for packages without a proper installer, etc. This has the benefit of allowing future updates to be manageable by Chocolatey without reinstalling the software which already exists.

Note: There may be some nuance surrounding specific packages you'll have to figure out, but for the majority of what is available as a Chocolatey package this approach will work.

codewario
  • 19,553
  • 20
  • 90
  • 159