PowerShell基础教程(2)——Windows PowerShell 使用初步

PowerShell基础教程(2)——Windows PowerShell 使用初步

若要从“开始”菜单启动 Windows PowerShell,请依次单击“开始”、“所有程序”、Windows PowerShell 1.0 和 Windows PowerShell。

若要从“运行”框启动 Windows PowerShell,请单击“开始”,再单击“运行”,然后键入:

powershell
 
若要从命令提示符 (cmd.exe) 窗口启动 Windows PowerShell,请在命令提示符下键入:

powershell
 
若要查看用于启动 Windows PowerShell 的选项,请在命令提示符窗口中键入:

powershell -?
 
在 Windows PowerShell 打开时,可以使用 Get-Help cmdlet 查找帮助。在 Windows PowerShell 命令提示符下,键入: 

get-help
使用 Windows PowerShell             
本节介绍使用 Windows PowerShell 的基本知识。首先介绍 Get-Help cmdlet,该 cmdlet 显示有关 Windows PowerShell 中 cmdlet 和概念性主题的信息。然后,介绍几个基本的 cmdlet,说明如何使用 cmdlet 参数,然后说明如何设置 cmdlet 输出的格式以获取有用显示中所需的数据。最后的主题说明如何使用别名以便更容易地使用 Windows PowerShell,如何在 Windows PowerShell 中运行传统的 Windows 程序,以及如何管理错误。

Get-Help:获取帮助
Get-Help cmdlet 是用于了解 Windows PowerShell 的有用工具。通过阅读 cmdlet 的说明、了解有关概念并浏览语言主题,您可以开始了解如何使用 Windows PowerShell 了。

感兴趣的第一个主题可能是帮助系统。若要显示有关 Windows PowerShell 中帮助系统的信息,请键入:

get-help
 
然后,您可能会对了解几个基本的 cmdlet(如 Get-Help、Get-Command、Get-Process、Get-Service 和 Get-Eventlog)感兴趣。 

若要显示 cmdlet 的帮助的最简单视图,请键入“get-help”,后跟该 cmdlet 的名称。例如,若要获取 Get-Command cmdlet 的帮助,请键入:

扫描二维码关注公众号,回复: 15227123 查看本文章

get-help get-command
 
如果 cmdlet 帮助的格式设置不正确(即,如果它以 XMLNS 标记开头),则可能是系统上的 Windows PowerShell 执行策略阻止系统加载用于设置 cmdlet 帮助格式的配置文件。有关执行策略的信息,请键入:

get-help about_signing   
 
若要显示 cmdlet 的详细帮助,包括参数说明和示例,请使用 Get-Help 的 Detailed 参数。例如,若要获取 Get-Command cmdlet 的详细帮助,请键入:

get-help get-command -detailed
 

若要显示 cmdlet 的所有可用帮助,包括有关 cmdlet 及其参数的技术信息,请使用 Full 参数。例如,若要获取 Get-Command cmdlet 的完整帮助,请键入:

get-help get-command -full
 
也可以显示帮助文件的所选部分。若要仅查看示例,请使用 Examples 参数。例如,若要显示 Get-Command cmdlet 的示例,请键入:

get-help get-command -examples
 
若要仅查看详细的参数说明,请使用 Get-Help 的 Parameter 参数。可以指定参数的名称,或者使用通配符 (*) 指定所有参数。例如,若要查看 Get-Command 的 TotalCount 参数说明,请键入:

get-help get-command -parameter totalcount
 
若要查看 Get-Command cmdlet 的所有参数,请键入:

get-help get-command -parameter *
 
也可以使用调用 Get-Help 的 Windows PowerShell 函数之一。Help 函数一次显示一整屏帮助内容。Man 函数显示与 Unix 中的手册页类似的帮助。若要使用 Help 和 Man 函数显示 Get-Command cmdlet 的帮助,请键入:

man get-command
 

help get-command
 

请求特定的帮助主题时,Get-Help 将显示该主题的内容。但是使用通配符请求多个主题时,Get-Help 将显示一个主题列表。例如,若要查看“Get”cmdlet 的帮助主题列表,请键入:

get-help get-*

有关 Windows PowerShell 中概念的帮助以“about_”开头。若要显示有关 Windows PowerShell 概念的帮助,请键入“get-help”,后跟概念名称。例如,若要获取有关通配符的帮助,请键入:

get-help about_wildcard
 

<o:p> </o:p>若要显示 Windows PowerShell 中所有概念性帮助主题的列表,请键入:

get-help about_*
 

通过阅读帮助主题并尝试示例,您将了解 Windows PowerShell 的工作原理以及在您的工作中如何使用它。

使用 Cmdlet 
cmdlet(读作“command-let”)是一个内置到外壳程序中的简单的单一功能命令行工具。可以就像使用传统的命令和实用工具那样使用 cmdlet。首先在 Windows PowerShell 命令提示符下键入 cmdlet 的名称。Windows PowerShell 命令不区分大小写,因此可以用任意大小写键入。

例如,可以尝试 Get-Date cmdlet:

C:\PS> get-date

2025 年 11 月 10 日,星期四,下午 4:43:50
 

<o:p> </o:p>若要在会话中列出 cmdlet,请使用 Get-Command cmdlet,且不带任何命令参数。

PS> get-command

<o:p> </o:p>

CommandType     Name                            Definition

-----------     ----                            ----------

Cmdlet          Add-Content                     Add-Content [-Path] <String[...

Cmdlet          Add-History                     Add-History [[-InputObject] ...

Cmdlet          Add-Member                      Add-Member [-MemberType] <PS...

...


 

默认的 Get-Command 显示有以下三列:CommandType、Name 和 Definition。列出 cmdlet 时,Definition 列显示 cmdlet 的语法。语法中的省略号 (…) 指示数据被截断。 

Get-Command cmdlet 还获取除 cmdlet 之外的命令和命令元素,其中包括在 Windows PowerShell 中可用的别名(命令昵称)、函数和可执行文件。 

通过使用 Get-Command 的 Name 参数,以下命令列出了在 Windows PowerShell 中可用的可执行文件。

PS> get-command *.exe

<o:p> </o:p>

CommandType Name                   Definition

----------- ----                   ----------

Application 000StTHK.exe           C:\WINDOWS\system32\000StTHK.exe

Application 00THotkey.exe          C:\WINDOWS\system32\00THotkey.exe

Application accwiz.exe             C:\WINDOWS\system32\accwiz.exe

...
 

列出可执行文件时,Defintion 列包含可执行文件的完整路径。

然后,尝试一些其他 cmdlet,如 Get-Process、Get-Service、Get-EventLog 和 Get-Alias。 

如果对简单的“Get-”cmdlet 已非常熟悉,请尝试更有趣的 cmdlet,如 Get-WmiObject。此 cmdlet 非常有用,因为使用它可以查看和更改远程计算机的组件。例如,以下命令获取有关 Server01 远程计算机上 BIOS 的信息:

get-wmiobject win32_bios -computername server01
 

如果需要任何 cmdlet 的帮助,请键入:

get-help <cmdlet 名称> -detailed

例如:

get-help get-alias -detailed。

了解对象:Get-Member
Get-Member 是最有用的 cmdlet 之一,它显示有关命令返回的 .NET 对象的信息。该信息包括对象的类型、属性和方法。

若要使用 Get-Member,请使用管道运算符 (|) 将命令结果发送到 Get-Member。例如:

get-service | get-member

此命令显示 Get-Service 实际上返回了一组 System.ServiceProcess.ServiceController 对象 -- 计算机上的每个服务都有一个对象。

TypeName:System.ServiceProcess.ServiceController

<o:p> </o:p>

Name                      MemberType    Definition

----                      ----------    ----------

Name                      AliasProperty Name = ServiceName

add_Disposed              Method        System.Void add_Disposed(EventHandler value)

Close                     Method        System.Void Close()

Continue                  Method        System.Void Continue()

CreateObjRef              Method        System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)

Dispose                   Method        System.Void Dispose()

Equals                    Method        System.Boolean Equals(Object obj)

ExecuteCommand            Method        System.Void ExecuteCommand(Int32 command)

get_CanPauseAndContinue   Method        System.Boolean get_CanPauseAndContinue()

get_CanShutdown           Method        System.Boolean get_CanShutdown()

get_CanStop               Method        System.Boolean get_CanStop()

get_Container             Method        System.ComponentModel.IContainer get_Container()

get_DependentServices     Method        System.ServiceProcess.ServiceController[] get_DependentServices()

get_DisplayName           Method        System.String get_DisplayName()

get_MachineName           Method        System.String get_MachineName()

get_ServiceHandle         Method        System.Runtime.InteropServices.SafeHandle get_ServiceHandle()

get_ServiceName           Method        System.String get_ServiceName()

get_ServicesDependedOn    Method        System.ServiceProcess.ServiceController[] get_ServicesDependedOn()

get_ServiceType           Method        System.ServiceProcess.ServiceType get_ServiceType()

get_Site                  Method        System.ComponentModel.ISite get_Site()

get_Status                Method        System.ServiceProcess.ServiceControllerStatus get_Status()

GetHashCode               Method        System.Int32 GetHashCode()

GetLifetimeService        Method        System.Object GetLifetimeService()

GetType                   Method        System.Type GetType()

InitializeLifetimeService Method        System.Object InitializeLifetimeService()

Pause                     Method        System.Void Pause()

Refresh                   Method        System.Void Refresh()

remove_Disposed           Method        System.Void remove_Disposed(EventHandler value)

set_DisplayName           Method        System.Void set_DisplayName(String value)

set_MachineName           Method        System.Void set_MachineName(String value)

set_ServiceName           Method        System.Void set_ServiceName(String value)

set_Site                  Method        System.Void set_Site(ISite value)

Start                     Method        System.Void Start(), System.Void Start(String[] args)

Stop                      Method        System.Void Stop()

ToString                  Method        System.String ToString()

WaitForStatus             Method        System.Void WaitForStatus(ServiceControllerStatus desiredStatus), System.Voi...

CanPauseAndContinue       Property      System.Boolean CanPauseAndContinue {get;}

CanShutdown               Property      System.Boolean CanShutdown {get;}

CanStop                   Property      System.Boolean CanStop {get;}

Container                 Property      System.ComponentModel.IContainer Container {get;}

DependentServices         Property      System.ServiceProcess.ServiceController[] DependentServices {get;}

DisplayName               Property      System.String DisplayName {get;set;}

MachineName               Property      System.String MachineName {get;set;}

ServiceHandle             Property      System.Runtime.InteropServices.SafeHandle ServiceHandle {get;}

ServiceName               Property      System.String ServiceName {get;set;}

ServicesDependedOn        Property      System.ServiceProcess.ServiceController[] ServicesDependedOn {get;}

ServiceType               Property      System.ServiceProcess.ServiceType ServiceType {get;}

Site                      Property      System.ComponentModel.ISite Site {get;set;}

Status                    Property      System.ServiceProcess.ServiceControllerStatus Status {get;}
 

此信息看起来技术性很强,但是它实际上非常实用。
 

猜你喜欢

转载自blog.csdn.net/gggwfn1982/article/details/131015455