1.  概要

- PowerShell 是cmdlet(command-let)的指令集合,类似unix的bash。

- IDE: Windows PowerShell ISE,不区分大小写,可以用命令行get-help获取帮助

- 以.NET为基础,向前兼容WSH,可以使用现有的COM技术,也可以直接访问.NET类库(如:[io.path]::GetDirectoryName($MyInvocation.InvocationName)),可以与windows系的相关软件互动(如:Excel, Exchange Server等)。

- wiki上的范例

 #1 - 获取所有命令[4]:
PS> Get-Command
#2 - 查看Get-Command命令的用法[5]:
PS> Get-Help Get-Command
#3 - 停止所有目前執行中的以"p"字元開頭命名的程式:
PS> get-process p* | stop-process
#4 - 停止所有目前執行中的所有使用大於1000MB記憶體的程式:
PS> get-process | where { $_.WS -gt 1000MB } | stop-process
#5 - 計算一個目錄下檔案內的位元組大小:
PS> get-childitem | measure-object -property length -sum
#6 - 等待一個叫做"notepad"的程式執行結束:
PS> $processToWatch = get-process notepad
PS> $processToWatch.WaitForExit()
#7 - 將"hello, world!"字串轉為英文大寫字元,成為"HELLO, WORLD!":
PS> "hello, world!".ToUpper()
#8 - 在字串"string"的第1個字元後插入字串"ABC",成為"sABCtring":
PS> "string".Insert(1, "ABC")
#9 - 訂閱一個指定的RSS Feed並顯示它最近8個主題:
PS> $rssUrl = "http://blogs.msdn.com/powershell/rss.aspx"
PS> $blog = [xml](new-object System.Net.WebClient).DownloadString($rssUrl)
PS> $blog.rss.channel.item | select title -first 8
#10 - 把"$UserProfile"設定成數值"UserProfile"的環境變數:
PS> $UserProfile = $env:UserProfile

2.  一段PS脚本的解析

 $status = @{1 = "L"; 2 = "R"; 4 = "W"; 8 = "M"}
$value = 14
$statutext = ($status.Keys | where { $_ -band $value } | foreach { $status.Get_Item($_) } | sort) -join ""
$expecting = ($expecting.GetEnumerator() | sort) -join ""

- $_ 代表循环体内的值,$status中的key哪些包含在$value中,以上代码段中当$value=14时,$status.Keys | where { $_ -band $value }的结果为 {2,4,8}

- 操作符-band 表示bit and; -join 作用是把数组连接成字符串,可以通过get-help about_join获得具体使用方法

- 操作符 | 为pipe line,如: command1 | command2 | command3,执行顺序为 command1 -〉 command2 -〉 command3

#参考文档 1

3.  about_Splatting

