get-service | get-member
使用这条命令就可以查看"get-service”t的属性和方法。在这个例子中,我们使用管道符来进行命令的传递。运行结果如下:
[url=http://files.jb51.net/file_images/article/201212/2012120515083320.png][img]http://files.jb51.net/file_images/article/201212/2012120515083320.png[/img]
[/url]
当然,我们可以使用"get-member"的参数来查看"get-service"的所有的属性类对象,或者方法类对象。
例如:
查看"get-service"的所有属性类对象
Get-Service | Get-Member -MemberType Property<enter>
查看get-service"的所有方法类对象
Get-Service | Get-Member -MemberType Method<enter>
为什么我们如此的强调对象?原因就是在PowerShell中,所有的一切都是对象。
例如:
我们要找出指定日期写入d:\的文件有哪些,使用如下命令:
Get-ChildItem -Path d:\ -Recurse | Where-Object {$_.LastWriteTime -gt "01/01/2010"}<enter>
现在来解释一下:
首先,"get-childitem"是用来枚举我们的文件系统的,使用"-path"参数,将路径指向"d:\",使用"-recurse"参数,意味着将显示所有的文件,甚至是子目录下的。接下来,我们将结果使用管道符传递给循环声明"where-object"中,用来筛选出符合条件的结果。
那么,"lastwritetime"又是什么?
我们使用如下命令看看"get-childitem"都有哪些属性可供我们筛选:
get-chileitem | get-member可以发现其中一条: [url=http://files.jb51.net/file_images/article/201212/2012120515083321.png][img]http://files.jb51.net/file_images/article/201212/2012120515083321.png[/img] [/url] 对,就是这个。我们需要筛选出的对象属性就是最后写日期。在后面的定义中可以看到"LastWriteTime"会将一个"Syetem.DateTime"数据类型作为反馈。因此,在整个语句的后半部,我们使用了"-gt"进行进一步的筛选,"-gt"是"greater than"的缩写,意味“大于”。在以后的教程中我将会介绍更多类似这样的操作。前面说到,"LastWriteTime"是一个"Syetem.DateTime"类型的数据,因此,我们最终使用类似"01/01/2010"这样的表达。这一点需要大家多加注意,在以后的运用中需要注意数据类型。 后续的教程中,我还会尽可能全面的介绍WMI、COM以及 .NET,不过,我们现在知道并掌握上面的就足够了。 [b]PowerShell的格式[/b] 在这一小节,我将介绍PowerShell中的格式化输出。当我们使用一个cmdlet时,参数"format-"允许我们选择一种习惯的输出模式。使用以下命令试一试:
Get-Command Format-* <enter>其结果为: [url=http://files.jb51.net/file_images/article/201212/2012120515083322.png][img]http://files.jb51.net/file_images/article/201212/2012120515083322.png[/img] [/url] 好了,这一个知识点很简单。请各位童鞋使用如下命令试一试,结果怎么样看看就知道了。
get-childitem c:\windows | format-table <enter>get-childitem c:\windows | format-table -autosize <enter>get-childitem c:\windows | format-custom <enter>get-childitem c:\windows | format-list <enter>get-childitem c:\windows | format-list -Property FullName <enter>get-childitem c:\windows | format-wide <enter>当然,复杂些的还有以下这些,我不想解释过多,大家只要肯亲自动手试一试,一眼就能看明白。
Get-ChildItem C:\Windows -Recurse | Format-List -Property FullName,CreationTime,LastWriteTime<enter> Get-ChildItem C: | Format-Wide -Column 3<enter>另外,在其他cmdlet中,存在其他格式的输出。例如,在"get-process"中就有"group-object","Get-EventLog"中我们可能用到"Sort-Object",甚至,我们可以输出为特定格式的文件,例如使用"Convertto-HTML"输出为html,使用"Export-CSV"输出为表格文件(可以使用Excel打开)。 统统举例如下(记住管道符):
Get-Process | Group-Object Company<enter> Get-EventLog System | Group-Object eventid<enter> Get-EventLog System | Group-Object eventid | Sort-Object Count -descending<enter> Get-Process | ConvertTo-html<enter> Get-Process | ConvertTo-html | out-file “Processes.html”<enter> Get-Process | Export-CSV Processes.csv<enter>至于打开文件,使用如下命令即可:
Invoke-Item Processes.html<enter> Invoke-Item Processes.csv<enter>看看截图吧(输出为".CSV"文件): [url=http://files.jb51.net/file_images/article/201212/2012120515083323.png][img]http://files.jb51.net/file_images/article/201212/2012120515083323.png[/img] [/url] 使用"Invoke-Item"命令打开:
Invoke-Item Processes.csv <Enter>
[url=http://files.jb51.net/file_images/article/201212/2012120515083324.png][img]http://files.jb51.net/file_images/article/201212/2012120515083324.png[/img]
[/url]
使用PowerShell的格式化输出是不是很简单呢?个人认为比VBScript要更加容易上手一些。管理系统更加方便!
[b]PowerShell的常见参数[/b]
我们前面介绍过,为了简化我们的记忆,PowerShell对cmdlet使用了全新的"动词-名词"的命名方式,更加方便的是,几乎所有的cmdlet都拥有统一的标准化参数,当然,我说了,几乎所有的——并非全部。下面这个列表,列举出了“公共参数”(这些参数的名称是我们无法自定义使用的):
get-service -<Tab>
或者使用帮助命令"get-help":
get-help get-service -full <Enter>
好了,我们做一些简单的演示吧:
[b]Set-[/b]ExecutionPolicy Unrestricted -whatif <enter>
[url=http://files.jb51.net/file_images/article/201212/2012120515083325.png][img]http://files.jb51.net/file_images/article/201212/2012120515083325.png[/img]
[/url]
是不是很方便?在执行cmdlet之前,"-whatif"就会告诉你接下来会发生什么。
那么如下这条cmdlet呢:
[b]Set-[/b]ExecutionPolicy Unrestricted -confirm <enter>
[url=http://files.jb51.net/file_images/article/201212/2012120515083326.png][img]http://files.jb51.net/file_images/article/201212/2012120515083326.png[/img]
[/url]
是的,它将返回一条验证操作,以获取用户的进一步许可。只是"Y"、"A"、"N"、"L"与"?"我们都能轻易理解,那么"S"呢?
请注意观察以下执行结果:
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”).执行实例截图: [url=http://files.jb51.net/file_images/article/201212/2012120515083327.png][img]http://files.jb51.net/file_images/article/201212/2012120515083327.png[/img] [/url] 聪明的你,明白了么?
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有