[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转载教程] 【探索PowerShell 】【三】PowerShell下使用Aliases

【探索PowerShell 】【一】初识http://www.bathome.net/thread-26144-1-1.html
【探索PowerShell 】【二】基本操作http://www.bathome.net/thread-26148-1-1.html
【探索PowerShell 】【三】PowerShell下使用Aliaseshttp://www.bathome.net/thread-26149-1-1.html
【探索PowerShell 】【四】PowerShell的对象、格式与参数http://www.bathome.net/thread-26159-1-1.html
【探索PowerShell 】【五】PowerShell基础知识http://www.bathome.net/thread-26162-1-1.html
【探索PowerShell 】【六】脚本基础简要http://www.bathome.net/thread-26163-1-1.html
【探索PowerShell 】【七】变量http://www.bathome.net/thread-26165-1-1.html
【探索PowerShell 】【八】数组、哈希表(附:复制粘贴技巧)http://www.bathome.net/thread-26166-1-1.html
【探索PowerShell 】【九】条件控制、逻辑http://www.bathome.net/thread-26167-1-1.html
【探索PowerShell 】【十】循环语句http://www.bathome.net/thread-26168-1-1.html
【探索PowerShell 】【十一】函数http://www.bathome.net/thread-26174-1-1.html
【探索PowerShell 】【十二】筛选器 - Filtershttp://www.bathome.net/thread-26187-1-1.html
【探索PowerShell 】【十三】WMI对象http://www.bathome.net/thread-26188-1-1.html
【探索PowerShell 】【十四】使用WMI对象的方法http://www.bathome.net/thread-26189-1-1.html
【探索PowerShell 】【十五】引号与转义字符http://www.bathome.net/thread-26190-1-1.html

不知道各位有没有接触过linux,我才疏学浅,对alias的认识是从linux上来的。Linux常用的Bash命令也是有上百个,虽然一般情况下命令都非常简短,不过很多时候需要大量的却是大量的参数,这样,对很多初学者来说,背命令变成为最头疼的事情,也使得很多Linux初学者望而生畏。因此,在linux下,很常用的就是alias,也就是别名。当然,PowerShell很好的借鉴了这一点。

这一节就来介绍PowerShell下的Aliases。

在PowerShell中,为了习惯于使用,可以使用别名代替命令。例如,Windows老手可以利用像dir、move、type、cls等,当然,为了吸纳更多使用者,PowerShell还提供了对Linux的别名设置,如:ls、pwd、mv、man、cat等。

为PowerShell设置别名的目的,是为了向新用户提供一个能够快速交互的shell。这里的alias,指的是cmdlet的替代名称。例如,"dir”替代的是"Get-ChildItem”。

在这一节,将介绍两种类型的alias:

内置的别名

适用于Windows,Unix和预定义的PowerShell cmdlet的替代名称。

使用以下命令来浏览PowerShell的alias列表:
  1. get-alias
复制代码
PS D:\> get-alias

CommandType     Name                                                Definition
-----------     ----                                                ----------
Alias           %                                                   ForEach-Object
Alias           ?                                                   Where-Object
Alias           ac                                                  Add-Content
Alias           asnp                                                Add-PSSnapIn
Alias           cat                                                 Get-Content
...


细心的童鞋可以发现,Get-ChileItem这个cmdlet拥有多个alias,如:dir、ls、gci。我们可以尝试这些命令,结果都是一样的,如下图:

PS D:\BatHome> ls


    Directory: D:\BatHome


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         8/30/2013   3:03 PM          0 bathome.txt
-a---         8/30/2013   3:03 PM          0 bbs.txt
-a---         8/30/2013   3:04 PM          0 net.txt


PS D:\BatHome> dir


    Directory: D:\BatHome


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         8/30/2013   3:03 PM          0 bathome.txt
-a---         8/30/2013   3:03 PM          0 bbs.txt
-a---         8/30/2013   3:04 PM          0 net.txt


PS D:\BatHome> gci


    Directory: D:\BatHome


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         8/30/2013   3:03 PM          0 bathome.txt
-a---         8/30/2013   3:03 PM          0 bbs.txt
-a---         8/30/2013   3:04 PM          0 net.txt


PS D:\BatHome> get-childitem


    Directory: D:\BatHome


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         8/30/2013   3:03 PM          0 bathome.txt
-a---         8/30/2013   3:03 PM          0 bbs.txt
-a---         8/30/2013   3:04 PM          0 net.txt


从本质上讲,我们只是运行了四次"get-chileitem"。内置别名的存在,使得我们在shell中工作时更加顺手而高效。不过说实在的,作为用户,我们没有必要多多的关注一个cmdlet都有哪些alias,我们只要用好自己习惯的就行了。
  1. 用户自定义的PowerShell的Alias
复制代码
设置alias的命令

例:为名为'get-service”的cmdlet定义一个alias
  1. Set-Alias gs Get-Service
复制代码
相当简单的语法,没有什么可多说的。

使用以上命令为你的cmdlet定义alias确实很容易,不过存在一个问题,就是这个alias只对当前处于会话期的PowerShell有效,也就是说,当你关闭并重新打开PowerShell时,输入这条alias,PowerShell会很无情的告诉你 “The term ‘gs’ is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.”

那么,当我们创建了一个Alias,并且不想失去它,我们怎么办?

  • 有两种选择:

        导入和导出PowerShell的Aliases;

        使用PowerShell配置文件来自定义Alias。

方法一:导入和导出PowerShell的Aliases

导出Aliases的一个好处就是,你可以将它带到任何一个你想使用的地方。其命令如下<导出文件名自定义>:
  1. Export-Alias -Path a.txt
