可以在 Windows PowerShell 中运行 Windows 命令行程序,而且可以在 Windows Powershell 提示符下启动具有图形用户界面的 Windows 程序(如记事本和计算器)。还可以捕获 Windows 程序生成的文本,并在 Windows PowerShell 中使用该文本。

例如,以下命令使用 Windows 的 IPConfig、Net 和 Shutdown 命令。

C:\PS> net localgroup administrators /add domain01\user01
The command completed successfully.

C:\PS> ipconfig
Windows IP Configuration
Ethernet adapter Local Area Connection:
       Connection-specific DNS Suffix  . : domain.corp.fabricam.com
       IP Address. . . . . . . . . . . . : 142.20.152.115
       Subnet Mask . . . . . . . . . . . : 255.255.252.0
       Default Gateway . . . . . . . . . : 172.30.180.1

C:\PS> shutdown -r

您甚至可以使用 Windows PowerShell cmdlet(如 Select-String)处理 Windows 程序返回的文本。

例如,以下命令会使用管道运算符将 IPConfig 命令的结果发送到 Windows PowerShell Select-String cmdlet,后者会搜索字符串中的文本。在此情况下,可以使用 Select-String 在 IpConfig 输出中查找模式“255”。

C:\PS> ipconfig | select-string -pattern 255
Subnet Mask . . . . . . . . . . . : 255.255.252.0

当 Windows 命令或工具带有参数时,如 Shutdown 的“-r”(重新启动)参数,Windows PowerShell 会将参数传递给该工具而不解释它们。

但是,如果该工具使用 Windows PowerShell 保留字或者使用 Windows PowerShell 不熟悉的命令格式,如 Nant 的“-D:debug=false”参数(Windows PowerShell 将此参数解释为两个参数:“-D”和“debug=false”),请用引号将参数括起来,以指示 Windows PowerShell 应该将参数发送给该工具而不进行解释。




目录