Skip to content

Instantly share code, notes, and snippets.

@cat2151
Last active July 20, 2017 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cat2151/95e39bd148c19ea1a45d02b5bf75914c to your computer and use it in GitHub Desktop.
Save cat2151/95e39bd148c19ea1a45d02b5bf75914c to your computer and use it in GitHub Desktop.
batファイル内のPowerShell内のJavaScriptプログラムを動かす
@%windir%\SysWOW64\cmd.exe /C powershell -NoProfile -ExecutionPolicy Unrestricted "$s=[scriptblock]::create((gc \"%~f0\"|?{$_.readcount -gt 1})-join\"`n\");&$s" %*&goto:eof
# http://qiita.com/cd01/items/82829ba0ec0f59e1b04d
# http://blog.livedoor.jp/morituri/archives/53997724.html
Param($argBat)
$objjs = New-Object -comobject ScriptControl
$objjs.Language = "Javascript"
$src = @'
function helloFunc(arg) {
msg = "hello " + arg + " world";
print(msg);
return msg;
}
var log = "";
function print(v) {
log += v;
}
function readLog() {
return log;
}
'@
$objjs.Eval($src)
$helloResult = $objjs.CodeObject.helloFunc($argBat)
Write-Output -InputObject $helloResult
Write-Output -InputObject $objjs.CodeObject.readLog()
exit 0
@cat2151
Copy link
Author

cat2151 commented Jul 20, 2017

特に根拠はないが、
batファイル内のPowerShell内のJScript.NET(≒JavaScript)プログラムを動かす
のほうがよさそう。あちらのほうが将来性、安定性、機能性などの面で有利そうに見受けられる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment