LoginSignup
1
0

More than 3 years have passed since last update.

PowerShellスクリプトのラッパースクリプト(JScript)

Posted at

PowerShellスクリプトはそのままでは実行できない。powershell.exeのExecutionPolicy引数にRemoteSignedを渡し、PowerShellスクリプトを実行できるようにしたラッパースクリプト。

実行方法

  1. ラッパースクリプト(拡張子 js)と実行したいPowerShellスクリプト(拡張子 ps1)を同じフォルダに置く
  2. 2つのスクリプトの拡張子を除くファイル名を同じにする
  3. ラッパースクリプトを実行する
    • ラッパースクリプトをダブルクリックする
    • ラッパースクリプトにファイルをドラッグ&ドロップする
    • コマンドプロンプトで cscript を使用して実行する

ソース

ラッパースクリプト

runps1.js

// powershell.exe の WindowStyle 引数の値
var PS_WINDOW_STYLE = "Normal";

// WshShell.Run の 第2引数(windowStyle)の値
var WSH_WINDOW_STYLE = 1;

// WshShell.Run の 第3引数(waitOnReturn)の値
var WSH_WAIT_ON_RETURN = true;

function main() {
    try {
        var cmd = [
                "powershell.exe", "-NoLogo", "-NoProfile",
                "-WindowStyle", PS_WINDOW_STYLE,
                "-ExecutionPolicy", "RemoteSigned",
                "-File", quote(WScript.ScriptFullName.replace(/\.js$/, ".ps1"))
            ];
        for (var i = 0; i < WScript.Arguments.length; i++) {
            cmd.push(quote(WScript.Arguments(i)));
        }
        return WScript.CreateObject("WScript.Shell").Run(cmd.join(" "), WSH_WINDOW_STYLE, WSH_WAIT_ON_RETURN);
    } catch (e) {
        WScript.Echo(e)
        return -1;
    }
}

function quote(arg) {
    return arg.indexOf(" ") >= 0 ? '"' + arg + '"' : arg;
}

WScript.Quit(main());

実行したいPowerShellスクリプトの例

runps1.ps1
echo "Hello World!"

Read-Host "続けるには Enter キーを押してください..."
1
0
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
1
0