zoukankan      html  css  js  c++  java
  • 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查看。

  • 相关阅读:
    Python装饰器
    Python常用内建模块
    Python文件的操作
    Python集合的操作
    Python字典的操作
    Python列表元组的操作
    os.path
    Python字符串的操作
    线性回归
    随机森林
  • 原文地址:https://www.cnblogs.com/binw/p/3921643.html
Copyright © 2011-2022 走看看