使用这条命令就可以查看"get-service”t的属性和方法。在这个例子中,我们使用管道符来进行命令的传递。运行结果如下:复制代码
- get-service | get-member
PS D:\> get-service | get-member TypeName: System.ServiceProcess.ServiceController Name MemberType Definition ---- ---------- ---------- Name AliasProperty Name = ServiceName RequiredServices AliasProperty RequiredServices = ServicesDependedOn Disposed Event System.EventHandler Disposed(System.Object, System.EventArgs) ... |
查看get-service"的所有方法类对象复制代码
- Get-Service | Get-Member -MemberType Property
为什么我们如此的强调对象?原因就是在PowerShell中,所有的一切都是对象。复制代码
- Get-Service | Get-Member -MemberType Method
现在来解释一下:复制代码
- Get-ChildItem -Path d:\ -Recurse | Where-Object {$_.LastWriteTime -gt "01/01/2010"}
复制代码
- get-childitem | get-member
... LastAccessTime Property System.DateTime LastAccessTime {get;set;} LastAccessTimeUtc Property System.DateTime LastAccessTimeUtc {get;set;} LastWriteTime Property System.DateTime LastWriteTime {get;set;} LastWriteTimeUtc Property System.DateTime LastWriteTimeUtc {get;set;} ... |
其结果为:复制代码
- Get-Command Format-*
PS D:\> Get-Command Format-* CommandType Name Definition ----------- ---- ---------- Cmdlet Format-Custom Format-Custom [[-Property] <Object[]>] [-Depth <... Cmdlet Format-List Format-List [[-Property] <Object[]>] [-GroupBy <... Cmdlet Format-Table Format-Table [[-Property] <Object[]>] [-AutoSize... Cmdlet Format-Wide Format-Wide [[-Property] <Object>] [-AutoSize] [... |
复制代码
- get-childitem c:\windows | format-table
复制代码
- get-childitem c:\windows | format-table -autosize
复制代码
- get-childitem c:\windows | format-custom
复制代码
- get-childitem c:\windows | format-list
复制代码
- get-childitem c:\windows | format-list -Property FullName
当然,复杂些的还有以下这些,我不想解释过多,大家只要肯亲自动手试一试,一眼就能看明白。复制代码
- get-childitem c:\windows | format-wide
复制代码
- Get-ChildItem C:\Windows -Recurse | Format-List -Property FullName,CreationTime,LastWriteTime
另外,在其他cmdlet中,存在其他格式的输出。例如,在"get-process"中就有"group-object","Get-EventLog"中我们可能用到"Sort-Object",甚至,我们可以输出为特定格式的文件,例如使用"Convertto-HTML"输出为html,使用"Export-CSV"输出为表格文件(可以使用Excel打开)。复制代码
- Get-ChildItem C: | Format-Wide -Column 3
复制代码
- Get-Process | Group-Object Company
复制代码
- Get-EventLog System | Group-Object eventid
复制代码
- Get-EventLog System | Group-Object eventid | Sort-Object Count -descending
复制代码
- Get-Process | ConvertTo-html
复制代码
- Get-Process | ConvertTo-html | out-file "Processes.html"
至于打开文件,使用如下命令即可:复制代码
- Get-Process | Export-CSV Processes.csv
复制代码
- Invoke-Item Processes.html
看看截图吧(输出为".CSV"文件):复制代码
- Invoke-Item Processes.csv
PS D:\BatHome> get-process | Export-CSV Processes.csv PS D:\BatHome> ls Directory: D:\BatHome Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 8/30/2013 3:03 PM 0 bathome.txt -a--- 8/30/2013 3:03 PM 0 bbs.txt -a--- 8/30/2013 3:04 PM 0 net.txt -a--- 8/30/2013 5:09 PM 37510 Processes.csv |
使用PowerShell的格式化输出是不是很简单呢?个人认为比VBScript要更加容易上手一些。管理系统更加方便!复制代码
- Invoke-Item Processes.csv
或者使用帮助命令"get-help":复制代码
- get-service -<Tab>
好了,我们做一些简单的演示吧:复制代码
- get-help get-service -full
复制代码
- Set-ExecutionPolicy Unrestricted -whatif
PS D:\> Set-ExecutionPolicy Unrestricted -whatif What if: Performing operation "Set-ExecutionPolicy" on Target "Unrestricted". |
复制代码
- Set-ExecutionPolicy Unrestricted -confirm
PS D:\> Set-ExecutionPolicy Unrestricted -confirm Confirm Are you sure you want to perform this action? Performing operation "Set-ExecutionPolicy" on Target "Unrestricted". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): |
Set-ExecutionPolicy Unrestricted -confirm<enter>
Are you sure you want…
S<enter> (places the prompt in suspend mode as denoted by “>>”).
>>Get-ExecutionPolicy<enter>
Resricted (or whatever the policy is set to).
>>exit<enter> (Typing “exit” leaves suspend mode and returns to the original command)
Are you sure you want…
Y<enter> (Confirms “Yes” and sets the ExecutionPolicy to “Unrestricted”).
PS D:\> Set-ExecutionPolicy Unrestricted -confirm Confirm Are you sure you want to perform this action? Performing operation "Set-ExecutionPolicy" on Target "Unrestricted". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): s PS D:\>>> Get-ExecutionPolicy Unrestricted PS D:\>>> exit Confirm Are you sure you want to perform this action? Performing operation "Set-ExecutionPolicy" on Target "Unrestricted". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): |
欢迎光临 批处理之家 (http://bbs.bathome.net/) | Powered by Discuz! 7.2 |