从PowerShell调用c#自定义操作

时间:2017-06-29 09:36:09

标签: c# powershell

我正在尝试从power-shell执行自定义操作方法这里是我用c#编写的代码

namespace MyCustomAction
{
    public class CustomActions
    {
       [CustomAction]
        public static Session TestResult(Session session)
        {
            session["name"] = "hello";
            return session;
        }
    }
}

在构建完解决方案后,我将如何加载程序集

[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\WiX Toolset v3.10\SDK\Microsoft.Deployment.WindowsInstaller.dll")

[System.Reflection.Assembly]::LoadFile("D:\CustomAction\\bin\x86\Debug\MyCustomAction.dll") | Out-Null

但我没有得到如何根据需要将会话传递给方法我尝试了以下但没有运气

$session = [Microsoft.Deployment.WindowsInstaller.Session]
[MyCustomAction.CustomActions]::TestResult($session)

1 个答案:

答案 0 :(得分:0)

如果您要创建班级的新对象,可以使用New-Object

$session = New-Object [Microsoft.Deployment.WindowsInstaller.Session]

https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/new-object