- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- Jonas Andersson Mike Pfeiffer
- 704字
- 2021-07-16 13:26:34
Working with script repositories
Windows Management Framework Version 5 includes a package manager called PowerShellGet, which enables functionalities, such as the find
, get
, install
, and uninstall
packages from internal and public sources. However, this recipe is not specific to Exchange; see this recipe as a tips and tricks recipe, since it's more PowerShell general than Exchange-specific.
PowerShellGet is a package manager for Windows PowerShell. Basically, it is a wrapper around the OneGet component that simplifies the package management for PowerShell modules. PowerShellGet is built on top of the well-known package management solution NuGet.
OneGet is a unified package management component that allows you to search for software installation, uninstallation, and inventory for any type of software that it supports through its provider interface.
OneGet works with the community-based software repository called Chocolatey. Currently, Chocolatey has over 2,600 unique packages.
There are a bunch of galleries (also referred to as providers) to use and select from, such as PowerShell Resource Gallery (Microsoft supported), MyGet, Inedo ProGet, JFrog Artifactory, and many more.
For a better understanding, let's take a look at the first example.
How to do it...
In this example, we will use OneGet to install two example modules from Chocolatey:
Import-Module –Name OneGet Get-Command –Module OneGet Find-Package | Out-GridView Find-Package –Name "notepadplusplus" Find-Package –Name "7zip" Install-Package –Name "7zip" Install-Package –Name "notepadplusplus"–Force Get-Package
How it works...
For illustrating how OneGet works we have seen the preceding example.
First, we imported the module of OneGet to use the cmdlets for the package manager. We then used the Get-Command
cmdlet to see what commands are available with this module.
With the Find-Package
cmdlet, we searched for available packages. First, we piped the results to a GridView
, since this can be user friendly to watch instead of text. Once we find the packages we are looking for; in this example, Notepad++ and 7zip, we will use the Install-Package
cmdlet to install these packages. The following screenshot shows you when the installation had taken place, the packages that are now available for use and can be found at the start button:
Once the packages are in place, and we have verified that everything has worked as expected, we can finalize this by uninstalling them. The following are some examples of cmdlets used to uninstall packages:
Uninstall-Package –Name "notepadplusplus" Uninstall-Package –Name "7zip"
There's more…
Chocolatey is great in many ways, but most companies or at least enterprise companies probably want to have their own internal, more trusted, and reliable repository but still hosted on the Internet.
So, let's take a look at how this can be established. First, let's sign up for an account at an optional provider.
In my case, I used http://www.myget.org as the provider and created a feed when the account was created.
Now, let's see how the feed can be used as a repository. The feed that was created got an URL as https://www.myget.org/F/tlpowershell/, Once it's created, we have to register it as a repository in PowerShell using the Register-PSRepository
cmdlet:
Register-PSRepository -Name MyGet -SourceLocation ` https://www.myget.org/F/tlpowershell/api/v1 ` -PublishLocation https://www.myget.org/F/tlpowershell/ ` -InstallationPolicy Trusted Find-Package -Source MyGet
Since the MyGet
repository is brand new, there are currently no packages. So, the next action is to upload a package to MyGet
. To upload a module, the module itself should have a file extension of .psm1
together with the module manifest using an extension of .psd1
. In the manifest, you need to include the values of Author
and Description
, but I want to recommend that you also include the values of RootModule
, ModuleVersion
, and CompanyName
. The following examples show how the manifest was created and also how the modules were published to MyGet
:
New-ModuleManifest -Path ` C:\Windows\System32\WindowsPowerShell\v1.0\Modules\mailboxes.psd1` -Author "Jonas Andersson" -CompanyName "Testlabs, Inc." `-RootModule "mailboxes" -Description `"Module that lists mailboxes" -ModuleVersion "1.0" Import-Module PowerShellGet$PSGalleryPublishUri = 'https://www.myget.org/F/tlpowershell/api/v2/package' $PSGallerySourceUri = 'https://www.myget.org/F/tlpowershell/api/v2' Publish-Module -Name mailboxes -NuGetApiKey `a2d5b281-c862-4125-9523-be42ef21f55a -Repository MyGet Find-Package -Source MyGet Install-Package –Name "mailboxes" –Source MyGet
Before we end this recipe, we might want to remove the repository for some reason. This is done simply by running the following cmdlet:
Unregister-PSRepository -Name MyGet
See also
- Understanding the new execution policy
- Creating custom objects
- Working with Desired State Configuration
- Python數據分析入門與實戰
- TypeScript Blueprints
- 潮流:UI設計必修課
- 趣學Python算法100例
- 營銷數據科學:用R和Python進行預測分析的建模技術
- INSTANT Weka How-to
- 微信小程序項目開發實戰
- 蘋果的產品設計之道:創建優秀產品、服務和用戶體驗的七個原則
- TMS320LF240x芯片原理、設計及應用
- Citrix XenServer企業運維實戰
- Clojure for Machine Learning
- Visual Basic程序設計(第三版)
- Cocos2d-x by Example:Beginner's Guide(Second Edition)
- OpenCV 3.0 Computer Vision with Java
- 美麗洞察力:從化妝品行業看顧客需求洞察