回答編集履歴

1

追記

2016/08/31 14:06

投稿

Zuishin
Zuishin

スコア28662

test CHANGED
@@ -1,3 +1,117 @@
1
1
  [PowerShellスクリプトをExeに変換する](http://mtgpowershell.blogspot.jp/2013/03/powershellexe.html)のはどうですか?
2
2
 
3
3
  または [CSharpCodeProvider クラス](https://msdn.microsoft.com/ja-jp/library/microsoft.csharp.csharpcodeprovider(v=vs.110).aspx)を使えば PowerShell などから C# をコンパイルできます。
4
+
5
+ ###追記
6
+
7
+ CSharpCodeProvider と WinAPI を使った例です。
8
+
9
+ 下記 Compile-Cs.ps1 と untitled.cs を保存し、PowerShell から `Compile-Cs untitled.cs` としてコンパイルしてください。untitled.exe ができますので、それを実行すると Hello World と表示されるはずです。
10
+
11
+ **Compile-Cs.ps1**
12
+
13
+ ```
14
+
15
+ [CmdletBinding(SupportsShouldProcess = $True)]
16
+
17
+ Param (
18
+
19
+ [Parameter(
20
+
21
+ Mandatory = $True,
22
+
23
+ Position = 0
24
+
25
+ )]
26
+
27
+ [string[]]$Path = @(),
28
+
29
+
30
+
31
+ [Parameter(
32
+
33
+ Mandatory = $False,
34
+
35
+ Position = 1
36
+
37
+ )]
38
+
39
+ [string]$ExeName = "untitled.exe",
40
+
41
+
42
+
43
+ [Parameter(
44
+
45
+ Mandatory = $False
46
+
47
+ )]
48
+
49
+ [string[]]$Reference = @()
50
+
51
+ )
52
+
53
+ $compiler = New-Object Microsoft.CSharp.CSharpCodeProvider
54
+
55
+ $compilerParameters = New-Object System.CodeDom.Compiler.CompilerParameters
56
+
57
+ if ($Reference.Length -gt 0) {
58
+
59
+ $compilerParameters.ReferencedAssemblies.AddRange($Reference)
60
+
61
+ }
62
+
63
+ $compilerParameters.GenerateInMemory = $False
64
+
65
+ $compilerParameters.GenerateExecutable = $True
66
+
67
+ $compilerParameters.OutputAssembly = $ExeName
68
+
69
+ $results = $compiler.CompileAssemblyFromFile($compilerParameters, $Path)
70
+
71
+ if ($results.NativeCompilerReturnValue -ne 0) {
72
+
73
+ $results.Output | Write-Host
74
+
75
+ exit 1
76
+
77
+ } else {
78
+
79
+ $results
80
+
81
+ }
82
+
83
+ ```
84
+
85
+ **untitled.cs**
86
+
87
+ ```C#
88
+
89
+ using System;
90
+
91
+ using System.Runtime.InteropServices;
92
+
93
+
94
+
95
+ static class Program
96
+
97
+ {
98
+
99
+ [DllImport("msvcrt.dll")]
100
+
101
+ static extern int puts(string s);
102
+
103
+
104
+
105
+ static void Main()
106
+
107
+ {
108
+
109
+ puts("Hello World!");
110
+
111
+ }
112
+
113
+ }
114
+
115
+ ```
116
+
117
+