[wpkg-users] Shutdown windows
Malte Starostik
malte at malte.homeip.net
Tue Mar 9 10:27:48 CET 2010
Am Montag, 8. März 2010 22:39:43 schrieb Sebastian Mazur:
> Hi,
>
> I`d like to turn off windows after installing package. I`ve tried to
> shutdown command (shutdown -s -t 0) but windows didn`t switch off.
>
> Has someone idea, how to do it?
If you run WPKG Client on startup, this doesn't work as Windows will refuse to
shutdown before having finished booting...
I've solved this by running a .js script in my post-action that spawns another
script without waiting for its completion, so that bootup can continue. This
second script attempts a delayed shutdown and keeps retrying for some time,
aborting the whole thing if a user is logged in.
Attachments have been renamed to *.txt to get them through the list:
post-action.js - run as WPKG Client's post action - checks for a <2h old file
called \\deploy\control\%COMPUTERNAME%.shutdown and if it exists, spawns off
shutdown.js from the same directory. This flagging file is created by a
script that I use to wake-on-lan machines with an inconsistent package state.
The logging format mimicks the one of wpkg.js so I can parse it with the same
tool.
Cheers,
Malte
-------------- next part --------------
var sh = new ActiveXObject("WScript.Shell");
var net = new ActiveXObject("WScript.Network");
var fs = new ActiveXObject("Scripting.FileSystemObject");
var shutdownFlag = "\\\\deploy\\control\\" + net.ComputerName.toLowerCase() + ".shutdown";
if (fs.FileExists(shutdownFlag))
{
var now = new Date();
var age = (now.getTime() - (new Date(fs.GetFile(shutdownFlag).DateCreated)).getTime()) / 1000;
if (age <= 7200) sh.Run("cscript " + fs.BuildPath(fs.GetParentFolderName(WScript.ScriptFullName), "shutdown.js"));
}
-------------- next part --------------
WScript.Sleep(60000);
function pad(value, digits)
{
var result = value.toString();
while (result.length < digits) result = '0' + result;
return result;
}
var sh = new ActiveXObject("WScript.Shell");
var fs = new ActiveXObject("Scripting.FileSystemObject");
var logStream = null;
function log(severity, msg)
{
var now = new Date();
var year = pad(now.getFullYear(), 4);
var month = pad(now.getMonth() + 1, 2);
var day = pad(now.getDate(), 2);
var hour = pad(now.getHours(), 2);
var min = pad(now.getMinutes(), 2);
var sec = pad(now.getSeconds(), 2);
if (logStream === null)
logStream = fs.OpenTextFile(
sh.ExpandEnvironmentStrings("\\\\deploy\\logs\\%COMPUTERNAME%-" + year + month + day + hour + min + sec + ".shutdown.log"),
8, true, -2
);
logStream.WriteLine(year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + ", " + severity + ": " + msg);
}
function info(msg) { log("INFO ", msg); }
function warn(msg) { log("WARNING", msg); }
function error(msg) { log("ERROR ", msg); }
try
{
fs.DeleteFile("\\\\deploy\\control\\" + (new ActiveXObject("WScript.Network")).ComputerName.toLowerCase() + ".shutdown");
}
catch (e)
{
}
for (var i = 0; i < 24; ++i)
{
if (GetObject("winmgmts://./root/cimv2").ExecQuery("select UserName from Win32_ComputerSystem where not UserName is null").Count > 0)
{
warn("The system was scheduled to shutdown but someone is logged in. Not shutting down!");
WScript.Quit(0);
}
for (
var it = new Enumerator(GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary = true"));
!it.atEnd();
it.moveNext()
)
{
var result = it.item().win32Shutdown(5);
if (result === 0)
{
info("System shutdown in progress!");
logStream.Close();
logStream = null;
}
else warn("Failed to initiate system shutdown: error " + result + ". Will keep trying.");
}
WScript.Sleep(300000);
}
error("Failed to initiate system shutdwon: error " + result + ". Giving up!");
WScript.Quit(1);
More information about the wpkg-users
mailing list