打印

简介Windows PowerShell

谢谢   我也刚接触,想看看和shell的异同。

TOP

[quote:ea1b359e33="seamonkey"]你能不能不要来说风凉话了,我辛辛苦苦的帖子,不要水了,有的是帖子给你水。[/quote]
不是风凉话啊,是为了解释的更清楚一点 ...

[quote:ea1b359e33="duotaiya"]加个精吧,因为我看不懂,也不知道这么强大的东西有什么用。 [/quote]
使得 windows 也可以 init 3 的东东哦


       高举马列主义毛泽东思想 !!!

TOP

看样子,MS貌似要去返祖了~~~

  
快乐的学习,快乐的生活~

TOP

使用cmdlet

cmdlet(读作:"command-let"),是简单的,单一功能的内建于shell中的命令行工具。使用cmdlet就同时用传统命令一样,在PowerShell命令提示符下输入cmdlet的名字,Windows PowerShell命令是大小写不敏感的。

输入Get-Date cmdlet:

[code:1]PS D:\> get-date

2006年10月13日 13:53:57[/code:1]

要列出所有当前会话可用的cmdlet,使用Get-Command cmdlet而不加任何参数。

[code:1]PS D:\> get-command

CommandType     Name                                                Definition
-----------     ----                                                ----------
Cmdlet          Add-Content                                         Add-Content [-Path] <String[]> [-Value] <Object[...
Cmdlet          Add-History                                         Add-History [[-InputObject] <PSObject[]>] [-Pass...
Cmdlet          Add-Member                                          Add-Member [-MemberType] <PSMemberTypes> [-Name]...
Cmdlet          Add-PSSnapin                                        Add-PSSnapin [-Name] <String[]> [-PassThru] [-Ve...
Cmdlet          Clear-Content                                       Clear-Content [-Path] <String[]> [-Filter <Strin...
…[/code:1]

Get-Command的输出分为三列:命令类型,名字,和定义。当列出cmdlet时,定义一栏显示cmdlet的语法。(…)表明数据被缩略了。

Get-Command cmdlet还可以获取除cmdlet之外的命令和命令成分,包括别名,函数,PowerShell可用的可执行文件。

下面的命令通过Get-Command的Name参数列出了可用的可执行文件:

[code:1]PS D:\> get-command *.exe

CommandType     Name                                                Definition
-----------     ----                                                ----------
Application     accwiz.exe                                          C:\WINDOWS\system32\accwiz.exe
Application     acldiag.exe                                         C:\Program Files\Support Tools\acldiag.exe
…[/code:1]

当列出可执行文件的时候,定义栏包含了可执行文件的完整路径。

接着尝试一下其他cmdlet,比如Get-Process, Get-Services, Get-EventLog, 和Get-Alias.

当你熟悉了简单的"Get-" cmdlet后,不妨来点更有趣的,比如Get-WimObject。这一cmdlet特别有用,它能让你查看和改变远程计算机的组件。比如下面的命令可获取名为"Server01"的远程计算机的BIOS信息:

[code:1]Get-wmiobject win32_bios -computername server01[/code:1]

别忘了,需要某cmdlet的帮助时,输入:

[code:1]Get-help <cmdlet-name> -detailed[/code:1]

TOP

不是返祖,是互相学习。

PowerShell的确有其先进之处,请不要先说不着边际的话,静心看看它的细节。

TOP

不同shell自有用武之地,*NIX的配置文件都是人可读的文本文件,所以基于文本流的ksh, bash, zsh等加上sed, awk这样的文本处理工具特别有用。

但Windows不是这样,移植一个bash并不能解决问题。

TOP

说明,这些文字基本是随PowerShell分发的Getting Started的翻译。

TOP

了解对象:Get-Member

Get-Member是最有用的cmdlet之一,它显示命令返回的.NET对象的信息,包括对象的类型,属性和方法。

使用管道符将命令的结果传送给Get-Member,比如:

[code:1]Get-service | get-member[/code:1]

Get-Service实际了返回了一组Syste.SericeProcess.ServiceController对象,每一个都是对应系统得一个服务。

[code:1]   TypeName: System.ServiceProcess.ServiceController

Name                      MemberType    Definition
----                      ----------    ----------
Name                      AliasProperty Name = ServiceName
add_Disposed              Method        System.Void add_Disposed(EventHandler value)
Close                     Method        System.Void Close()
Continue                  Method        System.Void Continue()
CreateObjRef              Method        System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)
Dispose                   Method        System.Void Dispose()
Equals                    Method        System.Boolean Equals(Object obj)
ExecuteCommand            Method        System.Void ExecuteCommand(Int32 command)
GetHashCode               Method        System.Int32 GetHashCode()
GetLifetimeService        Method        System.Object GetLifetimeService()
GetType                   Method        System.Type GetType()
get_CanPauseAndContinue   Method        System.Boolean get_CanPauseAndContinue()
get_CanShutdown           Method        System.Boolean get_CanShutdown()
get_CanStop               Method        System.Boolean get_CanStop()
get_Container             Method        System.ComponentModel.IContainer get_Container()
get_DependentServices     Method        System.ServiceProcess.ServiceController[] get_DependentServices()
get_DisplayName           Method        System.String get_DisplayName()
get_MachineName           Method        System.String get_MachineName()
get_ServiceHandle         Method        System.Runtime.InteropServices.SafeHandle get_ServiceHandle()
get_ServiceName           Method        System.String get_ServiceName()
get_ServicesDependedOn    Method        System.ServiceProcess.ServiceController[] get_ServicesDependedOn()
get_ServiceType           Method        System.ServiceProcess.ServiceType get_ServiceType()
get_Site                  Method        System.ComponentModel.ISite get_Site()
get_Status                Method        System.ServiceProcess.ServiceControllerStatus get_Status()
InitializeLifetimeService Method        System.Object InitializeLifetimeService()
Pause                     Method        System.Void Pause()
Refresh                   Method        System.Void Refresh()
remove_Disposed           Method        System.Void remove_Disposed(EventHandler value)
set_DisplayName           Method        System.Void set_DisplayName(String value)
set_MachineName           Method        System.Void set_MachineName(String value)
set_ServiceName           Method        System.Void set_ServiceName(String value)
set_Site                  Method        System.Void set_Site(ISite value)
Start                     Method        System.Void Start(), System.Void Start(String[] args)
Stop                      Method        System.Void Stop()
ToString                  Method        System.String ToString()
WaitForStatus             Method        System.Void WaitForStatus(ServiceControllerStatus desiredStatus), System.Voi...
CanPauseAndContinue       Property      System.Boolean CanPauseAndContinue {get;}
CanShutdown               Property      System.Boolean CanShutdown {get;}
CanStop                   Property      System.Boolean CanStop {get;}
Container                 Property      System.ComponentModel.IContainer Container {get;}
DependentServices         Property      System.ServiceProcess.ServiceController[] DependentServices {get;}
DisplayName               Property      System.String DisplayName {get;set;}
MachineName               Property      System.String MachineName {get;set;}
ServiceHandle             Property      System.Runtime.InteropServices.SafeHandle ServiceHandle {get;}
ServiceName               Property      System.String ServiceName {get;set;}
ServicesDependedOn        Property      System.ServiceProcess.ServiceController[] ServicesDependedOn {get;}
ServiceType               Property      System.ServiceProcess.ServiceType ServiceType {get;}
Site                      Property      System.ComponentModel.ISite Site {get;set;}
Status                    Property      System.ServiceProcess.ServiceControllerStatus Status {get;}[/code:1]

上面的信息看上去很技术化,其实是非常实用的。

类型名(typename, 如:"System.ServicesProcess.ServicesController")告诉你cmdlet返回的是何种类型的.NET对象。欲获取此.NET类中对象的信息,复制类型名到MSDN搜索。MSDN相关文档包含了该类中对象属性和方法的信息。

属性类型(Property types),代表对象的属性,每个属性的值都是服务对象的信息。比如,ServiceController对象具有CanPauseAndContinue属性。MSDN对此属性的解释为该服务是否可以被暂停和继续。

要列出某个特定服务的属性,输入:

[code:1](get-service <service-name>).<property-name>[/code:1]

比如:

[code:1](get-service alerter).canpauseandcontinue[/code:1]

以列表方式显示Alerter服务CanPauseAndContinue属性的名称和服务,输入:

[code:1]get-service alerter | format-list -property name, CanPauseAndContinue[/code:1]

以列表方式显示Alerter服务的所有属性的值:

[code:1]get-service alerter | format-list -property *[/code:1]

以表格形式显示所有服务CanPauseAndContinue属性的名称和值:

[code:1]get-service | format-table -property name, CanPauseAndContinue[/code:1]

方法类型(Method types)代表对象的方法,即你可以对该对象施加的行为。比如:ServiceController对象有Stop方法,可以让你停止服务。

要调用服务对象的行为,用以下格式,(务必包括括号):

[code:1](get-service <service-name>).<method-name>()[/code:1]

比如

[code:1](get-service schedule).stop()[/code:1]
停止schedule服务。

TOP

也许是习惯了UNIX的shell,感觉PowerShell有点太复杂。

TOP

是,还是个习惯问题。但有一点,cygwin bash做不了windows系统管理,cmd.exe交互太差,wsh/vbs看见了就心烦。

TOP

又臭又长的命令

TOP

不过还有一个PowerShell:  http://powershell.sourceforge.net/
引用:
PowerShell is a terminal emulator for the X11 Window System. If you end up having so many xterms open that you can never find the one you're looking for, PowerShell is for you. It supports many terminal "windows" embedded into a single X11 window, with each one given its own "notebook" tab.

TOP

对啊,那个家伙在他blog上兴奋地说起过这件事。

TOP

http://mspong.com/2006/04/28/i-am-way-too-amused-by-this/

找到了

TOP

去安装一下试试看。

TOP