Bekijk een demo×
×

Zie NinjaOne in actie!

Door dit formulier in te dienen geef ik aan akkoord te gaan met het privacybeleid van NinjaOne.

Een eenvoudig PowerShell script voor het installeren van Chocolatey

Voor IT-professionals en Managed Service Providers (MSP’s) die meerdere systemen moeten beheren, kan het installeren van software veel tijd kosten. Het gebruik van pakketbeheerders zoals Chocolatey kan het allemaal veel eenvoudiger maken. Het kan echter ook een uitdaging zijn om ervoor te zorgen dat Chocolatey zelf is geïnstalleerd of up-to-date is. Om dit proces te stroomlijnen, hebben we een PowerShell script ontworpen dat het installeren en upgraden van Chocolatey, en daarmee gewenste applicaties, moeiteloos maakt.

Wat is Chocolatey?

Chocolatey is een opdrachtregelpakketbeheerder op machineniveau voor Windows die het beheer van software-installaties vereenvoudigt. Met Chocolatey kunt u software installeren, upgraden, configureren en verwijderen met een paar eenvoudige commando’s, waardoor processen worden geautomatiseerd en handmatige interventies niet meer nodig zijn.

Chocolatey Installatie Script

#Requires -Version 5.1

<#
.SYNOPSIS
    Install an application with Chocolatey. This can install and upgrade Chocolatey before install an application.
.DESCRIPTION
    Install an application with Chocolatey. This can install and upgrade Chocolatey before install an application.
.EXAMPLE
     -Name "git"
    Installs git via Chocolatey.
    If Chocolatey isn't installed this downloads and uses 7zip to extract Chocolatey's install zip file.
.EXAMPLE
     -Name "git" -TimeOut 10
    Installs git via Chocolatey.
    If Chocolatey isn't installed this downloads and uses 7zip to extract Chocolatey's install zip file.
    If the install takes more than 10 minutes, exit the script with an error.
.EXAMPLE
     -Name "git" -InstallUri "https://community.chocolatey.org/install.ps1"
    Installs git via Chocolatey.
    Uses a custom URL to download the Chocolatey install script.
    If Chocolatey isn't installed this downloads and uses 7zip to extract Chocolatey's install zip file.
.EXAMPLE
     -Name "git" -Upgrade
    Installs git via Chocolatey.
    Runs the upgrade command for Chocolatey to attempt to upgrade Chocolatey.
    If Chocolatey isn't installed this downloads and uses 7zip to extract Chocolatey's install zip file.
.EXAMPLE
     -Name "git" -UseNativeUnzip
    Installs git via Chocolatey.
    If Chocolatey isn't installed this does not downloads and use 7zip to extract Chocolatey's install zip file.
.OUTPUTS
    String[]
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Release Notes:
    Initial Release
    (c) 2023 NinjaOne
    By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>

[CmdletBinding()]
param (
    [Parameter()]
    [String]
    $Name,
    [Parameter()]
    [String]
    $InstallUri = "https://community.chocolatey.org/install.ps1",
    [Parameter()]
    [ValidateRange(1, 60)]
    [int]
    $TimeOut = 5,
    [Parameter()]
    [Switch]
    $UseNativeUnzip,
    [Parameter()]
    [Switch]
    $Upgrade
)

