应用程序内的Powershell脚本在非开发计算机上不起作用

时间:2017-11-28 14:54:45

标签: c# powershell com vb6

我在旧的Visual Basic 6软件中有一个com兼容的.net dll。一切都很好。现在我添加了在我的软件中运行Powershell脚本的可能性。

DLL看起来像

public class PowerShellScripting
{

    private System.Collections.Generic.Dictionary<string, object> dic = new System.Collections.Generic.Dictionary<string, object>();

    public void AddObject(string Name, ref object obj)
    {
        this.dic.Add(Name, obj);
    }

    public string RunScript(string scriptText)
    {
        // create Powershell runspace
        System.Management.Automation.Runspaces.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();

        // open it
        runspace.Open();

        // to prevent error xxx.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
        System.Management.Automation.RunspaceInvoke scriptInvoker = new System.Management.Automation.RunspaceInvoke(runspace);
        scriptInvoker.Invoke("Set-ExecutionPolicy -Scope CurrentUser Unrestricted");

        foreach (System.Collections.Generic.KeyValuePair<string, object> entry in dic)
        {
            runspace.SessionStateProxy.SetVariable(entry.Key, entry.Value);
        }

        // create a pipeline and feed it the script text
        System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript(scriptText);

        System.Collections.ObjectModel.Collection<System.Management.Automation.PSObject> results = null;
        // execute the script
        try
        {
            results = pipeline.Invoke();
        }
        catch (Exception ex)
        {

            System.Windows.Forms.MessageBox.Show(ex.ToString(), "Code Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            return "";
        }

        // Check for errors
        if (pipeline.Error.Count > 0)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendLine("Error executing Powershell scripts:");

            foreach (object msg in pipeline.Error.ReadToEnd())
            {
                sb.AppendLine(msg.ToString());
            }

            System.Windows.Forms.MessageBox.Show(sb.ToString(), "Code Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
        }

        // close the runspace
        runspace.Close();

        // convert the script result into a single string
        System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
        foreach (System.Management.Automation.PSObject obj in results)
        {
            stringBuilder.AppendLine(obj.ToString());
        }

        return stringBuilder.ToString();
    }

}

我的开发计算机上的所有内容都运行良好,但在没有安装Visual Studio 2017的任何计算机上都无法运行(未测试旧版本)。 在我的应用程序中,我引用了我的dll并执行以下操作:

MyDLL.AddObject "MSFlexGrid", Me.MSFlexGrid1
MyDLL.RunScript MyScript

在我的电脑上,一切都运行得很好 在Powershellscript中,我可以访问网格

$MSFlexGrid.Rows = 20

在每台其他计算机上,我收到此错误消息

---------------------------
Code Error
---------------------------
System.Management.Automation.RuntimeException: The method or operation is not implemented. ---> System.NotImplementedException: The method or operation is not implemented.
   at System.Runtime.InteropServices.ComTypes.ITypeInfo.GetTypeAttr(IntPtr& ppTypeAttr)
   at System.Management.Automation.ComInterop.ComRuntimeHelpers.GetTypeAttrForTypeInfo(ITypeInfo typeInfo)
   at System.Management.Automation.ComInterop.IDispatchComObject.EnsureScanDefinedMethods()
   at System.Management.Automation.ComInterop.IDispatchComObject.System.Dynamic.IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
   at System.Dynamic.DynamicMetaObject.Create(Object value, Expression expression)
   at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters, LabelTarget returnLabel)
   at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at CallSite.Target(Closure , CallSite , Object , String )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   --- End of internal Exception ---
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at MyDLL.PowerShellScripting.RunScript(String scriptText)
---------------------------
OK   
---------------------------

正常的Powershell代码在所有计算机上运行正常。它只是访问我的主机应用程序中没有在开发计算机上运行的COM对象 我现在卡住了,因为我不知道客户端计算机上缺少什么。我查看了Powershell版本,一切都是一样的。

我在非开发计算机上部署的内容是什么?

0 个答案:

没有答案