Powershell基礎


 

一.對象

    從事開發工作的都知道,面向對象的概念是為了更好用程序語言解決現實問題二提出的。

  在Powershell中,對象是值我們收集信息或者執行操作的行為。包括屬性和方法。Powershell中的一切皆為對象。我們所有的操作都是對對象的操作。

二.Get-Member

  Get-Member 可以現實有關命令返回的 .NET 對象的信息。該信息包括對象的類型、屬性和方法。

  若要使用 Get-Member,請使用管道運算符 (|) 將命令結果發送到 Get-Member。例如:

  get-Service | get-member

  此命令顯示 Get-Service 實際上返回了一組 System.ServiceProcess.ServiceController 對象 -- 計算機上的每個服務都有一個對象。

   TypeName: System.ServiceProcess.ServiceController

Name                      MemberType    Definition
----                      ----------    ----------
Name                      AliasProperty Name = ServiceName
RequiredServices          AliasProperty RequiredServices = ServicesDependedOn
Disposed                  Event         System.EventHandler Disposed(System....
Close                     Method        System.Void Close()
Continue                  Method        System.Void Continue()
CreateObjRef              Method        System.Runtime.Remoting.ObjRef Creat...
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 InitializeLifetimeServ...
Pause                     Method        System.Void Pause()
Refresh                   Method        System.Void Refresh()
Start                     Method        System.Void Start(), System.Void Sta...
Stop                      Method        System.Void Stop()
ToString                  Method        string ToString()
WaitForStatus             Method        System.Void WaitForStatus(System.Ser...
CanPauseAndContinue       Property      System.Boolean CanPauseAndContinue {...
CanShutdown               Property      System.Boolean CanShutdown {get;}
CanStop                   Property      System.Boolean CanStop {get;}
Container                 Property      System.ComponentModel.IContainer Con...
DependentServices         Property      System.ServiceProcess.ServiceControl...
DisplayName               Property      System.String DisplayName {get;set;}
MachineName               Property      System.String MachineName {get;set;}
ServiceHandle             Property      System.Runtime.InteropServices.SafeH...
ServiceName               Property      System.String ServiceName {get;set;}
ServicesDependedOn        Property      System.ServiceProcess.ServiceControl...
ServiceType               Property      System.ServiceProcess.ServiceType Se...
Site                      Property      System.ComponentModel.ISite Site {ge...
Status                    Property      System.ServiceProcess.ServiceControl...

TypeName:對象的類型

Property:對象所擁有的屬性    如要顯示對象的屬性請鍵入 : (Get-Service <服務名稱>).<屬性名稱>  例如:(get-service alerter).canpauseandcontinue

Method:對象所擁有的方法      如要執行對象的方法請鍵入:(Get-Service <服務名稱>).<方法名稱()> 例如:(get-service schedule).stop()

 

三.CmdLet參數

  

  Cmdlet 參數的標識方法是在參數名稱前放一個連字符 (-)。(在 Windows PowerShell 中斜杠(/ )不用於參數。)

  鍵入參數名稱時,可以鍵入整個名稱,但是只需鍵入必要的字符數,能夠將該參數名稱與 cmdlet 的其他參數名稱區分開即可。

  例如,Get-Help cmdlet 具有一個名為“Detailed”的參數,但是可以鍵入“-det”,這就足以將它與 Get-Help 的Debug 參數區分開。

  一些參數名稱是可選的。可以通過鍵入參數值而不是鍵入參數名稱來使用參數。但是,如果省略參數名稱,則參數值出現在命令中的位置必須與它出現在語法圖表中的位置相同。

  例如,Get-Help cmdlet 具有一個 Name 參數,該參數指定 cmdlet 或概念的名稱。可以鍵入 Name 參數的名稱,也可以省略它。若要獲取 Get-Alias 命令的幫助,可以鍵入:get-help -name get-alias 或 get-help get-alias

  若要查找可選參數名稱,請參閱幫助文件中的語法塊。可選參數名稱出現在方括號中,例如:Get-Help Get-Command

名稱
    Get-Command

摘要
    獲取有關 cmdlet 以及有關 Windows PowerShell 命令的其他元素的基本信息。


語法
    Get-Command [[-Name] <string[]>] [-CommandType {Alias | Function | Filter |
     Cmdlet | ExternalScript | Application | Script | All}] [[-ArgumentList] <O
    bject[]>] [-Module <string[]>] [-Syntax] [-TotalCount <int>] [<CommonParame
    ters>]

    Get-Command [-Noun <string[]>] [-Verb <string[]>] [[-ArgumentList] <Object[
    ]>] [-Module <string[]>] [-Syntax] [-TotalCount <int>] [<CommonParameters>]

 

如上:Get-Command 幫助信息中語法部分  方括號[-name],[-Syntax]等都為可選參數名稱

 

四.命令輸出格式

在 Windows PowerShell 中,使用Fromat cmdlet設置輸出格式,主要有以下四種:

  • Format-List   列表格式  可指定顯示的屬性  如:Format-list  -Property * (*顯示所有 默認顯示所有,還可指定顯示名稱用,隔開如name,type)
  • Format-Custom  可根據試圖來定義顯示的格式
  • Format-Table   表格式 可指定顯示的屬性   如:Format-list  -Property * (*顯示所有 默認顯示所有,還可指定顯示名稱用,隔開如name,type)
  • Format-Wide   一個對象只顯示一個屬性的表格式 可指定顯示的屬性  如:Format-list  -Property * (*顯示所有 默認顯示所有,還可指定顯示名稱用,隔開如name,type)

Windows PowerShell 調用默認的格式化程序,該程序由所顯示的數據類型確定。格式化程序確定顯示輸出的哪些屬性以及用列表還是表顯示它們。

例如,使用 Get-Service cmdlet 時,默認顯示是一個包含三列的表,如下所示:

C:/PS> get-service

Status   Name               DisplayName

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

Running  AdtAgent           Event Forwarder

Stopped  Alerter            Alerter

Running  ALG                Application Layer Gateway Service

 

 

若要設置 cmdlet 的輸出格式,可使用管道運算符 (|) 將命令輸出發送到 Format cmdlet。

例如,以下命令將 Get-Service 命令的輸出發送到 Format-List cmdlet。因此,對於每個服務,服務數據被設置為列表格式。

C:/PS> get-service | format-list

Name                :AdtAgent

DisplayName         :Event Forwarder

Status              :Running

DependentServices   : {}

ServicesDependedOn  :{eventlog, dnscache}

CanPauseAndContinue :False

CanShutdown         :True

CanStop             :True

ServiceType         :Win32OwnProcess

 

Name                :Alerter

DisplayName         :Alerter

Status              :Stopped

DependentServices   : {}

ServicesDependedOn  :{LanmanWorkstation}

CanPauseAndContinue :False

CanShutdown         :False

CanStop             :False

ServiceType         :Win32ShareProcess

 

Name                :ALG

DisplayName         :Application Layer Gateway Service

Status              :Running

DependentServices   : {}

 

 

在此格式中,顯示了所有的信息,而不是默認顯示的三列,因為默認的格式化程序 Format-Table 因無法在屏幕上顯示三列以上的信息。

除了確定數據出現在列表中還是表中之外,還可以確定顯示對象的哪些屬性。例如,Get-Service 的默認顯示僅顯示服務對象的 Status、Name 和 DisplayName 屬性。

若要查看對象的所有屬性,請使用管道運算符 (|) 將命令輸出發送到 Get-Member cmdlet。

例如,若要查看服務對象的所有屬性,請鍵入:

get-service | get-member -membertype *property

 

TypeName:System.ServiceProcess.ServiceController

 

Name                MemberType    Definition

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

Name                AliasProperty Name = ServiceName

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;}

 

 

由於所有這些屬性都在 Get-Service 為每個服務檢索的對象中,因此可以顯示其中任一屬性或所有屬性。使用Format cmdlet 的 Property 參數可以選擇要顯示的屬性以及在其中顯示它們的其他屬性。例如,以下命令使用Format-Table 命令僅顯示服務的 Name、ServiceType 和 CanShutDown 屬性。

get-service | format-table name,Servicetype,Canshutdown

顯示:

Name                                      ServiceType               CanShutdown
----                                      -----------               -----------
AdobeFlashPlayerUpdateSvc             Win32OwnProcess                     False
AeLookupSvc                         Win32ShareProcess                     False
AERTFilters                           Win32OwnProcess                     False
ALG                                   Win32OwnProcess                     False
AppIDSvc                            Win32ShareProcess                     False
Appinfo                             Win32ShareProcess                     False
AppMgmt                             Win32ShareProcess                     False
aspnet_state                          Win32OwnProcess                      True
AudioEndpointBuilder                Win32ShareProcess                     False
AudioSrv                            Win32ShareProcess                     False
AxInstSV                            Win32ShareProcess                     False

 

 

以上只是對顯示格式的一個大概的描述及基本使用,如需要詳細了解各個用法用途可使用get-help查看。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM