1
2
3
<code>Get-ChildItem</code> <code>*.txt | </code><code>Foreach</code> <code>{</code>
<code> </code><code>Rename-Item</code> <code>-Path</code> <code>$_</code> <code>-NewName</code> <code>"$($_.basename).bak"</code>
<code>}</code>
上面使用的foreach对象cmdlet是不必要的,因为“重命名”项目接受管道输入路径和新名称参数。
下面的例子大家可以看到结果
4
5
<code>PS> </code><code>$obj</code> <code>= </code><code>New-Object</code> <code>PSObject –Property `</code>
<code> </code><code>@{Path=</code><code>'C:\Users\Cantgis\foo.txt'</code><code>;NewName=</code><code>'bar.txt'</code><code>}</code>
<code>PS> </code><code>$obj</code> <code>| </code><code>Rename-Item</code> <code>–WhatIf</code>
<code>What </code><code>if</code><code>: Performing operation </code><code>"Rename File"</code> <code>on Target</code>
<code>"Item: C:\Users\Cantgis\foo.txt Destination: C:\Users\Cantgis\bar.txt"</code><code>.</code>
你也许会想,虽然这可能是一个有趣的问题,但是它是如何对比早期的powershell版本的呢?
如果使用foreach对象会更好吗?
powershell有一个诀窍来帮助我们实现这个重命名操作。
诀窍是,PowerShell将接受任何参数是管道的约束,脚本调用的脚本区的一段。
你可以看到,如果一个参数是通过获取帮助,例如管道绑定:
6
7
8
9
10
11
12
<code>PS> </code><code>Get-Help</code> <code>Rename-Item</code>
<code>...</code>
<code>-LiteralPath</code>
<code> </code><code>...</code>
<code> </code><code>Accept pipeline input? true (ByPropertyName)</code>
<code>-Path</code>
<code> </code><code>Accept pipeline input? true (ByValue, ByPropertyName)</code>
<code>-NewName</code>
<code> </code><code>Position</code><code>? 2</code>
上面的信息告诉我们,LiteralPath:路径和新名称的参数接受管道输入。
Get-ChildItem:管道输出,重命名项:cmdlet LiteralPath参数。
我们可以使用脚本区结合这个诀窍来指定新名称。
<code>PS> </code><code>Get-ChildItem</code> <code>*.txt | </code><code>Rename-Item</code> <code>-NewName</code> <code>{</code><code>"$($_.BaseName).bak"</code><code>}</code>
在PowerShell 3.0的更新GridView的命令支持PassThru参数。
此外,OUT-GridView控件支持多选的项目传入以及取消操作。
例如,你可能想从列表中选择进程停止。
<code>PS> </code><code>Get-Process</code> <code>devenv |</code>
<code> </code><code>Select Name,Id,MainWindowTitle |</code>
<code> </code><code>Out-GridView</code> <code>-PassThru</code> <code>| </code><code>Stop-Process</code>
此命令显示出GridView的对话框,如下图所示。
我们可以看到基于MainWindowTitle属性的Visual Studio实例。
我可以选择一个或多个devenv的进程。
如果我按“确定”,然后我选择的进程将被停止。
如果我按出GridView的对话框上的取消按钮,停止管道,也没有进程被停止哦!
<a target="_blank" href="http://blog.51cto.com/attachment/201306/220440218.png"></a>
PSCX是一组通用的PowerShell命令。它提供的命令之一是显示树,基于PowerShell驱动器,如:
WSMan:\
Cert:\
HKLM:\
IIS:\ (if you have imported the WebAdministration module)
通常情况下,如果你想查找的驱动器,你可以使用Windows资源管理器。
不幸的是,除了那些基于文件系统上的,剩下的Windows资源管理器比不上可视性PowerShell驱动器。
同样不幸的是,像WSMAN驱动器和IIS:即所包含的功能是不是很发现,隐藏了很多功能。
这时这个社区提供的扩展模块命令,生成显示树是非常方便的。
它可以在PowerShell驱动器,来显示文件系统结构在控制台显示的信息。
例如,下面是在IIS上运行显示树的输出示例:\驱动器:
13
14
15
16
<code>PS> </code><code>Show-Tree</code> <code>IIS:\</code> <code>-Depth</code> <code>3</code>
<code>IIS:\</code>
<code>├──AppPools</code>
<code>│ ├──ASP.NET v4.0</code>
<code>│ │ └──WorkerProcesses</code>
<code>│ ├──ASP.NET v4.0 Classic</code>
<code>│ ├──Classic .NET AppPool</code>
<code>│ └──DefaultAppPool</code>
<code>│ └──WorkerProcesses</code>
<code>├──Sites</code>
<code>│ └──</code><code>Default</code> <code>Web Site</code>
<code>│ ├──aspnet_client</code>
<code>│ └──Blog</code>
<code>└──SslBindings</code>
在一般情况下,在PowerShell驱动器执行条件,可以是容器也可以是项目。
我们在上面看到的仅是容器项目。
例如,在项目属性为默认应用:
17
18
19
20
21
22
23
24
25
26
27
<code>PS> </code><code>Show-Tree</code> <code>IIS:\AppPools\DefaultAppPool</code> <code>-ShowProperty</code>
<code>IIS:\AppPools\DefaultAppPool</code>
<code>├──Property: applicationPoolSid = S-1-5-82-3006700770-424185619-1745488364-7...</code>
<code>├──Property: Attributes = Microsoft.IIs.PowerShell.Framework.ConfigurationAt...</code>
<code>├──Property: autoStart = True</code>
<code>├──Property: ChildElements = Microsoft.IIs.PowerShell.Framework.Configuratio...</code>
<code>├──Property: CLRConfigFile =</code>
<code>├──Property: cpu = Microsoft.IIs.PowerShell.Framework.ConfigurationElement</code>
<code>├──Property: ElementTagName = add</code>
<code>├──Property: enable32BitAppOnWin64 = False</code>
<code>├──Property: enableConfigurationOverride = True</code>
<code>├──Property: failure = Microsoft.IIs.PowerShell.Framework.ConfigurationElement</code>
<code>├──Property: ItemXPath = /system.applicationHost/applicationPools/add[</code><code>@name</code><code>=...</code>
<code>├──Property: managedPipelineMode = Integrated</code>
<code>├──Property: managedRuntimeLoader = webengine4.dll</code>
<code>├──Property: managedRuntimeVersion = v2.0</code>
<code>├──Property: Methods = Microsoft.IIs.PowerShell.Framework.ConfigurationMetho...</code>
<code>├──Property: passAnonymousToken = True</code>
<code>├──Property: processModel = Microsoft.IIs.PowerShell.Framework.Configuration...</code>
<code>├──Property: queueLength = 1000</code>
<code>├──Property: recycling = Microsoft.IIs.PowerShell.Framework.ConfigurationEle...</code>
<code>├──Property: Schema = Microsoft.IIs.PowerShell.Framework.ConfigurationElemen...</code>
<code>├──Property: startMode = OnDemand</code>
<code>├──Property: state = Started</code>
<code>├──Property: workerProcesses = Microsoft.IIs.PowerShell.Framework.Configurat...</code>
<code>└──WorkerProcesses</code>
这样来使用扩展模块命令的IIS:\驱动器可以显示更多的信息,如托管PipelineMode使用以及使用哪个版本的、NET运行时,它的应用程序池是什么?。
有了这个信息我们变得更容易弄清楚如何更改这些设置:
<code>PS> </code><code>Set-ItemProperty</code> <code>IIS:\AppPools\DefaultAppPool managedRuntimeVersion v4.0</code>
好了今天cantgis把周末的博文补上啦,我们明天再会。。
本文转自cantgis 51CTO博客,原文链接:http://blog.51cto.com/cantgis/1236121,如需转载请自行联系原作者