如何启用PowerShell Remoting之One-to-many Remoting

日期: 2012-07-04 作者:Don Jones翻译:杨旭 来源:TechTarget中国 英文

上篇文章《如何启用PowerShell Remoting 之One-to-one Remoting》中我们讲解了如何使用One-to-one remoting的方式使用PowerShell remoting。这里是PowerShell remoting使用指南的下半部分,继续讲解One-to-many remoting方式。   One-to-many Remoting   这是一项强大的技术,真正体现了远程处理的价值。你主要做的就是向远程计算机上传输一个命令(或者一些列命令)。

远程计算机会单独执行这些命令,序列化为XML并将结果反馈给你。你的PowerShell副本会将这些XML反序列化为对……

我们一直都在努力坚持原创.......请不要一声不吭,就悄悄拿走。

我原创,你原创,我们的内容世界才会更加精彩!

【所有原创内容版权均属TechTarget,欢迎大家转发分享。但未经授权,严禁任何媒体(平面媒体、网络媒体、自媒体等)以及微信公众号复制、转载、摘编或以其他方式进行使用。】

微信公众号

TechTarget微信公众号二维码

TechTarget

官方微博

TechTarget中国官方微博二维码

TechTarget中国

电子邮件地址不会被公开。 必填项已用*标注

敬请读者发表评论,本站保留删除与本文无关和不雅评论的权力。

