《Learn Windows PowerShell in a Month of Lunches Third Edition》读书笔记——CHAPTER 7 Adding commands

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_41104353/article/details/82192818

7.4 Extensions: finding and adding modules

使用 get-content env:psmodulepath 命令查看环境变量 PSModulePath 的值。

7.7 Playing with a new module

假设我们想要 清除DNS名称解析缓存 ,但是我们不知道PowerShell能不能做这个,我们从查询帮助系统来获取线索:

PS C:\> help *dns*

输入该命令后,应该看到有Module DnsClient ,如果没有的话,是系统版本太老了。

7.8 Profile scripts: preloading extensions when the shell starts

有三种方式来达到在打开PowerShell的时候就装载我们的snap-ins和modules。

The first involves creating a console file

这种方法只记住PSSnapins要加载的地方,不能记住modules要加载的地方。
打开PowerShell,手动加载完要加载的snap-ins,然后运行 Export-Console c:\myshell.psc,然后输入 %windir%\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -psconsolefile c:\myshell.psc 来达到创建快捷路径的目的。(博主个人认为,powershell.exe的位置可能每个人不太一样)

use a profile script to load a mix of snap-ins and modules

值得注意的是,PowerShell会自动装载在 PSModulePath 位置的modules,所以这里我们考虑的是不在此位置的modules。下面是Windows操作方式。

  1. 在shell中使用 $profile 命令查看 WindowsPowerShell 文件夹路径
  2. 在该 WindowsPowerShell 文件夹下创建一个叫 profile.ps1 的文件。(后缀那个是数字1)
  3. profile.ps1 文件中写入你的 Add-PSSnapin 和 Import-Module命令 ,每行一个命令。
  4. 在PowerShell中我们要允许脚本的执行,因为默认是不允许的。
  5. 管理员身份 打开的shell执行命令 Set-ExecutionPolicy RemoteSigned ,没有出错后重启shell就ok了。

博主注:我看了好几遍这一小节,都没有看到提到的第三种方法在哪…

7.9 Getting modules from the internet

有了module PowerShellGet 我们可以很方便的下载,安装和升级其他module。
PowerShellGet是v5版本以上的PowerShell自带的。
使用该模块很简单:

使用 Register-PSRepository 添加我们自己想要下载的模块的仓库的网址。默认是http://www.powershellgallery.com/
使用 Find-Module 在仓库里查找模块
使用 Install-Module 下载和安装模块
使用 Update-Module 升级下载的模块

7.10 Common points of confusion

我们可以使用 HelpGet-Command 加上通配符来缩小我们要查找命令的范围。
对于具体的命令,我们可以通过 -example-full 参数来学习怎么使用。

猜你喜欢

转载自blog.csdn.net/sinat_41104353/article/details/82192818