PowerShellGet系列(二):用PowerShellGet管理你的模块包

来源:互联网 发布:标准数据公司 编辑:程序博客网 时间:2024/05/22 04:45

转载请注明出处,不胜感激!关于PowerShell的文章,我也同步发布在我的微信公共订阅号里,欢迎关注:[一起PowerShell]

上一篇在PowerShellGet系列中【PowerShellGet系列(一):PowerShell的最佳管理能手】文章中,我们介绍了有关什么是PowerShellGet,今天这篇我们就来看看如何操作PowerShellGet模块里的命令。

不过在正式介绍里面的相关命令操作时,先让我们来简单回顾一下什么是PowerShellGet。

PowerShellGet是可以用来查找,安装,更新,管理等操作PowerShell模块与脚本的一个套件包,也就是所谓的管理PowerShell的包管理器。PowerShellGet里内置了一系列用于管理PowerShell模块与脚本的相关命令。用户可以使用PowerShellGet内的相关命令获取在线的PowerShell模块和脚本。

大家知道了什么是PowerShellGet,按照上面解释的说法,那是不是说明它里面所包含的Cmdlets就是各种操作的相关命令。让我使用Get-Command -Module PowerShellGet命令行来查看下里面包含的命令。

友好提示:文章里的一些代码片段单行内容比较长,如果排版有些拥挤,建议手机切换成横屏模式游览会舒服很多。

PS C:\Users\Administrator> Get-Command -Module PowerShellGetCommandType     Name                            Version    Source-----------     ----                            -------    ------Function        Find-Command                    1.0.0.1    PowerShellGetFunction        Find-DscResource                1.0.0.1    PowerShellGetFunction        Find-Module                     1.0.0.1    PowerShellGetFunction        Find-RoleCapability             1.0.0.1    PowerShellGetFunction        Find-Script                     1.0.0.1    PowerShellGetFunction        Get-InstalledModule             1.0.0.1    PowerShellGetFunction        Get-InstalledScript             1.0.0.1    PowerShellGetFunction        Get-PSRepository                1.0.0.1    PowerShellGetFunction        Install-Module                  1.0.0.1    PowerShellGetFunction        Install-Script                  1.0.0.1    PowerShellGetFunction        New-ScriptFileInfo              1.0.0.1    PowerShellGetFunction        Publish-Module                  1.0.0.1    PowerShellGetFunction        Publish-Script                  1.0.0.1    PowerShellGetFunction        Register-PSRepository           1.0.0.1    PowerShellGetFunction        Save-Module                     1.0.0.1    PowerShellGetFunction        Save-Script                     1.0.0.1    PowerShellGetFunction        Set-PSRepository                1.0.0.1    PowerShellGetFunction        Test-ScriptFileInfo             1.0.0.1    PowerShellGetFunction        Uninstall-Module                1.0.0.1    PowerShellGetFunction        Uninstall-Script                1.0.0.1    PowerShellGetFunction        Unregister-PSRepository         1.0.0.1    PowerShellGetFunction        Update-Module                   1.0.0.1    PowerShellGetFunction        Update-ModuleManifest           1.0.0.1    PowerShellGetFunction        Update-Script                   1.0.0.1    PowerShellGetFunction        Update-ScriptFileInfo           1.0.0.1    PowerShellGet

我们获得了各类用于管理的相关命令,为了方便查看我们用下面的命令做些编排以方便梳理。

PS C:\Users\Administrator> Get-Command -Module PowerShellGet | Group-Object -Property VerbCount Name              Group----- ----              -----    5 Find              {Find-Command, Find-DscResource, Find-Module, Find-RoleCapability...}    3 Get               {Get-InstalledModule, Get-InstalledScript, Get-PSRepository}    2 Install           {Install-Module, Install-Script}    1 New               {New-ScriptFileInfo}    2 Publish           {Publish-Module, Publish-Script}    1 Register          {Register-PSRepository}    2 Save              {Save-Module, Save-Script}    1 Set               {Set-PSRepository}    1 Test              {Test-ScriptFileInfo}    2 Uninstall         {Uninstall-Module, Uninstall-Script}    1 Unregister        {Unregister-PSRepository}    4 Update            {Update-Module, Update-ModuleManifest, Update-Script, Update-ScriptFileInfo}

大家可以看到,有Find(查找), Get(获取), Install(安装), New(新建), Publish(发布), Register(注册), Save(保存), Set(设置), Test(检查), Uninstall(卸载), Unregister(注销), Update(更新)一共包含了12大类的功能管理,几乎你可以用PowerShellGet来管理你整个PowerShell了。因为里面涵盖的内容非常多,所以我打算抽其中部分方面来介绍下。

好吧,我突然想到想用Find-Module查看PowerShellGet的所有包含版本号,因为PowerShellGet的数据源是PSGallery在线站点,所以它会连接PSGallery在线站点去获取我们的数据包,下面列出了目前站点上收录的可用版本号。

PS C:\Users\Administrator> Find-Module -Name PowerShellGet -AllVersionsVersion    Name                  Repository           Description-------    ----                  ----------           -----------1.1.2.0    PowerShellGet         PSGallery            PowerShell module with commands for discovering,..1.1.1.0    PowerShellGet         PSGallery            PowerShell module with commands for discovering,..1.1.0.0    PowerShellGet         PSGallery            PowerShell module with commands for discovering,..

