您的计算机上有应用程序,例如Spotify、Adobe Reader、Chrome、Firefox、7-Zip、VLC 媒体播放器……(VLC Media Player…the list)不胜枚举,而且它们都是免费的。没有免费软件(freeware)我们该怎么办?然而,这些应用程序需要经常更新以修复安全漏洞并引入新的有用功能。更新永远不会在方便的时候发生。
这就是Chocolatey发挥作用的地方。使用免费应用程序Chocolatey使您的软件保持最新。它简单、快速、简单,而且我们提到过免费吗?看起来(Seems)好得令人难以置信,但事实就是如此。但什么是巧克力(Chocolatey)?
什么是巧克力味?
如果您还没有在Windows 命令行(Windows command line)、PowerShell或Linux 命令行(Linux command line)中做过任何事情, Chocolatey的概念可能会有点奇怪。Chocolatey是一个机器级程序,可帮助您管理软件(manage software)。
机器级意味着没有图形用户界面(user interface)可以帮助您做事。必须键入所有命令。不要让(t let)那把你吓跑(Don)!如果你会写字,你就能掌握这个。
如何安装 Chocolatey?
Chocolatey需要做几件事。当一个程序需要其他Windows功能的程序才能工作时,这些需要的程序称为依赖项。Chocolatey的依赖项是您必须至少使用:
- Windows 7 或更新版本
- PowerShell 版本 2 或更高版本
- .NET Framework 4 或更高版本
- 另外,您必须在计算机上具有管理员访问权限(administrator access)
如果您拥有Windows 7或更高版本,则您已经拥有
PowerShell 版本 2(PowerShell version 2)或更高版本。如果您不确定您是否拥有 .NET Framework 4,请不要担心。安装Chocolatey时,如果您没有 .NET 4,Chocolatey
将为您安装它。
通过在“开始”菜单(Start Menu)的搜索框PowerShell中键入来打开PowerShell 。您应该会看到一个名为Windows PowerShell App的结果。
右键单击它并选择Run as Administrator。这样做称为在提升的实例中运行(elevated instance)PowerShell。它被提升为管理员权限。PowerShell 窗口(PowerShell window)将打开。
在PowerShell 窗口(PowerShell window)中,键入或复制(type or copy)并粘贴以下命令:
Set-ExecutionPolicy Bypass -Scope Process -Force; iex
((New-Object
System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’))
Set-ExecutionPolicy Bypass -Scope Process -Force部分
告诉PowerShell,您不想为接下来的事情强制执行受限执行策略。默认情况下, PowerShell(PowerShell)只允许签名进程运行。这是最高的安全设置(security setting)。但是我们需要运行这个未签名的安装Chocolatey的过程。
该命令的iex ((New-Object
System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’))
部分告诉PowerShell去Chocolatey 网站(Chocolatey website),下载安装.ps1 脚本,然后运行它。那是真正安装
Chocolatey的部分。
你会看到一堆文字飞过(text fly)。随时(Feel)回去阅读它,以便您知道刚刚发生了什么。最后,它建议您运行命令choco /? 查看功能列表。这是确保您的安装正常工作的好方法。继续运行该命令。
又是一堆文字飞掠而过,也很好读懂。最后,如果您的Chocolatey安装成功,您将看到类似以下内容,其中绿色部分列出了您刚刚安装的版本。
用 Chocolatey 安装软件
在我们开始使用 Chocolatey 更新软件之前,让我们快速浏览一下使用
Chocolatey安装软件。(Chocolatey)
转到页面https://chocolatey.org/packages浏览可通过Chocolatey获得的软件。您会看到它们被称为包。
找到您想要安装的软件包。对于此示例,我们将使用Malwarebytes Anti-Malware。在您的计算机上安装 Malwarebytes 总是好的。
在提升的PowerShell实例中,使用命令choco installmalwarebytes。就是这样。安装将开始。它将以以下文本停止:
如果您愿意让Chocolatey管理您的软件,那么在安装完成后,我们应该使用choco feature enable -n allowGlobalConfirmation命令,以便我们以后可以自动安装和更新。现在,让我们使用A来完成安装。
您会看到Malwarebytes正在下载,然后开始安装。
大约一分钟后,Malwarebytes将被下载并安装,无需您进行任何进一步的工作。
用 Chocolatey 更新软件
现在我们已经安装了一些软件,我们可以尝试使用Chocolatey更新它。同样(Again),您需要以管理员(Administrator)身份打开PowerShell。然后你可以运行命令choco upgrademalwarebytes。
这将使Chocolatey出去看看是否有更新,然后更新它。我们刚刚安装了 Malwarebytes,因此它会显示零一包已更新。没关系。
如果您已经使用Chocolatey(Chocolatey)安装了多个软件包,则可以使用一行命令将它们全部更新:choco upgrade all -y。
这很难。现在我们必须用Chocolatey自动更新软件,以某种方式使该命令按计划运行。
使用Chocolatey自动更新软件(Update Software)
下一步您可以使用 wither Notepad或 PowerShell ISE(集成脚本环境(Integrated Scripting Environment))。我们将使用记事本(Notepad)执行此示例,因为并非所有Windows版本都具有PowerShell ISE。
打开记事本。将命令choco upgrade all -y复制到记事本中。
通过将其命名为upgrade-ChocoPackages.ps1将其保存为PowerShell 脚本(PowerShell script)。.ps1 扩展名告诉 Windows 这是一个 ? PowerShell 脚本(PowerShell script)。
将文件类型从Text Documents (*.txt)更改为All Files (*.*).如果您不这样做,Windows将使其升级-ChocoPackages.ps1.txt(upgrade-ChocoPackages.ps1.txt)并认为它只是一个记事本文件(Notepad file)。
Windows 带有一个很棒的功能,称为任务计划程序(Task Scheduler)。普通家庭用户(home user)可能不知道它,但任务计划程序(Task Scheduler)这个功能已经让很多事情定期运行。
在任务计划程序(Task Scheduler)的开始菜单中搜索(Start Menu search)。结果出现时单击(Click)它。
当任务计划程序(Task Scheduler)打开时,单击窗口左上角的任务计划程序库。(Task Scheduler Library)这将向您显示当前在您的计算机上设置的所有计划任务。
在右上角区域的“操作”窗格中,单击“(Actions)创建任务(Create Task…)”…… 。Create Basic Task(Create Basic Task isn)不适合这种情况,因为我们稍后将使用参数语句。
在打开的窗口中,在General选项卡中,为任务命名,如Choco Upgrade All和描述(Description ),如Updates all software installed by Chocolatey。如果您当前的用户帐户(user account)不是计算机上的管理员帐户(administrator account),请使用更改用户或组(Change User or Group)按钮选择管理员帐户(Administrator account)。
您还需要知道管理员帐户(Administrator account)的密码。确保(Make)无论用户是否登录(Run whether the user is logged on or not)都选择运行。即使您不在计算机上,这也将允许脚本运行,并且它将拥有完成工作所需的所有权限。
触发器(Triggers)选项卡是您告诉任务何时运行的地方。为此,每周一次就足够了。在我们的示例中,它设置为每周日(Sunday)凌晨 1:00 运行 选择一个您不太可能使用计算机的时间,以获得最佳性能。
如果任务运行时间超过:(Stop the task if it runs longer than:) ,建议同时选中停止任务,并将持续时间更改为 2 小时。您可以根据需要进行调整。要允许应用触发器,您必须选中底部的启用框。(Enabled)
转到Actions选项卡,我们将告诉任务我们想要它做什么。Action将默认为Start a program。这就是我们想要的,所以就离开吧。在Program/script字段中,键入 powershell.exe。这让 Windows 知道您将要运行
PowerShell 脚本(PowerShell script)。
在添加参数(Add arguments)字段中,输入以下参数。
-noprofile -这会阻止PowerShell 配置文件(PowerShell profile)脚本运行,并告诉它只运行您想要的脚本。
-ExecutionPolicy Bypass –如果您不确定是否启用了脚本执行,最好将其包含在参数中。它将确保脚本运行。
-file - 这是告诉任务计划程序接下来是我们希望
PowerShell运行的文件的路径的参数。在我们的示例中,脚本被保存到C:\Scripts\upgrade-ChocoPackages.ps1。在您的计算机上可能会有所不同,因此请进行相应调整。如果文件的路径名称中包含空格,则需要将整个路径放在引号内。
完整的参数看起来像-noprofile
-executionpolicy bypass -file C:\Scripts\upgrade-ChocoPackages.ps1
在条件(Conditions )选项卡上,我们可以设置更多关于脚本运行方式的选项。看看他们,看看你想申请哪些。对于此示例,它设置为仅在计算机使用交流电源时启动任务,(Start the task only if the computer is on AC power)并在计算机切换到电池电源时停止(Stop if the computer switches to battery power )以确保我们不会耗尽电池。
选择唤醒计算机运行此任务(Wake the computer to run this task)以确保任务运行,无论计算机是否处于睡眠模式。
在Settings选项卡中,建议选中Allow task to be run on demand框,以便我们可以在完成后手动测试任务。其余部分的默认选择很好。
单击“确定(OK)”完成定时任务的创建。应弹出一个窗口,其中包含您在“常规”选项卡上选择的用户名。您必须输入用户密码并单击(user and click) OK。这告诉 Windows 您确实有权运行该任务。
现在您回到主任务计划程序窗口(Task Scheduler window)。找到你的新任务。右键单击任务并选择(task and select) 运行(Run )以对其进行测试。
您不会看到任何重要的事情发生,除了任务的状态将更改为Running。大约一分钟后,您应该会看到Last Run Time也更改为您开始运行任务时的时间戳。
如果您没有收到任何错误消息,则该任务应该没问题。关闭任务计划程序窗口(Task Scheduler window),不必担心必须再次手动更新您使用Chocolatey安装的任何软件。
全做完了!
设置它似乎需要做很多工作。考虑一下:设置它需要 10 到 30 分钟。如果您使用它来更新 10 个程序,并且每个程序每个月需要大约 6 分钟来完成更新过程,那么您已经为自己节省了 30 到 50 分钟。
您已经在节省时间方面领先了。在一年的时间里,这可能会节省 6 到 10 个小时的时间。这还不包括知道如何使用Chocolatey在一两分钟内而不是 10 或 15 分钟内安装程序所节省的时间。
How to Automatically Update Free Software with Chocolatey
You’ve got apps on your сomputer like Spotify, Adobe Reader, Chrome, Firefox, 7-Zip, VLC Media Player…the list goes on and they’re all free. What would we do without freeware? Yet, these apps require frequent updates to fix security holes and introduce new and useful features. Updates never happen at a convenient time.
That’s where Chocolatey comes into play. Use the free app Chocolatey to keep your software up to date. It’s easy, fast, simple, and did we mention free? Seems too good to be true, yet it is. But what is Chocolatey?
What is Chocolatey?
If you haven’t done things in the Windows command line, PowerShell, or Linux command line, the concept of Chocolatey can be a bit weird. Chocolatey is a machine-level program that helps you manage software.
Machine-level meaning that there isn’t a graphical user interface that helps you do things. All the commands must be typed. Don’t let that scare you away! If you can write words, you can master this.
How do I Install Chocolatey?
There are a couple things Chocolatey needs to work. When a
program needs other programs of Windows features to work, those needed programs
are referred to as dependencies. Chocolatey’s dependencies are you must be
using at least:
- Windows 7 or newer
- PowerShell version 2 or newer
- .NET Framework 4 or newer
- Plus, you must have administrator access on your computer
If you’ve got Windows 7 or newer, you’ve already got
PowerShell version 2 or newer. If you aren’t sure if you have .NET Framework 4,
don’t worry. When installing Chocolatey, if you don’t have .NET 4, Chocolatey
will install it for you.
Open PowerShell by typing in the Start Menu’s search
box PowerShell. You should see a result come up named Windows PowerShell App.
Right-click on that and select Run as Administrator.
Doing this is known as running PowerShell in an elevated instance. It’s
elevated to the Administrator’s privileges. The PowerShell window will open.
In the PowerShell window, type or copy and paste the command:
Set-ExecutionPolicy Bypass -Scope Process -Force; iex
((New-Object
System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’))
The Set-ExecutionPolicy Bypass -Scope Process -Force
part tells PowerShell that you don’t want to enforce the restricted execution
policy for just this next thing. PowerShell, by default, will only allow signed
processes to run. It’s the highest security setting. But we need to run this
unsigned process of installing Chocolatey.
The iex ((New-Object
System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’))
part of the command tells PowerShell to go to the Chocolatey website, download
the install.ps1 script, and run it. That’s the part that really installs
Chocolatey.
You’ll see a bunch of text fly by. Feel free to go back and
read it so you know what just happened. At the end, it suggests that you run
the command choco /? to see a list of functions. That’s a good way to
make sure your install worked. Go ahead and run that command.
Another pile of text will fly by, which is also good to read
and understand. At the end, if your install of Chocolatey worked, you’ll see something
like the following, where the green part lists what version you’ve just
installed.
Installing Software with Chocolatey
Let’s have a quick look at installing software with
Chocolatey before we get to updating software with it.
Go to the page https://chocolatey.org/packages to browse through the software that is available via Chocolatey. You’ll see that they are referred to as packages.
Find a package that you’d like to install. For this example, we’ll use Malwarebytes Anti-Malware. It’s always good to have Malwarebytes on your computer.
In an elevated instance of PowerShell, use the command choco
install malwarebytes. That’s it. The install will begin. It will stop with
the following text:
If you’re comfortable allowing Chocolatey to manage your
software, then when this install is done, we should use the choco feature
enable -n allowGlobalConfirmation command so we can automate installations
and updates in the future. For now, let’s just use A to complete the
installation.
You’ll see that Malwarebytes is downloading and then starts installing.
In about a minute, Malwarebytes will be downloaded and installed without any further work for you.
Update Software with Chocolatey
Now that we’ve got some software installed, we can try updating it with Chocolatey. Again, you need PowerShell open as Administrator. Then you can run the command choco upgrade malwarebytes.
That will make Chocolatey go out and see if there’s an update and then update it. We just installed Malwarebytes, so it will show that zero out one packages were updated. That’s okay.
If you’ve installed several packages with Chocolatey, you
can update them all with a one-line command: choco upgrade all -y.
That’s as hard as it gets. Now what we must do to
automatically update software with Chocolatey is to somehow make that command run
on a schedule.
Automatically Update Software with Chocolatey
The next step you can do using wither Notepad or PowerShell
ISE (Integrated Scripting Environment). We’ll do this example using Notepad as
not all Windows versions have the PowerShell ISE.
Open Notepad. Copy the command choco upgrade all -y into
Notepad.
Save that as PowerShell script by naming it something like upgrade-ChocoPackages.ps1. The .ps1 extension tells Windows that this is a PowerShell script.
Change the file type from Text Documents (*.txt) to All Files (*.*). If you don’t, Windows will make it upgrade-ChocoPackages.ps1.txt and think it’s just a Notepad file.
Windows comes with a great feature called Task Scheduler.
The average home user probably doesn’t know about it, but Task Scheduler is the
feature that already makes a lot of things run on a regular basis.
In Start Menu search on Task Scheduler. Click on it when it
comes up as a result.
When Task Scheduler opens, click on Task Scheduler Library in the top-left area of the window. This will show you all the scheduled tasks currently set up on your computer.
In the Actions pane in the top-right area, click on Create Task…. Create Basic Task isn’t suitable for this situation, as we’ll be using argument statements later.
In the window that opens, in the General tab, give the task a name like Choco Upgrade All and then a Description like Updates all software installed by Chocolatey. If your current user account is not an administrator account on your computer, use the Change User or Group button to select the Administrator account.
You’ll also need to know the Administrator account’s password. Make sure Run whether the user is logged on or not is selected. This will allow the script to run even if you’re not on your computer and it will have all the privileges it needs to do the job.
The Triggers tab is where you tell the task when you want it to run. For this, once a week should be plenty. In our example, it’s set to run every Sunday at 1:00 a.m. Pick a time when you’re not likely to be using your computer, for best performance.
It’s recommended to also check Stop the task if it runs longer than: and change the duration to 2 hours. You can adjust that as you’d like. To allow the trigger to apply, you must check the Enabled box at the bottom.
Over to the Actions tab, and we’ll tell the task what
we want it to do. The Action will be defaulted to Start a program.
That is what we want so just leave that. In the Program/script field,
type powershell.exe. This lets Windows know you’re going to be running a
PowerShell script.
In the Add arguments field, enter the following
arguments.
-noprofile – This prevents PowerShell profile scripts
from running and tells it to just run the script that you want.
-ExecutionPolicy Bypass – If you’re not sure if
script execution was enabled, it’s good to have this in the arguments. It will
ensure the script runs.
-file – This is the argument that tells Task
Scheduler that whatever follows next is the path to the file that we want
PowerShell to run. In our example, the script was saved to C:\Scripts\upgrade-ChocoPackages.ps1.
It may be different on your computer, so adjust accordingly. If the path to
your file has any names with spaces in them, you’ll need to put the entire path
inside of quotes.
The full argument will look like -noprofile
-executionpolicy bypass -file C:\Scripts\upgrade-ChocoPackages.ps1
On the Conditions tab, there are more options we can set on how the script is to run. Look at them to see which ones you’d like to apply. For this example, it is set to Start the task only if the computer is on AC power and Stop if the computer switches to battery power to ensure we’re not running down the battery.
Wake the computer to run this task is selected to ensure the task runs, whether the computer is in sleep mode or not.
In the Settings tab, it’s recommended to check the Allow
task to be run on demand box, so that we can manually test the task when
we’re done. The default selections for the remainder are fine.
Click OK to finish creating the scheduled task. A
window should pop up with the name of the user you selected back on the General
tab. You must enter the password for the user and click OK. This tells
Windows that you do, indeed, have the authority to run the task.
Now you’re back to the main Task Scheduler window. Find your
new task. Right-click on the task and select Run to test it.
You won’t see anything significant happen, except the status
of the task will change to Running. In a minute or so, you should see
the Last Run Time also change to the timestamp when you started running
the task.
If you didn’t get any error messages, the task should be
fine. Close the Task Scheduler window and don’t worry about having to manually
update any of the software you installed with Chocolatey again.
All Done!
It may seem like a lot of work to set this up. Consider this: it took you somewhere between 10 and 30 minutes to set this up. If you’re using this to update 10 programs, and each program takes about 6 minutes to go through the updating process every month, you’ve saved yourself between 30 and 50 minutes.
You’re already ahead in time saved. Over the course of a year, that could be 6 to 10 hours of time saved. That’s not including the time saved by knowing how to use Chocolatey to install programs in a minute or two instead of 10 or 15 minutes.