qshinoの日記

Powershell関係と徒然なこと

C# in powerShell

C# in PowerShell

PowerShell 内でC#を使う。

C#のコードを文字列に格納

$cs = "csharp code..."

Add-Typeコマンドレットでロード

Add-Type $cs

参照アセンブリがある場合は、

C#以外の言語なら、

  • Language <言語名>

言語名にはJSharp, CSharpVersion3, visualbasic, JScript が使用可能。

ロード後は、C#コードが使用可能。

ファイルから

$file=“a.cs” Add-Type $file

$dll = “a.dll” Add-Type $dll

サンプル

Param($name)
$asm = ("System")
$cs = @"
using System;
public static class Salute
    {
        public static void Hi(string name)
        {
            Console.WriteLine("Hi, " + name+ "!");
        }
    }
"@

Add-Type $cs -ReferencedAssemblies $asm
[Salute]::Hi($name)

DLLからの場合

> [Reflection.Assembly]::LoadFile("c:\temp\MyMathLib.dll")
> [MyMathLib.Methods]::Sum(10, 2)

> $mathInstance = new-object MyMathLib.Methods
> $mathInstance.Product(10, 2)

DLLの中は

namespace MyMathLib
{
    public class Methods
    {
        public Methods()
        {
        }

        public static int Sum(int a, int b)
        {
            return a + b;
        }

        public int Product(int a, int b)
        {
            return a * b;
        }
    }
}

アセンブリロードについて

まずはPSVersion

Add-TypeはPowerShell 2.0以降で実装

PowerShell1.0の場合

Add-Typeの問題

Add-Typeは便利だが、複数リビジョンのアセンブリが導入済みの場合、古い方のロードやロードに失敗する。

その場合、フルネームで指定する。

Add-Type -AssemblyName "Microsoft.SqlServer.SMO, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"

あるいは Reflection.Assembly::LoadWithPartialName(“Microsoft.SqlServer.Smo”)

アセンブリロード

整理すると、

  1. PS1.0なら、LoadWP 一択。
  2. 2.0以降なら、add-type クラス名で。
  3. エラーなら、LoadWP…
  4. 最後は、Add-Type フルネーム指定

Add-Type書式

4種類の書式がある。

  1. -AssemblyName : アセンブリ名[]
  2. [-Name] クラス名
  3. [-Path] ファイル名[]
  4. [-TypeDefinition] コード文字列

2番以降なら下記オプションでDLL作成可。簡易コンパイラになる。

  • -OutputAssemly DLL名
Add-Type -AssemblyName <string[]> [-IgnoreWarnings] [-PassThru] [<CommonParameters>]

Add-Type [-Name] <string> [-MemberDefinition] <string[]> [-CodeDomProvider <CodeDomProvider>] [-CompilerParameters <CompilerParameters>] [-Language {<CSharp> | <CSharpVersion3> | <VisualBasic> | <JScript>}] [-Namespace <string>] [-OutputAssembly <string>] [-OutputType <OutputAssemblyType>] [-ReferencedAssemblies <string[]>] [-UsingNamespace <string[]>] [-IgnoreWarnings] [-PassThru] [<CommonParameters>]

Add-Type [-Path] <string[]> [-CompilerParameters <CompilerParameters>] [-OutputAssembly <string>] [-OutputType <OutputAssemblyType>] [-ReferencedAssemblies <string[]>] [-IgnoreWarnings] [-PassThru] [<CommonParameters>]

Add-Type [-TypeDefinition] <string> [-CodeDomProvider <CodeDomProvider>] [-CompilerParameters <CompilerParameters>] [-Language {<CSharp> | <CSharpVersion3> | <VisualBasic> | <JScript>}] [-OutputAssembly <string>] [-OutputType <OutputAssemblyType>] [-ReferencedAssemblies <string[]>] [-IgnoreWarnings] [-PassThru] [<CommonParameters>]

参考

http://yomon.hatenablog.com/entry/2013/06/05/PowerShellスクリプト内でC#コードを書いて使う

add-type

http://tech.guitarrapc.com/entry/2014/03/17/042253

add-type reference

https://technet.microsoft.com/en-us/library/dd315241.aspx

dll load

http://stackoverflow.com/questions/3079346/how-to-reference-net-assemblies-using-powershell

technet

https://blogs.technet.microsoft.com/stefan_gossner/2010/05/07/using-csharp-c-code-in-powershell-scripts/