1. PowerShell中的格式


1. PowerShell中的对象

  • win平台下的面向对象概念一致,即指我们收集信息或者执行操作的行为。包括属性(信息,我们可以收集)和方法(我们可以执行)。

     

有一个生动的例子——“灯泡”。对象是显而易见的,它是一个灯泡。一个灯泡的属性可能包括了其颜色,功率和类型(荧光灯,白炽灯或卤素灯)。对于它的操作,或者称之为方法,则是我们可以执行的行为,如打开和关闭。这很容易理解!

 

  • 查看GET-service的属性和方法

     

    使用"get-member"的参数来查看"get-service"的所有的属性类对象,或者方法类对象

     

  1. 属性


PS C:\Users\Administrator> get-service | get-member-membertype property

 

 

   TypeName:System.ServiceProcess.ServiceController

 

Name               MemberType Definition

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

CanPauseAndContinue Property   System.Boolean CanPauseAndContinue {get;}

CanShutdown        Property   System.BooleanCanShutdown {get;}

CanStop            Property   System.Boolean CanStop{get;}

Container          Property  System.ComponentModel.IContainer Container {g...

DependentServices   Property   System.ServiceProcess.ServiceController[]Dep...

DisplayName        Property   System.StringDisplayName {get;set;}

MachineName        Property   System.StringMachineName {get;set;}

ServiceHandle      Property  System.Runtime.InteropServices.SafeHandle Ser...

ServiceName        Property   System.StringServiceName {get;set;}

ServicesDependedOn Property  System.ServiceProcess.ServiceController[] Ser...

ServiceType        Property  System.ServiceProcess.ServiceType ServiceType...

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

Status              Property  System.ServiceProcess.ServiceControllerStatus...



B.方法


PS C:\Users\Administrator> get-service |get-member -membertype method  

TypeName: System.ServiceProcess.ServiceController

 

Name                      MemberType Definition

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

Close                     Method     System.Void Close()

Continue                  Method     System.Void Continue()

CreateObjRef             Method     System.Runtime.Remoting.ObjRef CreateOb...

Dispose                   Method     System.Void Dispose()

Equals                    Method     bool Equals(System.Object obj)

ExecuteCommand          Method     System.Void ExecuteCommand(int command)

GetHashCode               Method     int GetHashCode()

GetLifetimeService        Method     System.Object GetLifetimeService()

GetType                   Method     type GetType()

InitializeLifetimeService Method     System.Object InitializeLifetimeService()

Pause                     Method     System.Void Pause()

Refresh                   Method     System.Void Refresh()

Start                     Method     System.Void Start(), System.Void Start(...

Stop                      Method     System.Void Stop()

ToString                  Method     string ToString()

WaitForStatus             Method     System.Void WaitForStatus(System.Servic..


2. PowerShell中的格式


PowerShell中的格式化输出:


 

Get-Command Format-* <enter>

其结果为:

wKioL1U7qIyxZdYTAAGfUwAJrWI720.jpg

更多实例:

 

get-childitem c:\windows | format-table<enter>

get-childitem c:\windows | format-table-autosize <enter>

get-childitem c:\windows | format-custom<enter>

get-childitem c:\windows | format-list<enter>

get-childitem c:\windows | format-list-Property FullName <enter>

get-childitem c:\windows | format-wide<enter>

实例:(记住管道符):

 

Get-ChildItem C:\Windows -Recurse |Format-List -Property FullName,CreationTime,LastWriteTime<enter>

Get-ChildItem  C: | Format-Wide -Column 3<enter>

Get-Process | Group-Object Company <enter>

Get-EventLog System | Group-Objecteventid<enter>

Get-EventLog System | Group-Object eventid| Sort-Object Count -descending<enter>

Get-Process | ConvertTo-html<enter>

Get-Process | ConvertTo-html | out-file“Processes.html”<enter>

Get-Process | Export-CSV Processes.csv <enter>

 

至于打开文件,使用如下命令即可:

Invoke-Item Processes.html<enter> 

Invoke-Item Processes.csv<enter>

 

看看截图吧(输出为".CSV"文件):

wKioL1U7qXHzTU_dAAKJOH6zx4w701.jpg

使用"Invoke-Item"命令打开:

Invoke-Item Processes.csv <Enter>

wKiom1U7qCzyz06QAAaq4Syt_-Y431.jpg

3.  PowerShell中的常见参数

 

下面这个列表,列举出了“公共参数”(这些参数的名称是我们无法自定义使用的):



-confirm 在执行cmdlet前提示用户。

-debug  提供相关调试信息。

-ErrorAction 提示cmdlet在执行某项操作时可能出现的错误。如:继续,停止等。 

-ErrorVariable  使用一个特定的变量($error)来保存错误信息。

-OutVariable 用于保存输出信息的变量。

-OutBuffer 确定在进行下一次管道传递前需要缓存的对象数量。

-Verbose 为我们提供更多细节。

-whatif  并不会真正执行cmdlet,只是告诉你会发生什么。

-"Tab"键,用于补全命令。



显示帮助:

get-service -<Tab> 

或者使用帮助命令"get-help"

get-help get-service -full <Enter>

 

实例:

Set-ExecutionPolicy Unrestricted -whatif<enter> 

wKiom1U7qFfBQ_rAAADDhz2tVds134.jpg

Set-ExecutionPolicy Unrestricted -confirm<enter> 

wKioL1U7qcOh2m__AAFFrbu5zbg875.jpg

是的,它将返回一条验证操作,以获取用户的进一步许可。只是"Y""A""N""L""?"我们都能轻易理解,那么"S"的作用如下:

wKiom1U7qGyCKc3TAAJurIDkD2I277.jpg

 

参考文献:


http://marui.blog.51cto.com/1034148/290535