复制代码
PS D:\BatHome> Export-Alias -Path a.txt
PS D:\BatHome> ls


    Directory: D:\BatHome


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         8/30/2013   3:08 PM      12032 a.txt
-a---         8/30/2013   3:03 PM          0 bathome.txt
-a---         8/30/2013   3:03 PM          0 bbs.txt
-a---         8/30/2013   3:04 PM          0 net.txt


我们可以用notepad.exe看看这个里面都是些什吗东东~~~

# Alias File
# Exported by : HAT
# Date/Time : Friday, August 30, 2013 15:08:46
# Machine : BATHOME
"ac","Add-Content","","ReadOnly, AllScope"
"asnp","Add-PSSnapIn","","ReadOnly, AllScope"
"clc","Clear-Content","","ReadOnly, AllScope"
...


好了,有人迫不及待的想试试导入了吧~~~
  1. Import-Alias -Path a.txt
复制代码
额~杯具了!!!

PS D:\BatHome> Import-Alias -Path a.txt
Import-Alias : Alias not allowed because an alias with the name 'ac' already exists.
At line:1 char:13
+ Import-Alias <<<<  -Path a.txt
    + CategoryInfo          : ResourceExists: (ac:String) [Import-Alias], SessionStateException
    + FullyQualifiedErrorId : AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand

Import-Alias : Alias not allowed because an alias with the name 'asnp' already exists.
At line:1 char:13
+ Import-Alias <<<<  -Path a.txt
    + CategoryInfo          : ResourceExists: (asnp:String) [Import-Alias], SessionStateException
    + FullyQualifiedErrorId : AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand
...


看起来很惨的样子……

其实如果一个cmdlet存在alias,那么再次导入就会产生这样的杯具。

所以,我们的导出文件,只需要很简单的定义出自己常用的alias即可(那些系统自带的都统统删除就好啦),如:

# Alias File
# Exported by : HAT
# Date/Time : Friday, August 30, 2013 15:08:46
# Machine : BATHOME
"hat","Stop-Process","","None"


好了,就是这样,嘿嘿嘿,是不是很邪恶~~~看看效果吧:

PS D:\BatHome> Import-Alias -Path a.txt
PS D:\BatHome> hat

cmdlet Stop-Process at command pipeline position 1
Supply values for the following parameters:
Id[0]:


当然,强大的PowerShell不会弱智到让用户每次启动shell时都手动载入aliases一次的。在PowerShell的配置文件中,我们可以进行相关设置。

方法二:使用PowerShell的配置文件

配置文件的位置存储在$Profile变量中,其默认值为:

'My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1″

使用如下命令查看$Profile值:
  1. $Profile
复制代码
PS D:\> $Profile
D:\users\HAT\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1


如前所述,PowerShell配置文件是一个扩展名为“.ps1”的文件。所有PowerShell脚本都使用这个扩展名。在此,我将使用记事本对其进行编辑。不过在此之前,还需要检查的PowerShell的执行策略设置。

PowerShell在第一次安装时默认的执行策略是"Restricted",也就是“受限制的”,这意味着PowerShell将不能运行任何脚本和配置文件。

• PowerShell的执行策略分级:

        Restricted - 不能运行任何脚本和配置文件
        AllSigned - 所有脚本和配置文件必须拥有受信任的发布者的签名
        RemoteSigned - 所有脚本和配置文件从可以是互联网上下载,但必须拥有受信任的发布者的签名
        Unrestricted - 所有脚本和配置文件都将运行,从互联网上下载的脚本在运行前会有提示。

使用如下cmdlet查看当前所处的策略级别:
  1. Get-ExecutionPolicy
复制代码
修改策略级别为Unrestricted:
  1. Set-ExecutionPolicy Unrestricted
复制代码
好的,我们现在来对$Profile进行配置。

• 第1步 验证$Profile是否存在
  1. test-path $Profile
复制代码
false ===> 配置文件不存在(继续步骤2)。
true ===> 不多说了,直接跳过步骤2和3吧。除非你想创建一个新的配置文件,不过这将删除当前的配置文件。

• 第2步 创建一个新的配置文件
  1. New-Item -Path $Profile -ItemType file -Force
复制代码
PS D:\> New-Item -Path $Profile -ItemType file -Force


    Directory: D:\users\HAT\My Documents\WindowsPowerShell


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         8/30/2013   3:20 PM          0 Microsoft.PowerShell_profile.ps1


第3步 再次验证是否成功创建新的配置文件

重复步骤1吧。结果应该为"true”。

PS D:\> test-path $Profile
True


好了,在记事本中打开配置文件:
  1. notepad $Profile
复制代码
OK!我们现在拥有了一个空白的配置文件,其实可以称之为一个空白脚本。

其文件名为:“Microsoft.PowerShell_profile.ps1″

现在开始配置吧:
set-alias marui stop-process


好了,现在可以关闭notepad和Powershell了。重新打开PowerShell试试看。

PS D:\> marui

cmdlet Stop-Process at command pipeline position 1
Supply values for the following parameters:
Id[0]:


完美!

另外需要说明几点,更改PowerShell策略级别时,需要使用管理员权限的。

好了,这一节就到这里。说实话,那几个策略级别的单词真不好记,不过幸好不常用~~~

本文出自 “马睿的技术博客” 博客,请务必保留此出处http://marui.blog.51cto.com/1034148/290067

标题

本帖最后由 wankoilz 于 2013-8-30 16:02 编辑

说干就干,楼主辛苦了!
完了把所有系列教程集中到一贴,然后置顶!

TOP

返回列表