TOPIC
about_Splatting SHORT DESCRIPTION
Describes how to use splatting to pass parameters to commands
in Windows PowerShell. LONG DESCRIPTION
[This topic was contributed by Rohn Edwards of Gulfport,
Mississippi, a system administrator and the winner
of the Advanced Division of the 2012 Scripting Games.
Revised for Windows PowerShell 3.0.] Splatting is a method of passing a collection of parameter
values to a command as unit. Windows PowerShell associates
each value in the collection with a command parameter.
Splatted parameter values are stored in named splatting
variables, which look like standard variables, but begin with
an At symbol (@) instead of a dollar sign ($). The At symbol
tells Windows PowerShell that you are passing a collection of
values, instead of a single value. Splatting makes your commands shorter and easier to read. You
can re-use the splatting values in different command calls and
use splatting to pass parameter values from the $PSBoundParameters
automatic variable to other scripts and functions. Beginning in Windows PowerShell 3.0, you can also use splatting
to represent all parameters of a command. SYNTAX <CommandName> <optional parameters> @<HashTable> <optional parameters> <CommandName> <optional parameters> @<Array> <optional parameters> To provide parameter values for positional parameters, in which
parameter names are not required, use the array syntax. To provide
parameter name and value pairs, use the hash table syntax. The
splatted value can appear anywhere in the parameter list. When splatting, you do not need to use a hash table or an array to
pass all parameters. You may pass some parameters by using splatting
and pass others by position or by parameter name. Also, you can splat
multiple objects in a single command just so you pass no more than one
value for each parameter. SPLATTING WITH HASH TABLES
Use a hash table to splat parameter name and value pairs. You can use
this format for all parameter types, including positional and named
parameters and switch parameters. The following examples compare two Copy-Item commands that copy the
Test.txt file to the Test2.txt file in the same directory. The first example uses the traditional format in which parameter names
are included. Copy-Item -Path "test.txt" -Destination "test2.txt" -WhatIf The second example uses hash table splatting. The first command creates
a hash table of parameter-name and parameter-value pairs and stores it
in the $HashArguments variable. The second command uses the $HashArguments
variable in a command with splatting. The At symbol (@HashArguments)
replaces the dollar sign ($HashArguments) in the command. To provide a value for the WhatIf switch parameter, use $True or $False. PS C:\>$HashArguments = @{ Path = "test.txt"; Destination = "test2.txt"; WhatIf = $true }
PS C:\>Copy-Item @HashArguments Note: In the first command, the At symbol (@) indicates a hash table, not
a splatted value. The syntax for hash tables in Windows PowerShell is:
@{ <name>=<value>; <name>=<value>; …} SPLATTING WITH ARRAYS
Use an array to splat values for positional parameters, which do not require
parameter names. The values must be in position-number order in the array. The following examples compare two Copy-Item commands that copy the Test.txt
file to the Test2.txt file in the same directory. The first example uses the traditional format in which parameter names are
omitted. The parameter values appear in position order in the command. Copy-Item "test.txt" "test2.txt" -WhatIf The second example uses array splatting. The first command creates an array
of the parameter values and stores it in the $ArrayArguments variable. The
values are in position order in the array. The second command uses the
$ArrayArguments variable in a command in splatting. The At symbol
(@ArrayArguments) replaces the dollar sign ($ArrayArguments) in the command. PS C:\>$ArrayArguments = "test.txt", "test2.txt"
PS C:\>Copy-Item @ArrayArguments -WhatIf EXAMPLES This example shows how to re-use splatted values in different commands.
The commands in this example use the Write-Host cmdlet to write messages
to the host program console. It uses splatting to specify the foreground
and background colors. To change the colors of all commands, just change the value of the $Colors
variable. The first command creates a hash table of parameter names and values and
stores the hash table in the $Colors variable. $Colors = @{ForegroundColor = "black"
BackgroundColor = "white"} The second and third commands use the $Colors variable for splatting in a
Write-Host command. To use the $Colors variable, replace the dollar sign
($Colors) with an At symbol (@Colors). # Write a message with the colors in $Colors
Write-Host "This is a test." @Colors # Write second message with same colors.
# The position of splatted hash table does not matter.
Write-Host @Colors "This is another test." This example shows how to forward their parameters to other commands by
using splatting and the $PSBoundParameters automatic variable. The $PSBoundParameters automatic variable is a dictionary
(System.Collections.Generic.Dictionary) that contains all of the
parameter names and values that are used when a script or function
is run. In the following example, we use the $PSBoundParameters variable to
forward the parameters values passed to a script or function from Test2
function to the Test1 function. Both calls to the Test1 function from
Test2 use splatting. function Test1
{
param($a, $b, $c) $a
$b
$c
} function Test2
{
param($a, $b, $c) # Call the Test1 function with $a, $b, and $c.
Test1 @PsBoundParameters # Call the Test1 function with $b and $c, but not with $a
$LimitedParameters = $PSBoundParameters
$LimitedParameters.Remove("a") | Out-Null
Test1 @LimitedParameters
} PS C:\> Test2 -a 1 -b 2 -c 3
1
2
3
2
3 SPLATTING COMMAND PARAMETERS
You can use splatting to represent the parameters of
a command. This technique is useful when you are creating
a proxy function, that is, a function that calls another
command. This feature is introduced in Windows PowerShell 3.0. To splat the parameters of a command, use @Args to represent
the command parameters. This technique is easier than
enumerating command parameters and it works without revision
even if the parameters of the called command change. The feature uses the $Args automatic variable, which contains
all unassigned parameter values. For example, the following function calls the Get-Process
cmdlet. In this function, @Args represents all of the parameters
of the Get-Process cmdlet. function Get-MyProcess { Get-Process @Args } When you use the Get-MyProcess function, all unassigned
parameters and parameter values are passed to @Args, as
shown in the following commands. PS C:\> Get-MyProcess -Name PowerShell Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
463 46 225484 237196 719 15.86 3228 powershell PS C:\> Get-MyProcess -Name PowerShell_Ise -FileVersionInfo ProductVersion FileVersion FileName
-------------- ----------- --------
6.2.9200.16384 6.2.9200.1638... C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe You can use @Args in a function that has explicitly
declared parameters. You can use it more than once in
a function, but all parameters that you enter are passed
to all instances of @Args, as shown in the following
example. function Get-MyCommand
{
Param ([switch]$P, [switch]$C)
if ($P) { Get-Process @Args }
if ($C) { Get-Command @Args }
} PS C:\> Get-MyCommand -P -C -Name PowerShell Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
408 28 75568 83176 620 1.33 1692 powershell Path : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Extension : .exe
Definition : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Visibility : Public
OutputType : {System.String}
Name : powershell.exe
CommandType : Application
ModuleName :
Module :
RemotingCapability : PowerShell
Parameters :
ParameterSets :
HelpUri :
FileVersionInfo : File: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe SEE ALSO
about_Arrays
about_Automatic_Variables
about_Hash_Tables
about_Parameters
$props = @{
From = $FROM_EMAIL_ADDRESS
To= $to
Subject = $subject
Body = $message
SmtpServer = $EMAIL_SERVER
} If($cc){$props.Add("CC",$cc)} Send-MailMessage @props

