學習windows powershell(基本信息獲得和顯示)

一、get-help

這個命令我想肯定是初學者最常用的,當然老手也得常用,我基本上不相信有人能把所有參數都能記下來的,如果真能全部記下來,我真的是五體投地了,這人超強。

任何命令有任何疑問都不要忘了使用這個命令,我的習慣就是這樣,任何軟件的學習首先看幫助,幫助不能解決問題就試驗一會兒,還不能解決問題那就百度一下了,當然,工作中並不是每個問題都能夠被解決的。

對get-help有任何疑問,可以get-help get-help

兩個參數:

    -full <SwitchParameter>
        顯示 cmdlet 的整個幫助文件,包括參數的相關技術信息。此參數不影響概念性
        (“About_”)幫助的顯示。

 

  -detailed <SwitchParameter>
    顯示有關 cmdlet 的其他信息,包括參數的描述和使用 cmdlet 的示例。此參數
    不影響概念性(“About_”)幫助的顯示。

get-help不僅可以是獲得某個命令的使用方法,同樣也可以作爲獲取命令列表使用,類似於get-command了,不過默認的輸出不同,例如:

PS C:/> get-help *-service

Name                                    Category                                Synopsis
----                                    --------                                --------
Get-Service                             Cmdlet                                  獲取本地計算機上的服務。
Stop-Service                            Cmdlet                                  停止一個或多個正在運行的服務。
Start-Service                           Cmdlet                                  啓動一個或多個已停止的服務。
Suspend-Service                         Cmdlet                                  掛起(暫停)一個或多個正在運行的服務。
Resume-Service                          Cmdlet                                  恢復一項或多項掛起(暫停的)服務。
Restart-Service                         Cmdlet                                  停止並接着啓動一個或更多服務。
Set-Service                             Cmdlet                                  更改服務的顯示名稱、說明或啓動模式。
New-Service                             Cmdlet                                  在註冊表和服務數據庫中爲 Windows 服...

get-command的輸出不同,因爲兩者是不同的類,get-command的輸出如下:

PS C:/> get-command *-service

