美文网首页运维监控运维驿站部署运维
Powershell札记6_信息详解、管道、格式化输出

Powershell札记6_信息详解、管道、格式化输出

作者: 皮皮大 | 来源:发表于2019-06-26 20:46 被阅读0次

获取帮助信息

PS中获取帮助信息有3种方法,后两种是cmdlet方式,使用广泛

  • -?:copy-item -?
  • get-command
  • get-help
PS C:\WINDOWS\system32> get-help   get-command  -parameter *

-All

    是否必需?                    False
    位置?                        已命名
    是否接受管道输入?            True (ByPropertyName)
    参数集名称          (所有)
    别名                     无
    动态?                    false


-ArgumentList <Object[]>

    是否必需?                    False
    位置?                        1
    是否接受管道输入?            True (FromRemainingArguments)
    参数集名称          (所有)
    别名                     Args
    动态?                    false


-CommandType <CommandTypes>

    是否必需?                    False
    位置?                        已命名
    是否接受管道输入?            True (ByPropertyName)
    参数集名称          AllCommandSet
    别名                     Type
    动态?                    false


-FullyQualifiedModule <ModuleSpecification[]>

    是否必需?                    False
    位置?                        已命名
    是否接受管道输入?            True (ByPropertyName)
    参数集名称          (所有)
    别名                     无
    动态?                    false


-ListImported

    是否必需?                    False
    位置?                        已命名
    是否接受管道输入?            True (ByPropertyName)
    参数集名称          (所有)
    别名                     无
    动态?                    false


-Module <string[]>

    是否必需?                    False
    位置?                        已命名
    是否接受管道输入?            True (ByPropertyName)
    参数集名称          (所有)
    别名                     PSSnapin
    动态?                    false


-Name <string[]>

    是否必需?                    False
    位置?                        0
    是否接受管道输入?            True (ByValue, ByPropertyName)
    参数集名称          AllCommandSet
    别名                     无
    动态?                    false


-Noun <string[]>

    是否必需?                    False
    位置?                        已命名
    是否接受管道输入?            True (ByPropertyName)
    参数集名称          CmdletSet
    别名                     无
    动态?                    false


-ParameterName <string[]>

    是否必需?                    False
    位置?                        已命名
    是否接受管道输入?            False
    参数集名称          (所有)
    别名                     无
    动态?                    false


-ParameterType <PSTypeName[]>

    是否必需?                    False
    位置?                        已命名
    是否接受管道输入?            False
    参数集名称          (所有)
    别名                     无
    动态?                    false


-ShowCommandInfo

    是否必需?                    False
    位置?                        已命名
    是否接受管道输入?            False
    参数集名称          (所有)
    别名                     无
    动态?                    false


-Syntax

    是否必需?                    False
    位置?                        已命名
    是否接受管道输入?            True (ByPropertyName)
    参数集名称          (所有)
    别名                     无
    动态?                    false


-TotalCount <int>

    是否必需?                    False
    位置?                        已命名
    是否接受管道输入?            True (ByPropertyName)
    参数集名称          (所有)
    别名                     无
    动态?                    false


-Verb <string[]>

    是否必需?                    False
    位置?                        已命名
    是否接受管道输入?            True (ByPropertyName)
    参数集名称          CmdletSet
    别名                     无
    动态?                    false

管道

  • 概念:将前一个命令的结果当做后一个命令的输入。
  • cmd、bash均支持管道。PS中的管道符号也为|
  • cmd中管道传递的文本信息,PS中的管道传递的对象.NET
get-location cmdlet  # 返回的是`pathinfo`对象,对象是一个信息报,报中包含各种相关的信息
  • 面向对象,对象有属性和方法,都可以认为是对象的成员。PS对象具有众多的成员,查看成果get-member
PS C:\WINDOWS\system32> get-location |get-member


   TypeName:System.Management.Automation.PathInfo

Name         MemberType Definition
----         ---------- ----------
Equals       Method     bool Equals(System.Object obj)
GetHashCode  Method     int GetHashCode()
GetType      Method     type GetType()
ToString     Method     string ToString()
Drive        Property   System.Management.Automation.PSDriveInfo Drive {get;}
Path         Property   string Path {get;}
Provider     Property   System.Management.Automation.ProviderInfo Provider {get;}
ProviderPath Property   string ProviderPath {get;}
  • get-member cmdlet支持:force、inputobject、membertype、name、static、view等参数
