0

Code for my chocolatey script:

$apps=@('googlechrome','firefox','codeblocks','windbg','nasm','explorersuite','pestudio','vscode','sysinternals','python','Google Earth')

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 $apps.Count; $i++)
    {
        # {0,5} means right align with spaces to max 5 characters
        Write-Host ('{0,5}. {1}' -f ($i + 1), $apps[$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 
    }

    # testing if the user entered a number between 1 and the total number of packages (inclusive)

    if ([int]::TryParse($UserInput,[ref]$null) -and 1..$apps.Count -contains [int]$UserInput)
    {
        # here you install the chosen package using the array index number (= user input number minus 1)
        $apps = [int]$UserInput - 1

        foreach($app in $apps) 
        {
            $result = Get-InstalledApps | Where-Object { $_.DisplayName -like $app }

            if($result -eq $null) 
            {
                # get the index for the installer for this app
                $i = $apps.IndexOf($app)
                Write-Host "$app not found. Installing" $name[$i]

                (cinst $name[$i] -y)
            }
            else
            {

                $availableOptions = 1..$apps.Count -join ','
                Write-Host "Error in selection, choose $availableOptions or q" -Foreground Color Red
            }

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

    }

}
          

When I run the script I am getting the following error message:

 Enter the software number to be installed: 11
 
 LIST OF SOFTWARES

     1. googlechrome
     2. firefox
     3. codeblocks
     4. windbg
     5. nasm
     6. explorersuite
     7. pestudio
     8. vscode
     9. sysinternals
    10. python
    11. Google Earth
  q. Exit the script
*************************************************

Enter the software number to be installed: 11
googlechrome not found. Installing googleearthpro
Chocolatey v0.11.1
Installing the following packages:
googleearthpro
By installing, you accept licenses for the packages.
googleearthpro v7.3.4 already installed.
 Use --force to reinstall, specify a version to install, or try upgrade.

Chocolatey installed 0/1 packages. 
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

Warnings:
 - googleearthpro - googleearthpro v7.3.4 already installed.
 Use --force to reinstall, specify a version to install, or try upgrade.
Press Enter to continue: 
firefox not found. Installing googlechrome
Chocolatey v0.11.1
Installing the following packages:
googlechrome
By installing, you accept licenses for the packages.
GoogleChrome v93.0.4577.63 already installed.
 Use --force to reinstall, specify a version to install, or try upgrade.

Chocolatey installed 0/1 packages. 
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

Warnings:
 - googlechrome - GoogleChrome v93.0.4577.63 already installed.
 Use --force to reinstall, specify a version to install, or try upgrade.
Press Enter to continue: 
codeblocks not found. Installing firefox
Chocolatey v0.11.1
Installing the following packages:
firefox
By installing, you accept licenses for the packages.
Firefox v91.0.2 already installed.
 Use --force to reinstall, specify a version to install, or try upgrade.

Chocolatey installed 0/1 packages. 
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

Warnings:
 - firefox - Firefox v91.0.2 already installed.
 Use --force to reinstall, specify a version to install, or try upgrade.
Press Enter to continue: 
windbg not found. Installing notepadplus
Chocolatey v0.11.1
Installing the following packages:
notepadplus
By installing, you accept licenses for the packages.
notepadplus not installed. The package was not found with the source(s) listed.
 Source(s): 'https://community.chocolatey.org/api/v2/'
 NOTE: When you specify explicit sources, it overrides default sources.
If the package version is a prerelease and you didn't specify `--pre`,
 the package may not be found.
Please see https://docs.chocolatey.org/en-us/troubleshooting for more
 assistance.

Chocolatey installed 0/1 packages. 1 packages failed.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

Failures
 - notepadplus - notepadplus not installed. The package was not found with the source(s) listed.
 Source(s): 'https://community.chocolatey.org/api/v2/'
 NOTE: When you specify explicit sources, it overrides default sources.
If the package version is a prerelease and you didn't specify `--pre`,
 the package may not be found.
Please see https://docs.chocolatey.org/en-us/troubleshooting for more
 assistance.
Press Enter to continue: 

What needs to be fixed to make this work?

8
  • without seeing what choco commands you run, and not nowing the content of $apps, we won't really be able to help you.
    – SimonS
    Commented Sep 7, 2021 at 5:28
  • $apps=@('googlechrome','firefox','codeblocks','windbg','nasm','explorersuite','pestudio','vscode','sysinternals','python','Google Earth') and the choco command i am using is choco install Commented Sep 7, 2021 at 5:50
  • 1
    Oh dear... $apps = [int]$UserInput - 1 returns the index number of the chosen app, so you should capture that in a variable $index. Now you are overwriting your array of apps.. Next you call a function Get-InstalledApps which is not defined and also really unneeded, since you know by then what the user wants to install, namely $apps[$index]. What is cinst?? shouldn't that be choco. You never said what version of chocolatey you're using. For the things you want, you need Chocolatey Pro..
    – Theo
    Commented Sep 7, 2021 at 11:15
  • sir tell me where the changes to be done in script so that i will do Commented Sep 7, 2021 at 11:29
  • 1
    @Seth The OP tried to change the code I gave here, but apparently did not fully grasp what it is doing. In comments, we've explained that only the Pro version of chocolately can skip installations when certain software is already installed (to some extent..) Otherwise, he will need to have choco install all the software from the list on a fresh computer and try to figure out if those packages can be spotted elsewhere (registry perhaps) together with their verion numbers. Then you can include PS logic to prevent installation where not needed.
    – Theo
    Commented Sep 7, 2021 at 14:50

1 Answer 1

0

Currently the only error in your question is that notepadplus failed to install, with the reason:

The package was not found with the source(s) listed. Source(s): 'https://community.chocolatey.org/api/v2/'

You probably meant to install notepadplusplus?

https://community.chocolatey.org/packages/notepadplusplus

1
  • 1.Sir every time the menu is not suppose of if enter the corresponding number of the software then after installing that software again the menu has to be displayed 2. Second issue I am facing is when I enter the corresponding number of the software for example I enter 11 for google earth software to be installed but I am getting chrome not found why it is showing chrome not found . What mistake is their in my script. Commented Sep 9, 2021 at 1:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .