1

我正在研究 PowerShell 管理单元,我计划将它作为 WSMan 模块的插件托管在 IIS 上。我想知道如何将附加参数从 web.config 传递到管理单元?

这里详细介绍:

托管 PS 管理单元的应用程序的 Web.config 文件:

<system.webServer>
    <system.management.wsmanagement.config>
      <PluginModules>
        <OperationsPlugins>
          <Plugin Name="MyPSPlugin" Filename="%windir%\system32\pwrshplugin.dll" SDKVersion="1" XmlRenderingType="text">
            <InitializationParameters>

              <!-- I'd like to declare additional parameter for PS span-in here something like this: -->
              <Param Name="myData" Value="Test" />

              <Param Name="PSVersion" Value="2.0" />
              <Param Name="assemblyname" Value="C:\MyServices\PowerShell\Bin\MyPSSnapin.dll" />
              <Param Name="pssessionconfigurationtypename" Value="MyCompany.PowerShell.MyPSSessionConfiguration" />
            </InitializationParameters>
            <Resources>
              <Resource ResourceUri="http://schemas.microsoft.com/powershell/Hosting.PowerShell" SupportsOptions="true">
                <Capability Type="Shell" />
              </Resource>
            </Resources>
          </Plugin>
        </OperationsPlugins>
      </PluginModules>
    </system.management.wsmanagement.config>
</system.webServer>

这里 PSSessionConfiguration 的实现:

namespace MyCompany.PowerShell
{
    public class MyPSSessionConfiguration : PSSessionConfiguration
    {    
        public override InitialSessionState GetInitialSessionState(PSSenderInfo senderInfo)
        {
            // read additional parameter something like this:
            var myData = sendrerInfo.ApplicationArguments["myData"];

            return base.GetInitialSessionState(senderInfo);
        }
    }
}
4

1 回答 1

0

我不熟悉 powershell 插件开发,但在 Web 开发中,我添加了对 System.Configuration 的引用,然后使用 ConfigurationManager 将 web.config 的 appsettings 部分的设置读入具有只读属性的静态类。这可以通过缓存值来防止重复读取 web.config。你能应用同样的技术吗?

网络配置

<?xml version="1.0"?>
<configuration>
        <appSettings>
            <add key="MyData" value="Test"/>
        </appSettings>
</configuration>

静态类

using System;
using System.Configuration;
namespace WebTest
{
    public static class CachedSettings
    {
        public static string MyData;
        public static CachedSettings()
        {
            MyData = ConfigurationManager.AppSettings["MyData"];
        }
    }
}

然后我可以轻松地调用它

string a = CachedSettings.MyData;
于 2015-02-10T00:45:23.677 回答