PS C:\WINDOWS\system32> get-location |get-member -static


   TypeName:System.Management.Automation.PathInfo

Name            MemberType Definition
----            ---------- ----------
Equals          Method     static bool Equals(System.Object objA, System.Object objB)
ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System.Object objB)

格式化输出

Powershell中支持4个格式化输出cmdlet,四个命令均需要管道命令输出对象作为输入,同时4个命令均有默认的输出属性。

  • format-wide:默认仅输出一个默认属性
    • 利用format-wide 的property参数输出comandtype信息。
    • get-command | format-wide -property commandtype -colum 6:输出指定的列
PS C:\WINDOWS\system32> get-command  | format-wide

Add-ProvisionedAppxPackage                                                Add-ProvisioningPackage
Add-TrustedProvisioningCertificate                                        Apply-WindowsUnattend
Disable-PhysicalDiskIndication                                            Disable-StorageDiagnosticLog
Enable-PhysicalDiskIndication                                             Enable-StorageDiagnosticLog
Flush-Volume                                                              Get-DiskSNV
Get-PhysicalDiskSNV                                                       Get-ProvisionedAppxPackage
Get-StorageEnclosureSNV                                                   Initialize-Volume
Move-SmbClient                                                            Optimize-ProvisionedAppxPackages
Remove-EtwTraceSession                                                    Remove-ProvisionedAppxPackage
Remove-ProvisioningPackage                                                Remove-TrustedProvisioningCertificate
Set-EtwTraceSession                                                       Write-FileSystemCache
A:                                                                        Add-BCDataCacheExtension
Add-BitLockerKeyProtector                                                 Add-DnsClientNrptRule
Add-DtcClusterTMMapping                                                   Add-EtwTraceProvider
省略.....
  • format-list:
    • format-list列表的形式输出信息
    • format-listproperty参数可以指定输出的属性多少
PS C:\WINDOWS\system32> get-location | format-list

Drive        : C
Provider     : Microsoft.PowerShell.Core\FileSystem
ProviderPath : C:\WINDOWS\system32
Path         : C:\WINDOWS\system32
PS C:\WINDOWS\system32> get-location | format-list -property path   # path 输出指定的path属性

Path : C:\WINDOWS\system32

---
PS C:\WINDOWS\system32> get-location | format-list -property *  # 利用*输出所有的属性

Drive        : C
Provider     : Microsoft.PowerShell.Core\FileSystem
ProviderPath : C:\WINDOWS\system32
Path         : C:\WINDOWS\system32

  • format-table:以表格的形式输出当前的路径
PS C:\WINDOWS\system32> get-location | format-table

Path
----
C:\WINDOWS\system32


PS C:\WINDOWS\system32> get-location | format-table -property *  # 以表格的形式输出所有的路径

Drive Provider                             ProviderPath        Path
----- --------                             ------------        ----
C     Microsoft.PowerShell.Core\FileSystem C:\WINDOWS\system32 C:\WINDOWS\system32

利用property属性来输出指定的信息CPU、path、id等信息

PS C:\WINDOWS\system32> get-process | format-table  -property  path,cpu,id,pm

Path                                                                                                             CPU            Id        PM
----                                                                                                             ---            --        --
                                                                                                                              3228  83533824
                                                                                                                 1824.828125  7528 137388032
                                                                                                                 47.890625    3432  81133568
                                                                                                                 119.234375   8516   1884160
C:\Program Files\DellTPad\Apntex.exe                                                                             0.015625     8848   1957888
C:\Program Files\DellTPad\Apoint.exe                                                                             11.34375     7404   3551232
  • format-custom

输出各种不同的进程process信息

PS C:\WINDOWS\system32> get-process | format-custom

class Process
{
  Id = 3228
  Handles = 1667
  CPU =
  SI = 0
  Name = 360EntClient
}

class Process
{
  Id = 7528
  Handles = 1180
  CPU = 1836.546875
  SI = 1
  Name = 360EntClient
}

class Process
{
  Id = 3432
  Handles = 888
  CPU = 48.390625
  SI = 1
  Name = 360tray
}
......

相关文章

网友评论

    本文标题:Powershell札记6_信息详解、管道、格式化输出

    本文链接:https://www.haomeiwen.com/subject/jzbhcctx.html