Hashtable操作

4. launch VBA script from powershell

==============Powershell script=======================
$xl = new-object -comobject Excel.Application
$source_wb = $xl.workbooks.open("myExcel.xls")
$xl.visible=$false
$param = "myMethod myParams1 myParams2"
$type=$xl.GetType() $ret=$type.InvokeMember("Run",[Reflection.BindingFlags]::InvokeMethod,$null,$xl,$param)
$xl.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
Remove-Variable xl ==============VBA script=======================
Sub myMethod(ByVal p1 As String, ByVal p2 As String)
'TODO sth.
End Sub

5. 获取当前脚本运行的路径

把以下脚本放到function里面,否则Path为null
Split-Path $script:MyInvocation.MyCommand.Path
#to make this snippet work, it should be named as a powershell file
# e.g. save it as D:\Scripts\get-location1.ps1 function Get-ScriptDirectory
{
#Split-Path $script:MyInvocation.MyCommand.Path
return $script:MyInvocation.MyCommand.Path
} "immediate func call"
$xxx = Get-ScriptDirectory
echo $xxx

6. here-string

 $x = @"
"
Curiouser
and
curiouser
!
"
"@

7.

 
 

PowerShell Notes的更多相关文章

  1. PowerShell vs. PsExec for Remote Command Execution

    Posted by Jianpeng Mo / January 20, 2014 Monitoring and maintaining large-scale, complex, highly dis ...

  2. Git for Windows v2.11.0 Release Notes

    homepage faq contribute bugs questions Git for Windows v2.11.0 Release Notes Latest update: December ...

  3. 使用powershell提权的一些技巧

    原文:http://fuzzysecurity.com/tutorials/16.html 翻译:http://www.myexception.cn/windows/1752546.html http ...

  4. Building Forms with PowerShell – Part 1 (The Form)

    For those of you familiar with Scripting languages you are probably used to using alternate applicat ...

  5. win7 powershell配色方案

    首先我是参考微软的word的, look~ Windows PowerShell 配置文件 要配置powershell很简单, 就几步 1.显示 Windows PowerShell 配置文件的路径 ...

  6. SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script

    In this post I will introduce a way how to run a script for backing up SharePoint data which could b ...

  7. 如何在Win8.1和Win2012上运用PowerShell快速生成、安装、导出自签名证书 (Self-Signed Certificate)

    自签名证书用途很广,测试,开发,本地或者云端网站(比如Microsoft Azure Web Site)都会使用到.本文会介绍一种在Win8.1和Win2012 R2上使用PowerShell快速生成 ...

  8. [Powershell]发布基于.NET Framework的WebAPI和Job控制台程序项目

    获取要发布的定时计划任务. 禁用和停止定时计划任务. 批量强制结束Job进程. 打印定时计划任务状态. 备份项目文件夹. 发布项目文件夹. 删除部署包. 启用定时计划任务. <# .NOTES ...

  9. 不安装任何软件或脚本使用powershell快速计算文件的MD5/SHA1/SHA256等校验值

    有的时候在检查升级过程中需要对xml文件进行签名,而xml文件中一般都需要包含安装包的SHA256值,这里分享一个使用PowerShell快速计算SHA256等其他值的方法. 一.在需要计算文件SHA ...

