電源操作 ― 2007年06月02日 05時16分27秒
がさっと作ってみますた
昨日のエントリで拾ったコマンドラインから電源操作ネタでちょっとしたスクリプトを書いてみた。
なんのひねりもないクラス名だが、こんな感じ。
var PowerManager = function() {
this.initialize.apply( this, arguments );
}
PowerManager.prototype = {
initialize : function() {
this.shell = new ActiveXObject("WScript.Shell");
},
shutdown : function() {
this.shell.Run( "shutdown -s -t 0" );
},
reboot : function() {
this.shell.Run( "shutdown -r -t 0" );
},
suspend : function() {
this.shell.Run( "rundll32 powrprof.dll,SetSuspendState" );
}
}
つか、そもそもクラスにする意味ないっちゃないし。気にしないで。
使い方
もう、見てのとおり。shutdown()でシステム終了、reboot()で再起動、suspend()で休止状態(またはサスペンド)。
WshShell#Runでさくっとコマンド呼び出ししてるだけなので、shutdownとrebootは一瞬コンソールが表示される。どうせシステム終了するので放っておいてるが、気になるなら
shutdown : function() {
this.shell.Run( "shutdown -s -t 0", 0 );
}
とかしておけば非表示実行できる。と思う。試してないけど。
応用例
というか、サンプルスクリプト。ショートカットを作って、渡すパラメータを変えて使うとかね。
var PowerManager = function() {
this.initialize.apply( this, arguments );
}
PowerManager.prototype = {
initialize : function() {
this.shell = new ActiveXObject("WScript.Shell");
},
shutdown : function() {
this.shell.Run( "shutdown -s -t 0" );
},
reboot : function() {
this.shell.Run( "shutdown -r -t 0" );
},
suspend : function() {
this.shell.Run( "rundll32 powrprof.dll,SetSuspendState" );
}
}
// コマンドライン引数によって動作を決定
var p = WScript.Arguments.Unnamed.Length ? WScript.Arguments.Unnamed.Item(0) : "s";
var pm = new PowerManager();
var method = {
s : "shutdown",
r : "reboot",
p : "suspend"
}[ p ] || "shutdown";
pm[ method ]();
注意事項つか
手持ちのPCのみの現象かもしらんが、shutdown()を実行すると「電源を切ってもよい」状態になるだけで実際に電源が切れない。他所様の環境ではわからん。

最近のコメント