上篇文章《如何启用PowerShell Remoting 之One-to-one Remoting》中我们讲解了如何使用One-to-one remoting的方式使用PowerShell remoting。这里是PowerShell remoting使用指南的下半部分,继续讲解One-to-many remoting方式。

  One-to-many Remoting

  这是一项强大的技术,真正体现了远程处理的价值。你主要做的就是向远程计算机上传输一个命令(或者一些列命令)。远程计算机会单独执行这些命令,序列化为XML并将结果反馈给你。你的PowerShell副本会将这些XML反序列化为对象,将其放入流水线。例如,假设我们想从两台不同的计算机上获得所有以字母“s”开头的进程的列表:

  PS C:> invoke-command -ScriptBlock { Get-Process -name s* } –computername

  localhost,win8

  Handles  NPM(K)    PM(K)      WS(K)

  VM(M)   CPU(s)     Id ProcessN PSCompu
                                                            ame      terName

  -------  ------    -----      ----- -----   ------     -- -------- -------

      217      11     3200       7080    33     1.23    496 services win8

       50       3      304        980     5     0.13    248 smss     win8

      315      16     2880       8372    46     0.03     12 spoolsv  win8

      472      36     8908      11540    60     0.31    348 svchost  win8

      306      12     2088       7428    36     0.19    600 svchost  win8

      295      15     2372       5384    29     0.61    636 svchost  win8

      380      15    17368      19428    55     0.56    728 svchost  win8

     1080      41    12740      25456   120     2.19    764 svchost  win8

      347      19     3892       8812    93     0.03    788 svchost  win8

    614      52    13820      18220  1129     2.28    924 svchost  win8

       45       4      508       2320    13     0.02   1248 svchost  win8

      211      18     9228       8408  1118     0.05   1296 svchost  win8

       71       6      804       3540    28     0.00   1728 svchost  win8

     2090       0      120        292     3    10.59      4 System   win8

      217      11     3200       7080    33     1.23    496 services loca...

       50       3      304        980     5     0.13    248 smss     loca...

      315      16     2880       8372    46     0.03     12 spoolsv  loca...

      469      36     8856      11524    59     0.31    348 svchost  loca...

      306      12     2088       7428    36     0.19    600 svchost  loca...

      295      15     2372       5384    29     0.61    636 svchost  loca...

      380      15    17368      19428    55     0.56    728 svchost  loca...

     1080      41    12740      25456   120     2.19    764 svchost  loca...

      347      19     3892       8812    93     0.03    788 svchost  loca...

      607      49    13756      18132  1129     2.28    924 svchost  loca...

       45       4      508       2320    13     0.02   1248 svchost  loca...

      211      18     9228       8408  1118     0.05   1296 svchost  loca...

       71       6      804       3540    28     0.00   1728 svchost  loca...

     2089       0      120        292     3    10.59      4 System   loca...

   这个命令是Invoke-Command。–ScriptBlock参数接收这些你想要传递给远程机器上的命令(多个命令用分号隔开);–ComputerName参数确认机器名字。另外,一个脚本块对象可以创建更长的命令:

  $sb = {Get-Process -Name s*}

  Invoke-Command -ComputerName localhost,win8 -ScriptBlock $sb

  和Enter-PSSession一样,你必须指定计算机名字,因为它会出现在Active Directory或本地受信任的主机列表上。你不能使用IP地址或DNS CNAME 别名。

  注意到输出结果有什么有趣的地方了吗?输出结果包含一个叫做PSComputerName的列,包括了每个结果行来源计算机的名称,方便分离、排序、分组并组织结果。这个属性是通过PowerShell添加到传入的结果中;如果你不想在输出结果中看到这个属性,就将–HideComputerName参数添加到Invoke-Command中。这个属性仍然存在(可以用来排序等等),但是默认不在输出结果中显示。

  同Enter-PSSession一样,Invoke-Command会用到远程机器上的默认PowerShell终端,在64位操作系统上,将是64位版本的PowerShell

  默认的,Invoke-Command只能一次对话32个计算机。这样做需要它来保持与之对话的每台远程计算机将PowerShell实例放在内存中;32个是微软努力追赶的数字,并且在各种情况下能够良好运行。如果你指定的计算机多于32个,那么多出来的只能排队等待,并且Invoke-Command完成头32个以后才开始工作。你可以使用命令的–ThrottleLimit参数来改变并行数量,记住,数量越大,你的电脑上的工作负载就越大,而远程计算机上没有额外的负载。

  远程处理说明

  从远程计算机发送到你的电脑上的数据是打包过的,以便于在网络中传输。我们曾提到过的序列化和反序列化可以完成这个工作——但是会损失一些功能。例如,Get-Service产生的对象的类型:

  PS C:> get-service | get-member

     TypeName: System.ServiceProcess.ServiceController

  Name                      MemberType    Definition

  ----                      ----------    ----------

  Name                      AliasProperty Name = ServiceName

  RequiredServices          AliasProperty RequiredServices = ServicesDepe...

  Disposed                  Event         System.EventHandler Disposed(Sy...

  Close                     Method        System.Void Close()

  Continue                  Method        System.Void Continue()

  CreateObjRef              Method        System.Runtime.Remoting.ObjRef ...

  Dispose                   Method        System.Void Dispose()

  Equals                    Method        bool Equals(System.Object obj)

  ExecuteCommand            Method        System.Void ExecuteCommand(int ...

  GetHashCode               Method        int GetHashCode()

  GetLifetimeService        Method        System.Object GetLifetimeService()

  GetType                   Method        type GetType()

  InitializeLifetimeService Method        System.Object InitializeLifetim...

  Pause                     Method        System.Void Pause()

  Refresh                   Method        System.Void Refresh()

  Start                     Method        System.Void Start(), System.Voi...

  Stop                      Method        System.Void Stop()

  WaitForStatus             Method        System.Void WaitForStatus(Syste...

  CanPauseAndContinue       Property      bool CanPauseAndContinue {get;}

  CanShutdown               Property      bool CanShutdown {get;}

  CanStop                   Property      bool CanStop {get;}

  Container                 Property      System.ComponentModel.IContaine...

  DependentServices         Property      System.ServiceProcess.ServiceCo...

  DisplayName               Property      string DisplayName {get;set;}

  MachineName               Property      string MachineName {get;set;}

  ServiceHandle             Property      System.Runtime.InteropServices....

  ServiceName               Property      string ServiceName {get;set;}

  ServicesDependedOn        Property      System.ServiceProcess.ServiceCo...

  ServiceType               Property      System.ServiceProcess.ServiceTy...

  Site                      Property      System.ComponentModel.ISite Sit...

  Status                    Property      System.ServiceProcess.ServiceCo...

  ToString                  ScriptMethod  System.Object ToString();

  正如你看到的,这些对象的成员包含一些属性,可以让你停止或者暂停等等。现在,试想通过远程处理恢复来自远程机器上的相同类型的对象:

  PS C:> invoke-command -ComputerName win8 -ScriptBlock { Get-Service } |

  >> Get-Member

  >>

   TypeName: Deserialized.System.ServiceProcess.ServiceController
 
  Name                MemberType   Definition

  ----                ----------   ----------

  ToString            Method       string ToString(), string ToString(str...

  Name                NoteProperty System.String Name=AeLookupSvc

  PSComputerName      NoteProperty System.String PSComputerName=win8

  PSShowComputerName  NoteProperty System.Boolean PSShowComputerName=True

  RequiredServices    NoteProperty Deserialized.System.ServiceProcess.Ser...

  RunspaceId          NoteProperty System.Guid RunspaceId=00e784f7-6c27-4...

  CanPauseAndContinue Property     System.Boolean {get;set;}

  CanShutdown         Property     System.Boolean {get;set;}

  CanStop             Property     System.Boolean {get;set;}

  Container           Property      {get;set;}

  DependentServices   Property     Deserialized.System.ServiceProcess.Ser...

  DisplayName         Property     System.String {get;set;}

  MachineName         Property     System.String {get;set;}

  ServiceHandle       Property     System.String {get;set;}

  ServiceName         Property     System.String {get;set;}

  ServicesDependedOn  Property     Deserialized.System.ServiceProcess.Ser...

  ServiceType         Property     System.String {get;set;}

  Site                Property      {get;set;}

  Status              Property     System.String {get;set;}

  方法(除了通用的ToString()方法)不见了,这是因为你正在关注一个反序列化类型的对象(就在输出顶部的TypeName里)。本质上,你得到一个静态版本的只读对象。

  这并不一定是,因为序列化和方法的移除不会发生,直到远程命令结束执行,并且输出是打包的。这个对象在远程计算机上仍然是“活的”,所以你只需要简单地在远程计算机上启动、停止、暂停等等。换句话说,你需要发起的任何“行为”需要变成发送至远程计算机执行命令的一部分。

作者

Don Jones
Don Jones

投稿作者

相关推荐