LoginSignup
3
3

More than 5 years have passed since last update.

とりあえずpowershellのスクリプトを実行する。

Posted at

「スクリプトの実行がシステムで無効になっているため、ファイル C:... を読み込めません。」とかはもうどうでもいいから、前準備とかなしにとりあえず1ファイル一発で動くpowershell用スクリプトをでっちあげる仕組みを考えた。

WSH/Jscript内にコメントとしてpowershell用スクリプトを記述する。

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe

を起動するようにすれば、MSScriptControl.ScriptControl も使えてお得かもしれない。試してないけど。

KickPs.js
fso = new ActiveXObject("Scripting.FileSystemObject");
WshShell = new ActiveXObject("WScript.Shell");
var temp = fso.GetSpecialFolder(2); // 2:%TEMP%

var myself = function(){return fso.OpenTextFile( WScript.ScriptFullName,1).ReadAll();}();
myself = myself.replace(/^[\s\S]*\/\* powershell code from here/m, "" );
// [\s\S]:「改行も含むすべての文字」にマッチするトリッキーなパターン
myself = myself.replace(/   powershell code to here \*\//, "" );

var tempps1 = fso.BuildPath( temp, "temptemp.ps1" );
var ouf = fso.CreateTextFile( tempps1,true );
ouf.Write( myself ); ouf.Close();

var cmd = "powershell -NoProfile -ExecutionPolicy Unrestricted " + tempps1;
WshShell.Run(cmd,10,true);
fso.DeleteFile(tempps1);

//////////////////////////////////////////////////////////////////////
// ↓のコメント部分は変更禁止。
/* powershell code from here

Write-Host "Hello world"
pause

   powershell code to here */

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3