﻿using System.Dynamic;
using System.IO;
using System.Management.Automation;
using System.Reflection;

namespace ConfigPS
{
    public class Global : DynamicObject
    {
        readonly PowerShell ps;

        public Global()
        {
            var fileName = Path.GetFileName(Assembly.GetCallingAssembly().CodeBase);
            var configFileName = fileName + ".ps1";
            ps = PowerShell.Create();

            if (File.Exists(configFileName))
            {
                const string initScript = @"
                        function Add-ConfigItem($name, $value)
                        {
                            Set-Variable -Name $name -Value $value -Scope global
                        }
                    ";

                ps.AddScript(initScript);
                ps.Invoke();

                var profileScript = File.ReadAllText(configFileName);
                ps.AddScript(profileScript);
                ps.Invoke();
            }
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = ps.Runspace.SessionStateProxy.GetVariable(binder.Name);
            return true;
        }
    }
}

﻿using System.Dynamic;
using System.IO;
using System.Management.Automation;
using System.Reflection;

namespace ConfigPS
{
    public class Global : DynamicObject
    {
        readonly PowerShell ps;


        public Global(string configFileName = null)
        {
            if (configFileName == null)
            {
                var fileName = Path.GetFileName(Assembly.GetCallingAssembly().CodeBase);
                configFileName = fileName + ".ps1";
            }

            ps = PowerShell.Create();

            if (File.Exists(configFileName))
            {
                const string initScript = @"
                        function Add-ConfigItem($name, $value)
                        {
                            Set-Variable -Name $name -Value $value -Scope global
                        }
                    ";

                ps.AddScript(initScript);
                ps.Invoke();

                var profileScript = File.ReadAllText(configFileName);
                ps.AddScript(profileScript);
                ps.Invoke();
            }
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = ps.Runspace.SessionStateProxy.GetVariable(binder.Name);
            return true;
        }
    }
}

Allow config file to passed in
Allow config file to passed in