begin {
    function Test-ChocolateyInstalled {
        [CmdletBinding()]
        param()
    
        $checkPath = if ($env:ChocolateyInstall) { $env:ChocolateyInstall } else { "$env:PROGRAMDATAchocolatey" }
        $Command = Get-Command choco.exe -ErrorAction Ignore

        if ($Command.Path -and (Test-Path -Path $Command.Path)) {
            # choco is in the %PATH% environment variable, assume it's installed
            Write-Warning "'choco' was found at '$($Command.Path)'."
            $true
        }
        elseif (-not (Test-Path $checkPath)) {
            # Install folder doesn't exist
            $false
        }
        elseif (-not (Get-ChildItem -Path $checkPath)) {
            # Install folder exists but is empty
            $false
        }
        else {
            # Install folder exists and is not empty
            Write-Warning "Files from a previous installation of Chocolatey were found at '$($CheckPath)'."
            $true
        }
    }
}
process {
    if (-not $(Test-ChocolateyInstalled)) {
        # Install Chocolatey
        Write-Host "Chocolatey not installed."
        Write-Host "Installing Chocolatey."
        [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
        $ChocolateyScript = [scriptblock]::Create($(Invoke-RestMethod -Uri $InstallUri))
        try {
            if ($UseNativeUnzip) {
                $ChocolateyScript.Invoke(@("-UseNativeUnzip"))
            }
            else {
                $ChocolateyScript.Invoke()
            }
            Write-Host "Installed Chocolatey."
        }
        catch {
            Write-Error $_
            Write-Host "Failed to install Chocolatey."
            exit 1
        }
    }

    # Upgrade Chocolatey
    if ($Upgrade) {
        choco.exe upgrade chocolatey --yes --nocolor --limitoutput --no-progress
        $ExitCode = $LASTEXITCODE
    
        if ($ExitCode -eq 0) {
            Write-Host "Chocolatey Upgraded."
        }
        elseif ($ExitCode -ne 0) {
            Write-Error "Failed to upgrade Chocolatey."
            Write-Host "Installer returned exit code: $($ExitCode)"
            exit 1
        }
    }

    # Install Application
    choco.exe install $Name --yes --nocolor --limitoutput --no-progress
    $ExitCode = $LASTEXITCODE
    
    if ($ExitCode -eq 0) {
        Write-Host "Installed $Name"
        exit 0
    }
    elseif ($ExitCode -ne 0) {
        Write-Error "Failed to install $Name."
        Write-Host "Installer returned exit code: $($ExitCode)"
        exit 1
    }
    
    Write-Host "Installed $Name."
}
end {}

PowerShell Script-Parameters en hun Gebruik

Ons PowerShell-script accepteert verschillende parameters waarmee de werking kan worden geregeld:

  1. Naam: Geeft de naam op van de toepassing die u wilt installeren.
  2. InstallUri: Hiermee kunt u een aangepaste URL gebruiken om het Chocolatey installatiescript te downloaden. De standaard is de officiële Chocolatey installatiescript URL.
  3. TimeOut: Dit is de maximale tijd, in minuten, dat het script wacht tot een bewerking voltooid is voordat het afbreekt. De standaardwaarde is 5 minuten.
  4. UseNativeUnzip: Een schakelaar die, indien aanwezig, het eigen Windows hulpprogramma gebruikt om het Chocolatey installatiebestand uit te pakken.
  5. Upgrade: Een schakelaar die, indien aanwezig, Chocolatey zal upgraden naar de laatste versie voordat de applicatie wordt geïnstalleerd.

Installatievoorbeelden

Hier zijn een paar voorbeelden van hoe het script kan worden gebruikt om verschillende soorten software te installeren: Om Git te installeren, gebruikt u:

powershell 
  -Naam "git" 

Gebruik om Visual Studio Code te installeren met een aangepaste time-out van 15 minuten:

powershell 
  -Naam "visualstudiocode" -TimeOut 15 

Om Node.js te installeren en Chocolatey te upgraden voor de installatie, gebruik:

powershell 
  -Naam "nodejs" -Upgrade 

Beveiligingsoverwegingen

Hoewel het script het installatieproces kan vereenvoudigen, is het cruciaal om rekening te houden met de gevolgen voor de beveiliging. Het script downloadt en installeert software van het internet, dus moet de integriteit van de bron worden gecontroleerd. Standaard gebruikt het script de officiële URL van het Chocolatey installatiescript, een betrouwbare en veilige bron. Als u echter de ‘InstallUri’ parameter gebruikt om een aangepaste URL op te geven, zorg er dan voor dat het een betrouwbare en veilige bron is. Bovendien wordt het script uitgevoerd met de rechten van de gebruiker die het uitvoert, dus let op de rechten die u toekent. Gebruik in het ideale geval een gebruiker met de minimaal vereiste rechten om potentiële veiligheidsrisico’s te beperken. Concluderend biedt ons PowerShell script een gestroomlijnde, efficiënte manier om Chocolatey op Windows te installeren. Het is flexibel en kan worden aangepast aan uw behoeften, waardoor het een onmisbare bron is voor IT-professionals en MSP’s. Door PowerShell te gebruiken om Chocolatey te installeren, kunt u workflows verbeteren, de efficiëntie en productiviteit verhogen en tegelijkertijd de kans op fouten verkleinen. Krijg toegang tot dit script en honderden andere in NinjaOne.

Volgende stappen

Het opbouwen van een efficiënt en effectief IT-team vereist een gecentraliseerde oplossing die fungeert als uw kerndienstleveringstool. NinjaOne stelt IT-teams in staat om al hun apparaten te monitoren, beheren, beveiligen en ondersteunen, waar ze ook zijn, zonder de noodzaak van complexe on-premises infrastructuur.

Leer meer over NinjaOne Endpoint Management, bekijk een live rondleiding, of start uw gratis trial van het NinjaOne-platform

Categorieën:

Dit vindt u misschien ook leuk

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).