CommandType     Name                                                Definition
-----------     ----                                                ----------
Cmdlet          Get-Service                                         Get-Service [[-Name] <String[]>] [-Include <Stri...
Cmdlet          New-Service                                         New-Service [-Name] <String> [-BinaryPathName] <...
Cmdlet          Restart-Service                                     Restart-Service [-Name] <String[]> [-Force] [-Pa...
Cmdlet          Resume-Service                                      Resume-Service [-Name] <String[]> [-PassThru] [-...
Cmdlet          Set-Service                                         Set-Service [-Name] <String> [-DisplayName <Stri...
Cmdlet          Start-Service                                       Start-Service [-Name] <String[]> [-PassThru] [-I...
Cmdlet          Stop-Service                                        Stop-Service [-Name] <String[]> [-Force] [-PassT...
Cmdlet          Suspend-Service                                     Suspend-Service [-Name] <String[]> [-PassThru] [...

兩者是不同的類,當然可用屬性也不同,

PS C:/> get-command|get-member -membertype property


   TypeName: System.Management.Automation.CmdletInfo

Name             MemberType Definition
----             ---------- ----------
CommandType      Property   System.Management.Automation.CommandTypes CommandType {get;}
Definition       Property   System.String Definition {get;}
HelpFile         Property   System.String HelpFile {get;}
ImplementingType Property   System.Type ImplementingType {get;}
Name             Property   System.String Name {get;}
Noun             Property   System.String Noun {get;}
ParameterSets    Property   System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Management.Automation.Comman...
PSSnapIn         Property   System.Management.Automation.PSSnapInInfo PSSnapIn {get;}
Verb             Property   System.String Verb {get;}


PS C:/> get-help|get-member -membertype noteproperty


   TypeName: System.String

Name          MemberType   Definition
----          ----------   ----------
Category      NoteProperty System.String Category=HelpFile
Component     NoteProperty System.String Component=
Functionality NoteProperty System.String Functionality=
Name          NoteProperty System.String Name=default
Role          NoteProperty System.String Role=
Synopsis      NoteProperty System.String Synopsis=顯示有關 PowerShell cmdlet 和概念的幫助。

從上可以看出,get-help顯示的是noteproperty下的屬性,而get-command顯示的是property屬性。

二、get-member

這個命令的使用有點特殊,如:

PS C:/> get-process|get-member


   TypeName: System.Diagnostics.Process

Name                           MemberType     Definition
----                           ----------     ----------
Handles                        AliasProperty  Handles = Handlecount
Name                           AliasProperty  Name = ProcessName
NPM                            AliasProperty  NPM = NonpagedSystemMemorySize
PM                             AliasProperty  PM = PagedMemorySize
VM                             AliasProperty  VM = VirtualMemorySize
WS                             AliasProperty  WS = WorkingSet

......

這個命令的使用可以讓我們再次來體驗一下PS是基於類的概念,get-process的輸出如下:

PS C:/> get-process

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    125       4    12804      14744    46            1412 audiodg
     20       1     1788       2600    23     0.08   3000 cmd
     74       3     2532       6796    62     0.23   3576 cmd
     31       2      812       3688    40     0.19   2980 conime
    509       5     1908       5736   109     2.42    508 csrss
    538       7    17596      32516   187   157.03    552 csrss
   1153      53   126108     183328   616    24.59   2568 devenv

......

根據兩個結果的比較,應該能發現問題,那就是列表是對象的輸出信息,而管道傳遞的仍然是對象,因此纔有上面列表的結果。

如果對於get-member深入的學習,可以使用get-help get-member -detailed/full,那將獲得完整的信息。

get-member輸入中的membertype大概有如下幾種:

aliasproperty

codeproperty

property

noteproperty

scriptproperty

properties

propertyset

method

codemethod

scriptmethod

methods

parameterizedproperty

memberset

all

另外需要注意的就是 .format.ps1xml文件,PS是根據這類設置文件中的內容來決定如何顯示對象類型的。

 

三、format

這是格式化輸出的命令集,有:

PS C:/> get-command format-*

CommandType     Name                            Definition
-----------     ----                            ----------
Cmdlet          Format-Custom                   Format-Custom [[-Property] <...
Cmdlet          Format-List                     Format-List [[-Property] <Ob...
Cmdlet          Format-Table                    Format-Table [[-Property] <O...
Cmdlet          Format-Wide                     Format-Wide [[-Property] <Ob...

由於第一個在primer教程上沒有,我在此也準備略過,如果要學習仍然是get-help format-custom -detailed/full.

format-wide最簡單,顯示該類對象默認的屬性輸出,也可以指定-property顯示自己想看到的結果,如:

PS C:/> get-process -name powershell|format-wide -property  path


C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe   C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe

一行顯示兩列,當然一行顯示一列也是可以實現的

PS C:/> get-process -name powershell|format-wide -property path -column 1


C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe
C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe

接下來討論format-list

典型的輸出格式如下:

PS C:/> get-process -name powershell|format-list


Id      : 2256
Handles : 601
CPU     : 6.1932397
Name    : powershell

Id      : 3232
Handles : 594
CPU     : 206.4205232
Name    : powershell

再次回顧一個get-member命令:

PS C:/> get-process|get-member -membertype property


   TypeName: System.Diagnostics.Process

Name                       MemberType Definition
----                       ---------- ----------
BasePriority               Property   System.Int32 BasePriority {get;}
Container                  Property   System.ComponentModel.IContainer Conta...
EnableRaisingEvents        Property   System.Boolean EnableRaisingEvents {ge...
ExitCode                   Property   System.Int32 ExitCode {get;}
ExitTime                   Property   System.DateTime ExitTime {get;}
Handle                     Property   System.IntPtr Handle {get;}
HandleCount                Property   System.Int32 HandleCount {get;}
HasExited                  Property   System.Boolean HasExited {get;}
Id                         Property   System.Int32 Id {get;}
MachineName                Property   System.String MachineName {get;}
MainModule                 Property   System.Diagnostics.ProcessModule MainM...
MainWindowHandle           Property   System.IntPtr MainWindowHandle {get;}
MainWindowTitle            Property   System.String MainWindowTitle {get;}
MaxWorkingSet              Property   System.IntPtr MaxWorkingSet {get;set;}
MinWorkingSet              Property   System.IntPtr MinWorkingSet {get;set;}
Modules                    Property   System.Diagnostics.ProcessModuleCollec...
NonpagedSystemMemorySize   Property   System.Int32 NonpagedSystemMemorySize ...
NonpagedSystemMemorySize64 Property   System.Int64 NonpagedSystemMemorySize6...
PagedMemorySize            Property   System.Int32 PagedMemorySize {get;}
PagedMemorySize64          Property   System.Int64 PagedMemorySize64 {get;}
PagedSystemMemorySize      Property   System.Int32 PagedSystemMemorySize {get;}
PagedSystemMemorySize64    Property   System.Int64 PagedSystemMemorySize64 {...
PeakPagedMemorySize        Property   System.Int32 PeakPagedMemorySize {get;}
PeakPagedMemorySize64      Property   System.Int64 PeakPagedMemorySize64 {get;}
PeakVirtualMemorySize      Property   System.Int32 PeakVirtualMemorySize {get;}
PeakVirtualMemorySize64    Property   System.Int64 PeakVirtualMemorySize64 {...
PeakWorkingSet             Property   System.Int32 PeakWorkingSet {get;}
PeakWorkingSet64           Property   System.Int64 PeakWorkingSet64 {get;}
PriorityBoostEnabled       Property   System.Boolean PriorityBoostEnabled {g...
PriorityClass              Property   System.Diagnostics.ProcessPriorityClas...
PrivateMemorySize          Property   System.Int32 PrivateMemorySize {get;}
PrivateMemorySize64        Property   System.Int64 PrivateMemorySize64 {get;}
PrivilegedProcessorTime    Property   System.TimeSpan PrivilegedProcessorTim...
ProcessName                Property   System.String ProcessName {get;}
ProcessorAffinity          Property   System.IntPtr ProcessorAffinity {get;s...
Responding                 Property   System.Boolean Responding {get;}
SessionId                  Property   System.Int32 SessionId {get;}
Site                       Property   System.ComponentModel.ISite Site {get;...

很多的屬性,你可以選擇其中的用於list

PS C:/> get-process -name powershell|format-list -property id,site,workingset,th
reads


Id         : 2256
Site       :
WorkingSet : 49053696
Threads    : {2276, 2504, 1120, 1468...}

Id         : 3232
Site       :
WorkingSet : 73781248
Threads    : {3812, 1240, 2148, 3680...}

如果覺得無聊,那你就這樣用:

PS C:/> get-process -id 2256|format-list -property *


__NounName                 : Process
Name                       : powershell
Handles                    : 675
VM                         : 207093760
WS                         : 49164288
PM                         : 48988160
NPM                        : 10208
Path                       : C:/Windows/System32/WindowsPowerShell/v1.0/powersh
                             ell.exe
Company                    : Microsoft Corporation
CPU                        : 6.396041
FileVersion                : 6.0.6001.18000 (longhorn_rtm.080118-1840)
ProductVersion             : 6.0.6001.18000
Description                : PowerShell.EXE
Product                    : Microsoft® Windows® Operating System
Id                         : 2256
PriorityClass              : Normal
HandleCount                : 675
WorkingSet                 : 49164288
PagedMemorySize            : 48988160
PrivateMemorySize          : 48988160
VirtualMemorySize          : 207093760
TotalProcessorTime         : 00:00:06.4116411
BasePriority               : 8
ExitCode                   :
HasExited                  : False
ExitTime                   :
Handle                     : 1100
MachineName                : .
MainWindowHandle           : 0
MainWindowTitle            :
MainModule                 : System.Diagnostics.ProcessModule (powershell.exe)
MaxWorkingSet              : 1413120
MinWorkingSet              : 204800
Modules                    : {powershell.exe, ntdll.dll, kernel32.dll, ADVAPI32
                             .dll...}
NonpagedSystemMemorySize   : 10208
NonpagedSystemMemorySize64 : 10208
PagedMemorySize64          : 48988160
PagedSystemMemorySize      : 275160
PagedSystemMemorySize64    : 275160
PeakPagedMemorySize        : 58941440
PeakPagedMemorySize64      : 58941440
PeakWorkingSet             : 58757120
PeakWorkingSet64           : 58757120
PeakVirtualMemorySize      : 226820096
PeakVirtualMemorySize64    : 226820096
PriorityBoostEnabled       : True
PrivateMemorySize64        : 48988160
PrivilegedProcessorTime    : 00:00:01.2168078
ProcessName                : powershell
ProcessorAffinity          : 3
Responding                 : True
SessionId                  : 1
StartInfo                  : System.Diagnostics.ProcessStartInfo
StartTime                  : 2008/8/30 11:35:10
SynchronizingObject        :
Threads                    : {2276, 2504, 1120, 1468...}
UserProcessorTime          : 00:00:05.2260335
VirtualMemorySize64        : 207093760
EnableRaisingEvents        : False
StandardInput              :
StandardOutput             :
StandardError              :
WorkingSet64               : 49164288
Site                       :
Container                  :

飛過一大堆東西,也不知道有些什麼玩意兒。

接下來的是fotmat-table,顧名思義,是按表格形式進行格式化,其實上述的get-process命令輸出列表就是一表格,以tab鍵爲分隔的表格,看完上面的不難想象,表格列也是可以自己進行定義的,參數還是-property,需要說明的兩個參數是-autosieze -wrap,一是自動列寬,二是折行。如果顯示的數據不多,不妨使用 -wrap參數,這樣就不會顯示...而是在以下的行顯示全部內容。

四、out

這個out不是出局的意思

PS C:/> get-command out-*

CommandType     Name                            Definition
-----------     ----                            ----------
Cmdlet          Out-Default                     Out-Default [-InputObject <P...
Cmdlet          Out-File                        Out-File [-FilePath] <String...
Cmdlet          Out-Host                        Out-Host [-Paging] [-InputOb...
Cmdlet          Out-Null                        Out-Null [-InputObject <PSOb...
Cmdlet          Out-Printer                     Out-Printer [[-Name] <String...
Cmdlet          Out-String                      Out-String [-Stream] [-Width...

這是數據顯示或者輸出到指定設備使用的命令集,查看幫助文件,順便重溫一下format-table

PS C:/> get-help out-* |format-table -autosize -wrap

Name        Category Synopsis
----        -------- --------
Out-Null    Cmdlet   刪除輸出,不將其發送到控制檯。
Out-Default Cmdlet   將輸出發送到默認的格式化程序和默認的輸出 cmdlet。此 cmdlet 對格式化或輸出無效。它是佔位符,用於編    '這是因爲顯示問題,在終端顯示的是和上一行的“將”對齊的
                     寫您自己的 Out-Default 函數或 cmdlet。
Out-Host    Cmdlet   將輸出發送到命令行。
Out-File    Cmdlet   將輸出發送到文件。
Out-Printer Cmdlet   將輸出發送到打印機。
Out-String  Cmdlet   將對象作爲一列字符串發送到主機。

需要說明的如下

out-null,如果命令存在錯誤,則仍然會輸出錯誤消息

out-file.可以通過-Encoding參數來指定字符集,如:-Encoding ASCII,out-file默認輸出的是unicode字符集。

out-printer 可以通過-name參數來指定打印機的名稱

out是最終處理,也就是out以後別指望在out以後還有對象可以處理,也就是在out後,別用管道符去做後續處理了。

out-file 由於行寬不再受console的限制,因此,在有些情況下可以-width 參數來指定輸出的列寬(max:32-bit int)。

PS C:/> get-command out-* |out-file D:/out.txt -width 2147 -encoding ASCII

其他的就不試驗了。

到這裏,基本的有關基礎使用就說完了。感覺也挺累的。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章