我如何举办一个PowerShell脚本或应用程序,所以它通过WSManConnectionInfo的访问? (如Office 365) [英] How do I host a Powershell script or app so it's accessible via WSManConnectionInfo? (like Office 365)

查看:1507
本文介绍了我如何举办一个PowerShell脚本或应用程序,所以它通过WSManConnectionInfo的访问? (如Office 365)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道的唯一的方式连接到远程运行空间包括下面的参数

  WSManConnectionInfo connectionInfo = 
新WSManConnectionInfo(假的,本地主机,80/ PowerShell的,http://schemas.microsoft.com/powershell/Microsoft.Exchange,证书);

  WSManConnectionInfo connectionInfo = 
新WSManConnectionInfo(假,本地主机,5985,/ WSMAN,http://schemas.microsoft.com/powershell/Microsoft.P​​owershell,证书);




  • 我如何建立自己的自定义PowerShell的对象,以便我可以通过HTTP公开呢?


  • 什么是使用正确的参数,如何设置他们?



解决方案

有几件这一点,所以我将分开来解释他们,然后把他们聚在一起。



隐式远程处理



交易所使用的隐式远程处理



它的工作原理是,你建立一个PSSession可在远程计算机的方式,然后导入一些命令可以从远程实例到你自己的。



这是通过使用导入模块-Session $会议进口的PSSession



您可以在PowerShell中纯粹试试这个自己。使用不具备安装了Active Directory RSAT工作站(不具备ActiveDirectory的PowerShell命令),然后连接到一台机器,它(我们称之为 DC1 ):



<预类=郎PowerShell的prettyprint-覆盖> $ S =新的PSSession -ComputerName DC1
调用命令-Session $小号-ScriptBlock {导入模块的ActiveDirectory}
进口的PSSession -Session $ S - 模块的ActiveDirectory

限制调用进口的PSSession 来只是一个模块,您可以只导入这些cmdlet。在这一点上,你将能够执行 GET-ADComputer 为例,就好像它是本地可用,即使实际呼叫正在对完成DC1



会话配置



当你犯了一个PowerShell远程连接,要连接到会话配置。如果没有指定,连接到一个叫 Microsoft.P​​owerShell 。要查看所有正在一台机器上定义的配置时,请拨打 GET-PSSessionConfiguration 。您可能会看到一些人,例如 Microsoft.P​​owerShell32 是连接到一个32位PowerShell会话的方式。



要连接到特定的配置,使用新的PSSession -ConfigurationName 新的PSSession -ConnectionUri



定义会话配置



您可以指定的很多;该版本的PowerShell的位数,哪些模块是预先进口的,你可以预先定义的功能和代码,你可以的防止可等的语言功能。



这个答案提供了如何创建自己的配置一个很好的概述。



中的一个组件,它会为你正在试图做什么工作良好的内部

您也可以把配置信息。



封装代码模块



如您所见与进口的PSSession 更容易导入只是你想,如果它在一个模块中存在的代码。因此,你应该确保你的小命令是通过一个模块暴露出来。



您在你想写在C#中的cmdlet的评论说。这不是我做的,但这篇文章似乎提供的如何在C#创建一个PowerShell模块。



把它一起



的步骤应大致类似这样的:




  1. 编译模块,并使其可在远端,因此,它可以导入从计算机上的本地会话的PowerShell。

  2. 创建一个新的PSSession配置文件,并指定 -AssembliesToLoad -ModulesToImport (或两者如有必要),或的指定大会本身(这里可能是首选)的配置信息。

  3. 注册在机器上的配置。

  4. 在客户端,你想让它提供给PowerShell的,所以你会简单地创建会话,然后将其导入:



<预类=郎PowerShell的prettyprint-覆盖> $ S =新的PSSession -ComputerName RemoteMachine -ConfigurationName的myconfig
#配置以这样的定义这样
#,你的模块已在远程会话被导入。
进口的PSSession - 模块MyModule的



简化呢?



您不要的必须的创建在远程端自定义配置。只要你的模块可在远程计算机上的任何PowerShell会话,你可以跳过会话配置步骤,然后你会只是做:



<预类=朗PowerShell的prettyprint-覆盖> $ S =新的PSSession -ComputerName RemoteMachine
调用命令-Session $ S -ScriptBlock {导入模块MyModule的}
进口的PSSession -Session $小号 - 模块MyModule的

不过,您可能希望其他自定义并控制你必须使用一个会话配置,所以这是给你。这就是交流怎么做的,但它可能是矫枉过正的目的。


The only ways I know to connect to a remote runspace include the following parameters

   WSManConnectionInfo connectionInfo = 
     new WSManConnectionInfo(false, "localhost", 80, "/Powershell", "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);

or

   WSManConnectionInfo connectionInfo = 
     new WSManConnectionInfo(false, "localhost", 5985, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.Powershell", credential);

  • How do I set up my own custom Powershell object so I can expose it through HTTP?

  • What are the correct parameters to use and how do I set them up?

解决方案

There are several pieces to this, so I'll explain them separately and then bring them together.

Implicit Remoting

Exchange is using Implicit Remoting.

The way it works is that you establish a PSSession to a remote machine, then import some of the commands available from the remote instance into your own.

This is done using Import-Module -Session $session or Import-PSSession.

You can try this for yourself purely in Powershell. Use a workstation that does not have the Active Directory RSAT installed (doesn't have the ActiveDirectory powershell cmdlets), then connect to a machine that does (let's call it DC1):

$s = New-PSSession -ComputerName DC1
Invoke-Command -Session $s -ScriptBlock { Import-Module ActiveDirectory }
Import-PSSession -Session $s -Module ActiveDirectory

Restricting the call to Import-PSSession to just the one module lets you import just those cmdlets. At this point you would be able to execute Get-ADComputer for example, as though it were available locally, even though the actual call is being done on DC1.

Session Configurations

When you make a powershell remoting connection, you are connecting to a session configuration. If you don't specify one, you connect to one called Microsoft.PowerShell. To see all of the configurations that are defined on a machine, call Get-PSSessionConfiguration. You might see some others, for example Microsoft.PowerShell32 is a way to connect to a 32 bit powershell session.

To connect to a specific configuration, use New-PSSession -ConfigurationName or New-PSSession -ConnectionUri.

Defining Session Configurations

You can specify a lot of stuff in a session configuration; the version of powershell, the bitness, which modules are pre-imported, you can pre-define functions and code, you can prevent language features from being available, etc.

This answer provides a good overview of how to create your own configuration.

You can also put configuration information inside of an assembly, which would work well for what you're trying to do.

Wrapping Code in Modules

As you've seen with Import-PSSession it's easier to import just the code you want if it exists in a module. Therefore you should make sure that your cmdlet is exposed through a module.

You said in a comment that you wanted to write your cmdlet in C#. This is not something I've done, but this article appears to provide detailed instructions on how to create a PowerShell Module in C#.

Bringing it Together

The steps should roughly resemble this:

  1. Compile module and make it available on the remote end, so that it can be imported to powershell from a local session on that machine.
  2. Create a new PSSession configuration file, and specify either -AssembliesToLoad or -ModulesToImport (or both if necessary), or specify the configuration information in the assembly itself (probably preferred here).
  3. Register the configuration on the machine.
  4. On the client side, you wanted to make it available to PowerShell, so you would simply create the session, then import it:

    $s = New-PSSession -ComputerName RemoteMachine -ConfigurationName MyConfig
    # The configuration was defined in such a way 
    # that your module will already be imported in the remote session.
    Import-PSSession -Module MyModule

Simplifying it?

You don't have to create a custom configuration on the remote side. As long as your module is available to any powershell session on the remote machine, you can skip the session configuration steps, and then you would just do:

$s = New-PSSession -ComputerName RemoteMachine
Invoke-Command -Session $s -ScriptBlock { Import-Module MyModule }
Import-PSSession -Session $s -Module MyModule

But you may want the additional customization and control you have by using a session configuration, so that's up to you. That's how exchange does it, but it may be overkill for your purposes.

这篇关于我如何举办一个PowerShell脚本或应用程序,所以它通过WSManConnectionInfo的访问? (如Office 365)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