wang_rx 发表于 2018-8-19 13:00:05

13 powershell三个实用特性和功能实例

昨天:周六和的今天周日的合在一起了,最近Cantgis比较忙。。。
管道绑定参数绑定到脚本区
我看到许多的PowerShell对堆栈溢出 看起来类似下面:
Get-ChildItem *.txt | Foreach {  
Rename-Item -Path $_ -NewName "$($_.basename).bak"
  
}
  上面使用的foreach对象cmdlet是不必要的,因为“重命名”项目接受管道输入路径和新名称参数。
  下面的例子大家可以看到结果
PS> $obj = New-Object PSObject –Property `  
@{Path='C:\Users\Cantgis\foo.txt';NewName='bar.txt'}
  
PS> $obj | Rename-Item –WhatIf
  
What if: Performing operation "Rename File" on Target
  
"Item: C:\Users\Cantgis\foo.txt Destination: C:\Users\Cantgis\bar.txt".
  你也许会想,虽然这可能是一个有趣的问题,但是它是如何对比早期的powershell版本的呢?
  如果使用foreach对象会更好吗?
  powershell有一个诀窍来帮助我们实现这个重命名操作。
  诀窍是,PowerShell将接受任何参数是管道的约束,脚本调用的脚本区的一段。
  你可以看到,如果一个参数是通过获取帮助,例如管道绑定:
PS> Get-Help Rename-Item  
...
  
-LiteralPath
  
...
  
Accept pipeline input? true (ByPropertyName)
  
-Path
  
...
  
Accept pipeline input? true (ByValue, ByPropertyName)
  
-NewName
  
...
  
Position?                  2
  
Accept pipeline input? true (ByPropertyName)
  上面的信息告诉我们,LiteralPath:路径和新名称的参数接受管道输入。
  Get-ChildItem:管道输出,重命名项:cmdlet LiteralPath参数。
  我们可以使用脚本区结合这个诀窍来指定新名称。
PS> Get-ChildItem *.txt | Rename-Item -NewName {"$($_.BaseName).bak"}使用PowerShell 3.0 输出GridView的多重选择
  在PowerShell 3.0的更新GridView的命令支持PassThru参数。
  此外,OUT-GridView控件支持多选的项目传入以及取消操作。
  例如,你可能想从列表中选择进程停止。
PS> Get-Process devenv |  
Select Name,Id,MainWindowTitle |
  
Out-GridView -PassThru | Stop-Process
  此命令显示出GridView的对话框,如下图所示。
  我们可以看到基于MainWindowTitle属性的Visual Studio实例。
  我可以选择一个或多个devenv的进程。
  如果我按“确定”,然后我选择的进程将被停止。
  如果我按出GridView的对话框上的取消按钮,停止管道,也没有进程被停止哦!

使用PowerShell社区的扩展的命令来显示树视图_关于供应商的
  提示:如果需要可以下载免费的PowerShell社区扩展模块(PSCX)。
  PSCX是一组通用的PowerShell命令。它提供的命令之一是显示树,基于PowerShell驱动器,如:

[*]  WSMan:\
[*]  Cert:\
[*]  HKLM:\
[*]  IIS:\ (if you have imported the WebAdministration module)
  通常情况下,如果你想查找的驱动器,你可以使用Windows资源管理器。
  不幸的是,除了那些基于文件系统上的,剩下的Windows资源管理器比不上可视性PowerShell驱动器。
  同样不幸的是,像WSMAN驱动器和IIS:即所包含的功能是不是很发现,隐藏了很多功能。
  这时这个社区提供的扩展模块命令,生成显示树是非常方便的。
  它可以在PowerShell驱动器,来显示文件系统结构在控制台显示的信息。
  例如,下面是在IIS上运行显示树的输出示例:\驱动器:
PS> Show-Tree IIS:\ -Depth 3  
IIS:\
  
├──AppPools
  
│├──ASP.NET v4.0
  
││└──WorkerProcesses

  
│├──ASP.NET v4.0>  
││└──WorkerProcesses
  
│├──Classic .NET AppPool
  
││└──WorkerProcesses
  
│└──DefaultAppPool
  
│   └──WorkerProcesses
  
├──Sites
  
│└──Default Web Site
  
│   ├──aspnet_client
  
│   └──Blog
  
└──SslBindings
  在一般情况下,在PowerShell驱动器执行条件,可以是容器也可以是项目。
  我们在上面看到的仅是容器项目。
  例如,在项目属性为默认应用:
PS> Show-Tree IIS:\AppPools\DefaultAppPool -ShowProperty  
IIS:\AppPools\DefaultAppPool
  
├──Property: applicationPoolSid = S-1-5-82-3006700770-424185619-1745488364-7...
  
├──Property: Attributes = Microsoft.IIs.PowerShell.Framework.ConfigurationAt...
  
├──Property: autoStart = True
  
├──Property: ChildElements = Microsoft.IIs.PowerShell.Framework.Configuratio...
  
├──Property: CLRConfigFile =
  
├──Property: cpu = Microsoft.IIs.PowerShell.Framework.ConfigurationElement
  
├──Property: ElementTagName = add
  
├──Property: enable32BitAppOnWin64 = False
  
├──Property: enableConfigurationOverride = True
  
├──Property: failure = Microsoft.IIs.PowerShell.Framework.ConfigurationElement
  
├──Property: ItemXPath = /system.applicationHost/applicationPools/add[@name=...
  
├──Property: managedPipelineMode = Integrated
  
├──Property: managedRuntimeLoader = webengine4.dll
  
├──Property: managedRuntimeVersion = v2.0
  
├──Property: Methods = Microsoft.IIs.PowerShell.Framework.ConfigurationMetho...
  
├──Property: passAnonymousToken = True
  
├──Property: processModel = Microsoft.IIs.PowerShell.Framework.Configuration...
  
├──Property: queueLength = 1000
  
├──Property: recycling = Microsoft.IIs.PowerShell.Framework.ConfigurationEle...
  
├──Property: Schema = Microsoft.IIs.PowerShell.Framework.ConfigurationElemen...
  
├──Property: startMode = OnDemand
  
├──Property: state = Started
  
├──Property: workerProcesses = Microsoft.IIs.PowerShell.Framework.Configurat...
  
└──WorkerProcesses
  
...
  这样来使用扩展模块命令的IIS:\驱动器可以显示更多的信息,如托管PipelineMode使用以及使用哪个版本的、NET运行时,它的应用程序池是什么?。
  有了这个信息我们变得更容易弄清楚如何更改这些设置:
PS> Set-ItemProperty IIS:\AppPools\DefaultAppPool managedRuntimeVersion v4.0  好了今天cantgis把周末的博文补上啦,我们明天再会。。


页: [1]
查看完整版本: 13 powershell三个实用特性和功能实例