我好像记得我当前电脑里的PowerShellGet不是最新的呢,让我用Get-Module查看下PowerShellGet版本号确认下吧。

PS C:\Users\Administrator> Get-Module -Name PowerShellGetModuleType Version    Name            ExportedCommands---------- -------    ----            ----------------Script     1.0.0.1    PowerShellGet   {Find-Command, Find-DscResource, Find-Module, Find-RoleCap..

果然不是最新的,我电脑里的版本还是1.0.0.1,而之前我用Find-Module列出的在线版本号最新的已经到1.1.2.0了,赶紧升级下看看是不是又多了什么新功能。怎么升级呢?我想到了Update-Module的命令

PS C:\Users\Administrator> Update-Module -Name PowerShellGet -RequiredVersion 1.1.2.0Update-Module : Module 'PowerShellGet' was not installed by using Install-Module, so it cannot be updated.At line:1 char:1+ Update-Module -Name PowerShellGet -RequiredVersion 1.1.2.0+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (PowerShellGet:String) [Write-Error], WriteErrorException    + FullyQualifiedErrorId : ModuleNotInstalledUsingInstallModuleCmdlet,Update-Module

什么情况?居然报错了,提示Module ‘PowerShellGet’ was not installed by using Install-Module, so it cannot be updated. 这段错误提示告诉我,我要升级的PowerShellGet模块并没有通过Install-Module来安装(由于我们目前自带的PowerShellGet是安装WMF 5.0时自带的),所以不能被升级,也就是只有通过Install-Module方式来安装的模块才可以配合Update-Module更新。为了验证下,我用Get-InstalledModule命令查看下,果真下面列出的安装模块列表里显示我之前没用Install-Module方式安装过PowerShellGet模块。

PS C:\Users\Administrator> Get-InstalledModuleVersion    Name                Repository      Description-------    ----                ----------      -----------0.1.0.1    AppxGet             PSGallery       Powershell Package Management (OneGet) Provider ...1.5.1      AzureRM             PSGallery       Azure Resource Manager Module0.5        GitHubProvider      PSGallery       GitHub-as-a-Package - PackageManagement PowerShe...1.1.3.0    PackageManagement   PSGallery       PackageManagement (a.k.a. OneGet) is a new way t...

既然知道原因了那就简单了,马上用Install-Module安装吧。

PS C:\Users\Administrator> Install-Module -Name PowerShellGet -RequiredVersion 1.1.2.0Untrusted repositoryYou are installing the modules from an untrusted repository. If you trust this repository, change itsInstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from'PSGallery'?[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): N

咦,这又什么情况?提示You are installing the modules from an untrusted repository. 我正要安装的模块包来自于一个未被信任的仓库来源。虽然我们输入 “Y” 参数可以继续执行,因为我们知道这是安全的。但是我还是想让它所以,这里我还是输入 “N” 先不继续安装。那怎么解决信任问题呢?按照刚才的错误提示用Set-PSRepository来操作,将PSGallery设置为可信任源。

PS C:\Users\Administrator>> Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

再用下面的命令验证查看下确实已经为信任级别了。

PS C:\Users\Administrator>> Get-PSRepositoryName             InstallationPolicy   SourceLocation----             ------------------   --------------PSGallery        Trusted              https://www.powershellgallery.com/api/v2/

现在,终于可以安装PowerShellGet模块了,不过为了后续给大家介绍如何更新模块,我在这里就刻意安装一个非最新版本1.1.0.0的PowerShellGet,等等再来升级它。

PS C:\Users\Administrator> Install-Module -Name PowerShellGet -RequiredVersion 1.1.0.0

然后再用Update-Module来升级到指定的最新版本。

PS C:\Users\Administrator> Update-Module -Name PowerShellGet -RequiredVersion 1.1.2.0

就是这么简单,我用Get-Module配合ListAvaliable参数居然发现有3个版本,这是因为在PowerShell 5.0中考虑到了兼容性已经支持了相同模块但是多版本的功能。

PS C:\Users\Administrator> Get-Module -Name PowerShellGet -ListAvailable    Directory: C:\Program Files\WindowsPowerShell\ModulesModuleType Version    Name              ExportedCommands---------- -------    ----              ----------------Script     1.1.2.0    PowerShellGet     {Install-Module, Find-Module, Save-Module, Update-Module...}Script     1.1.0.0    PowerShellGet     {Install-Module, Find-Module, Save-Module, Update-Module...}Script     1.0.0.1    PowerShellGet     {Install-Module, Find-Module, Save-Module, Update-Module...}

总结

大致的介绍说完了,虽然只是一个安装升级模块的过程出现了点小插曲,但在这过程中我们却用到了许多相关命令。让我们总结下,我们用Find-Module来查找在线仓库里可用的模块版本。然后用Get-Module获得当前的版本信息号以此来比较是否需要更新。接着我们用Update-Module来更新模块,但知道了一个重要的点一旦能用Update-Module更新的模块包前提必须都是要用Install-Module方式来安装的。在安装的过程中我们用Set-PSRepository设置可信任的安装源,最后终于可以顺利安装了。

阅读全文
1 0