Power Shell 基本概念

命令的语法:
command(命令的名称) -parameter1(Switch Parameter,不需要Argument) -parameter2 arg1(含有Argument的Parameter) arg2(Positional Argument)
命令的4种分类:
1)cmdlet(特指PowerShell的Environment),继承基类Cmdlet的.NET实现;
编译成DLL,随着PowerShell的进程启动而运行;
2)function(一段PowerShell的脚本代码,当解释器运行时存在内存,结束时自动释放)
3)script(放在.ps文件里面的一段PowerShell代码)

4)Native(能够被OS调用的外部可执行文件)

对同一Command,有多个名称(Alias,可以通过get-command查看定义)


PowerShell如何解析:

1)引号:单引号(把输入专为字符串),双引号(Expand Variable,把输入转为字符串),`(转义字符,防止Expand Variable)

比如:$v="files"; cd "c:\program $v" ; pwd (运行结果是c:\program files);

而:cd 'c:\program $v';运行结果是c:\program $v(不会替换变量,运行出错)'

write-output "`$v is c:\program $v",运行结果是@v is c:\program files

常见的转义字符:`n是NewLine;


解析分类简单包括:expression mode 和command mode

2+2; expression mode;   write-output 2+2(运行结果为 2+2); command mode;

write-output (2+2)(首先是expression mode,然后是command mode,运行结果为4)


Pipeline Command:以|分隔的命令集;


格式化命令:format-list; format-table;

如:dir $pwd|fomat-list;


输出的命令:out-printer;out-default;out-file


sub-expression:

The expression in the $( ... ) sequence in the string is replaced by the result of evaluating
the expression. $(2+2) is replaced by 4 and so on.

比如:"2+2 is $(2+2)"; 运行结果是2+2 is 4;


复杂的Sub-Expression: 

1)数组分隔符号;

比如:1;2;3输出为:

1

2

3

"string is $(1;2;3)" 输出为string is 1 2 3.

2)for循环表达式

"Number 1 to 10 is $(for ($i=1;$i -le 10;$i++) {$i})" 输出为: Number  1 to 10 is 1 2 3 4 5 6 7 8 9 10


定义Dictionary;

1)以"@{" 开始,"}"结束;中间分割用;

比如:$user=@{Firstname="Mike"; LastName="John"}

$user输出为:

Name                           Value                                                                         
----                           -----                                                                         
LastName                          John                                                                           
Firstname                      Mike

$user.firstname or $user["firstname"];输出为Mike;

2)显示hashtable的Keys和Values: $user.keys;$user.values;$user[$user.keys];

3)支持排序:$user.keys | sort-object or $user.keys | sort -desc

4)添加/修改value:$user.Lastname="modify"

5)删除Value; $user.remove("Firstname")

6)Hashtable是引用类型:$user2=$user,$user2和$user指向 同一个对象


数组的定义:以逗号;作为分割符号

$list=1,2,3; $list.gettype() 输出为Object[]

用下标访问元素$list[0];

添加元素,用+=;$list += 5


PowerShell的类型(添加中括号[])和.NET Type映射:

 PowerShell type aliases and their corresponding .NET types
PowerShell Type Alias Corresponding .NET Type
[int] System.Int32
[int[]] System.Int32[]
[long] System.Int64
[long[]] System.Int64[]
[string] System.String
[string[]] System.String[]


查找类型信息的方法; get-member 命令

[string] | get-member -static

[string] :: Compare("123","234") ; 返回结果是-1;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值