PowerShell 简介(超越 Bash)






4.14/5 (6投票s)
微软现在推出了 Windows PowerShell(代号 MONAD),一个非常强大的基于 .NET 的 shell。
引言
多年来,UNIX/Linux 管理员一直瞧不起 Windows 环境,主要原因之一是 Windows 没有一个好用的 CLI。UNIX 一直拥有 sh、bash、ksh 这些优秀的 shell。微软现在推出了 Windows PowerShell(代号 MONAD),一个非常强大的基于 .NET 的 shell。Shell 是系统管理员自动化任务的一个强大工具。PowerShell 拥有与当前 UNIX shell 相同的功能,例如:
- 组合 - 这是将许多小命令组合起来以创建复杂功能
- 管道 - 能够将信息从一个命令传递到下一个命令
它还有一些特性使其区别于 nix* 系统上可用的其他 shell 环境
- 面向对象 - PowerShell 是完全面向对象的。这是我最喜欢的特性之一,也是我认为它比 bash 强大得多的原因。例如,在 bash 中,您可以将进程列表通过管道传递给另一个应用程序。使用此文本列表,我可以搜索它并找到与特定字符串匹配的进程 ID。然而在 PowerShell 中,我将获得一个进程对象列表,这些对象具有属性和方法。
- 访问完整的 .NET 2.0 程序集 - 无需多言?
- 可扩展性 - 创建您自己的 cmdlet 程序集,以便人们可以脚本化您的应用程序。Exchange 2007 是第一个支持 PowerShell 的 Windows 应用程序之一。通过 PowerShell cmdlet,您可以运行脚本来管理和自动化 Exchange 的管理。例如,(PS> get-mailbox *noob* | set-mailbox -SendStorageQuota 1GB) 将为名称中包含字符串 noob 的所有名称设置配额。
- 托管 - 能够在您的 .NET 应用程序中托管 PowerShell,只需不到 10 行代码。由于 PowerShell 是面向对象的,您可以将对象从托管程序传递到脚本,只需一行代码。
- 能够使用 Windows COM 对象。
- 强大的 XML 支持。
那么,让我们开始吧...
- 首先从这里获取 .NET 2.0 运行时:.NET Runtime 下载
- 然后从这里获取 PowerShell:下载 PowerShell
- 接下来我们需要一个 IDE(这个工具是学习 PowerShell 和编写脚本的黄金利器):http://www.karlprosser.com/coder/?page_id=14
在使用 IDE 之前,我们需要启用所有脚本的执行。所以从 PowerShell(开始 -> Windows PowerShell -> Windows PowerShell),我们启用脚本。
PS > Get-Help Set-ExecutionPolicy - detailed
这将为我们提供有关 Set-ExecutionPolicy 命令的一些信息。
PS > Set-ExecutionPolicy Unrestricted
允许您从外部源运行脚本。
附注:它由许多 cmdlet 组成,这些 cmdlet 是由 PS 解释的 DLL。将来,我们可以了解如何创建自己的 cmdlet,以便人们可以控制我们的应用程序。现在,让我们使用一些内置的 cmdlet。
一些示例
PS > get-service | get-member
这里 get-service cmdlet 将服务对象列表通过管道传递给另一个 cmdlet。Get-member 然后列出“System.ServiceProcess.ServiceController
”类中所有可用的成员。
PS > get-service schedule | format-list -property *
在这里,get-service 获取服务对象“schedule”并将其通过管道传递给另一个格式化属性输出的 cmdlet。还有其他格式可用,请尝试...
PS > get-service | Format-List
PS > get-service | Format-Custom
PS > get-service | Format-Table
PS > get-service | Format-Wide
PS > get-service | format-table name, Servicetype, Canshutdown
这里我们通过管道获取 ipconfig 的信息并使用 findstr 命令进行过滤。
PS > ipconfig | findstr "Address"
请注意:ipconfig 是一个返回文本的命令,因此它不返回具有属性和函数的对象。
Get-Help
PS 附带了大量的文档。Get-Help 很像 nix 的 man 命令。
其他选项
PS > Get-help get-command -detailed
PS > Get-help get-command -full
PS > Get-help get-command -examples
PS > Get-help get-command -parameter *
常用命令
PS > Get-Command - Gets basic information about cmdlets and about other elements
of Windows PowerShell commands.
PS > Get-Process - Gets the processes that are running on the local computer.
PS > Get-Service - Gets the services on the local computer
PS > Get-Eventlog - Gets information about local event logs or the entries stored in
those event logs.
PS > Start-transcript, stop-transcript - Lets you record sessions/interaction with PS
别名 (get-alias)
别名实际上是命令的别名。例如:'ps' 是 get-process 的别名。
PS > get-alias ps
PS > get-alias | where-object {$_.definition -eq "set-location"}
PS > set-alias gh get-help
PS > set-alias np c:\windows\notepad.exe
PS > remove-item alias:ls
变量、循环和字符串操作
作为一种脚本语言,PS 支持变量、循环和字符串操作。
将结果放入变量
$result = ipconfig
使用 for
循环打印带行号的内容。
for ($i=0 ; $i -lt $result.length; $i++ )
{
"{0} {1}" -f $i, $result[$i]
}
使用 foreach
...
$i = 1;
foreach($singleLine in $result)
{
$i++
"{0} {1}" -f $i, $singleLine
}
使用内置的字符串函数...
PS > "How dooz is this".split()
函数
可以像任何脚本语言一样创建函数。可以传递参数并返回数值。
$result = ipconfig function AddLineNumbers { $i = 1; foreach($singleLine in $args[0]) { $i++ "{0} {1}" -f $i, $singleLine } } AddLineNumbers $result
WMI(Windows Management Instrumentation)脚本
WMI 是 Windows 管理的关键技术。WMI 使您能够访问 Windows 系统的大量信息。Get-WmiObject
cmdlet 提供了对 WMI 对象的访问。
例如,这里我们使用 Get-WmiObject
来获取操作系统和 BIOS 信息。
Get-WmiObject win32_bios -computername 127.0.0.1
Get-WmiObject -Class Win32_OperatingSystem -ComputerName 127.0.0.1
.NET 脚本
在脚本中,一个常见的操作是发送电子邮件。首先,我们需要使用 New-Object
创建一个邮件消息 .NET 对象。
PS> $mailMsg = New-Object -TypeName System.Net.Mail.MailMessage(
" sender@net.com.au"," recipient@net.com.au");
然后我们可以设置一些选项和邮件消息。
PS> $mailMsg.IsBodyHtml = $true;
PS> $mailMsg.Body = "<html>This is a sample mail message sent from PowerShell</html>";
PS> $mailMsg.Subject = "This is a sample mail message sent from PowerShell";
创建完邮件消息后,我们需要创建客户端对象并发送电子邮件。
PS> $client = New-Object -TypeName System.Net.Mail.SmtpClient("mail.optusnet.com.au");
PS> $client.Send($mailMsg);
Shell profile
所有别名、函数和变量仅添加到当前会话。为了保留更改,您需要将它们添加到您的 profile 中。以下是 profile 位置列表
%windir%\system32\WindowsPowerShell\v1.0\profile.ps1
此 profile 适用于所有用户和所有 shell。
%windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1
此 profile 适用于所有用户,但仅适用于 Microsoft.PowerShell shell。
%UserProfile%\My Documents\WindowsPowerShell\profile.ps1
此 profile 仅适用于当前用户,但影响所有 shell。
%UserProfile%\\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
此 profile 仅适用于当前用户和 Microsoft.PowerShell shell。
您还可以创建、共享和分发 profile。
导航
PS 允许使用通用方法导航计算机的文件系统、注册表和证书。要查看可用的导航区域,请使用
get-psdrive
文件系统
照常。
注册表
cd HKLM:
ls
cd system\currentcontrolset\control
深入阅读
- 一个快速资源的精彩页面在这里:http://channel9.msdn.com/wiki/default.aspx/Channel9.WindowsPowerShellQuickStart
- PS 附带了大量的文档。可以查看一下。用户指南是 IT 管理员的绝佳参考。
- Scott Hanselman 制作了一些很棒的 PS 播客。如果您还没有收听 hanselminutes,您应该去看看。
- 官方页面上有很多信息:http://www.microsoft.com/windowsserver2003/technologies/ management/powershell/default.mspx
- 使用 PowerShell 的顶级技巧 - 点击这里。
- PowerShell Analyzer 是一个必不可少的工具(提供少量智能感知和类似于 Windows 的查询分析器来执行命令):http://karlprosser.edify.us/coder/