随机推荐

  1. PHP语法(三):控制结构(For循环/If/Switch/While)

    相关链接: PHP语法(一):基础和变量 PHP语法(二):数据类型.运算符和函数 PHP语法(三):控制结构(For循环/If/Switch/While) 本文我来总结几个PHP常用的控制结构,先来 ...

  2. 理解Docker(8):Docker 存储之卷(Volume)

    (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 (4)Docker 容器的隔离性 - 使用 ...

  3. Bluemix中国版体验(一)

    很高兴终于拿到了中国版Bluemix的账号!中国版的Bluemix是由世纪互联运营的,这也是世纪互联继Microsoft Azure,Office 365之后运营的又一个国际一线大品牌的云服务. 中国 ...

  4. 【repost】JavaScript 事件模型 事件处理机制

    什么是事件? 事件(Event)是JavaScript应用跳动的心脏 ,也是把所有东西粘在一起的胶水.当我们与浏览器中 Web 页面进行某些类型的交互时,事件就发生了.事件可能是用户在某些内容上的点击 ...

  5. U3D 打包时找不到tag的问题

    在公司改完一个功能,把工程拷回家打开后,编辑器模式下运行正常,打包PC平台和安卓平台时都报错,找不到chatContent这个tag,察看了下下拉列表中明明有这个tag,并且勾选上了,但是点击add ...

  6. [LeetCode] Nested List Weight Sum II 嵌套链表权重和之二

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  7. HTTP服务器(1)

    单文件服务器 导语 在研究HTTP服务器时,我们可以从一个单文件服务器开始.无论接受到什么请求,这个服务器始终发送同一个文件.下面是示例代码,绑定的端口,发送的文件名以及文件的编码从命令行读取.如果省 ...

  8. Java序列化与反序列化

    Java序列化与反序列化是什么?为什么需要序列化与反序列化?如何实现Java序列化与反序列化?本文围绕这些问题进行了探讨. 1.Java序列化与反序列化 Java序列化是指把Java对象转换为字节序列 ...

  9. HTML <meta> 标签,搜索引擎

    关于Mate标签的详尽解释,请查看w3school 网址为:http://www.w3school.com.cn/tags/tag_meta.asp meta标签作用 META标签是HTML标记HEA ...

  10. vue.js 第三课

    1.构造器 constructor 2.属性和方法 properties methods 3.实例生命周期 instance_lifecycle   1.vue.js都是通过 var vm=new V ...