您的位置:首页 > 运维架构 > Shell

学习windows powershell(基本信息获得和显示)

2008-08-30 13:54 302 查看
一、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

其他的就不试验了。

到这里,基本的有关基础使用就说完了。感觉也挺累的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: