/** * @name CeL function for Windows * @fileoverview * 本檔案包含了 Windows 系統管理專用的 functions。 * @since */ 'use strict'; if (typeof CeL === 'function') CeL.run( { name : 'application.OS.Windows', code : function(library_namespace) { /** * null module constructor * @class web 的 functions */ var _// JSDT:_module_ = function() { // null module constructor }; /** * for JSDT: 有 prototype 才會將之當作 Class */ _// JSDT:_module_ .prototype = { }; // -------------------------------------------------------- // 在 .hta 中, typeof WScript==='undefined'. // http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/objects/shell/application.asp // http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/programmersguide/shell_intro.asp // 用 IE 跑 ActiveXObject 可能會出現 ActiveX 指令碼的警告,須更改「允許主動式內容在我電腦上的檔案中執行」或改用 Mark of the Web。 // IE 工具→「網際網路選項」→「進階」→「安全性」→「允許主動式內容在我電腦上的檔案中執行」 _// JSDT:_module_ . // for Microsoft Windows Component Object Model. // http://blogs.msdn.com/b/ericlippert/archive/2004/06/01/145686.aspx // http://technet.microsoft.com/library/ee156598.aspx new_COM = typeof WScript === 'object' ? function(id) { // http://msdn.microsoft.com/en-us/library/xzysf6hc(v=vs.84).aspx return WScript.CreateObject(id); } : typeof ActiveXObject === 'function' ? function(id) { // e.g., in HTA return new ActiveXObject(id); } : typeof Server === 'object' && Server.CreateObject && function(id) { return Server.CreateObject(id); }; // CeL.application.OS.Windows.no_COM if (_.no_COM = !_.new_COM && 'No Component Object Model support!') { if (false && !library_namespace.env.ignore_COM_error) library_namespace.warn('new_COM: no Component Object Model support!'); (_.new_COM = function(id) { // 忽略沒有 Windows Component Object Model 的錯誤。 if (!library_namespace.env.ignore_COM_error) throw new Error('new_COM: No method to get Microsoft Component Object Model (COM): [' + id + ']! You may need to set `CeL.env.ignore_COM_error = true`!'); }); return _; } // WScript.Echo((_.no_COM? '沒' : '') + '有 Windows Component Object Model。'); /** * test if is a COM.
* 經驗法則。並非依照規格書。 * * @param object * object to test * @returns {Boolean} is a COM. */ function is_COM(object) { try { if (library_namespace.is_Object(object) && '' === '' + object && typeof object.constructor === 'undefined' ) { var i; for (i in object) { return false; } try { i = String(object); return false; } catch (e) { return true; } } return false; } catch (e) { // TODO: handle exception } } _// JSDT:_module_ . is_COM = is_COM; var HTA; /** * Are we run in HTA?
* ** HTA 中應該在 DOM ready 後呼叫,否則 document.getElementsByTagName 不會有東西! * @param {String}[id] HTA tag id (only used in low version that we have no document.getElementsByTagName) * @return We're in HTA * @require library_namespace.is_WWW * @since 2009/12/29 19:18:53 * @_memberOf _module_ * @see * http://msdn2.microsoft.com/en-us/library/ms536479.aspx * http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr05/hey0420.mspx * http://www.msfn.org/board/lofiversion/index.php/t61847.html * lazy evaluation * http://peter.michaux.ca/articles/lazy-function-definition-pattern */ function get_HTA(id) { if (!library_namespace.is_HTA) return; if (library_namespace.is_WWW(true)) { HTA = document.getElementsByTagName('APPLICATION')[0]; } else HTA = library_namespace.is_WWW() && id && document.all && document.all[id]; return HTA; }; library_namespace.set_initializor(get_HTA, _); function parse_command_line(command_line, no_command_name){ // TODO: "" 中的判別仍有問題。 var args = [], re = no_command_name ? /\s+("([^"]*)"|(\S+))/g : /\s*("([^"]*)"|(\S+))/g, result; // commandLine 第一引數為 full script name while (result = re.exec(command_line)) args.push(result[3] || result[2]); return args; } //get_WScript_object(); //get_WScript_object[generateCode.dLK]='is_WWW,get_HTA,WSH,dirSp'.split(','); //get_WScript_object[generateCode.dLK].push('*var args,WshShell,WinShell,WinShell,fso;get_WScript_object();'); var WSH, WshShell = "WScript.Shell", WinShell = "Shell.Application", FSO = "Scripting.FileSystemObject", args; var log_message; /** * * @param HTML_only */ function get_WScript_object(HTML_only) { var i; if (typeof WshShell === 'string') { //library_namespace.debug('Initializing using ' + _.new_COM, 1, 'get_WScript_object'); try { WshShell = _.new_COM(WshShell); WinShell = _.new_COM(WinShell); FSO = _.new_COM(FSO); } catch (e) { if ((e.number & 0xFFFF) === 429) // Automation 伺服程式無法產生物件 // Run-Time Error '429' OLE Automation Server Can't Create Object // 把 HTA 當作 HTML? throw e; library_namespace.error(e); } //library_namespace.debug(typeof FSO, 1, 'get_WScript_object'); } else library_namespace.debug('Already initialized!', 1, 'get_WScript_object'); if (typeof WScript === 'object' // && typeof WScript.constructor=='undefined' ) { // Array.from() args = Array.prototype.slice.call(WScript.Arguments); // Microsoft Windows Script Host (WSH) i = (WSH = WScript.FullName).lastIndexOf(library_namespace.env.path_separator); if (i !== -1) WSH = WSH.slice(i + 1); } else if ( !(typeof HTML_only === 'undefined' ? library_namespace.is_WWW() && !_.get_HTA() : HTML_only)// !HTML_only// && typeof ActiveXObject !== 'undefined') try { if (i = _.get_HTA()){ args = parse_command_line(i.commandLine); // default HTA host is mshta.exe. WSH = 'mshta.exe'; } } catch (e) { } // 判斷假如尚未load則排入以確定是否為HTA else if (library_namespace.is_WWW(1) && !_.get_HTA() // && !document.getElementsByTagName('body').length ) setTimeout(function() { get_WScript_object(HTML_only); }, 100); try { // CScript.exe only // var stdout = FSO.GetStandardStream(1); // var stderr = FSO.GetStandardStream(2); log_message = function(message) { // stdout.WriteLine(message); // WScript.StdOut.Write(message); WScript.StdOut.WriteLine(message); }; } catch (e) { // using WScript.exe log_message = function(message) { WScript.Echo(message); }; } // WScript.StdIn.ReadLine() /* * @cc_on @if(@_jscript_version >= 5) // JScript gives us Conditional * compilation, we can cope with old IE versions. // and security blocked * creation of the objects. ;//else.. @end@ */ i = { WshShell : WshShell, WinShell : WinShell, FSO : FSO, args : args, WSH : WSH }; if(HTML_only) throw i; return i; }; library_namespace.set_initializor(get_WScript_object, _); /* 2007/11/17 23:3:53 使用 ADSI (Active Directory Service Interface) 存取資料 http://support.microsoft.com/kb/234001 http://www.dbworld.com.tw/member/article/010328b.htm http://support.microsoft.com/kb/216393 */ function addUser(name,pw,group,computer){ // http://msdn.microsoft.com/library/en-us/script56/html/wsmthenumprinterconnections.asp ; // 連上伺服器 var oIADs,o; // 利用Create指令,指定產生一個新的使用者類別,以及使用者帳號的名稱。使用SetInfo的指令將目錄服務中的資料更新。 try{oIADs=new Enumerator(GetObject(computer='WinNT://'+(computer||(new_COM('WScript.Network')).ComputerName)));}catch(e){}//WScript.CreateObject('WScript.Network') if(oIADs){//try{ if(name){ try{o=oIADs.Create('user',name);}catch(e){o=new Enumerator(GetObject(computer+'/'+name));} o.SetPassword(pw),/*o.FullName=name,o.Description=name,*/o.SetInfo(); // Administrators if(group)(new Enumerator(GetObject(computer+'/'+group))).Add(o.ADsPath); // o.ADsPath: computer+'/'+name return o; // 得到用戶 } //oIADs.Filter=['user'];//new VBArray('user'); // no use, 改用.AccountDisabled o={}; // http://msdn2.microsoft.com/en-us/library/aa746343.aspx // 對所有的oIADs,通常有Name,Description for(var i,j,a,b,p='Name,AccountDisabled,Description,FullName,HomeDirectory,IsAccountLocked,LastLogin,LoginHours,LoginScript,MaxStorage,PasswordExpirationDate,PasswordMinimumLength,PasswordRequired,Profile'.split(',');!oIADs.atEnd();oIADs.moveNext())if(typeof oIADs.item().AccountDisabled==='boolean'){ for(i=oIADs.item(),j=0,a={};jnul 2>nul" : "%COMSPEC% /c exit", 0, true); } catch (e) { } }; /* 送key到application http://msdn.microsoft.com/library/en-us/script56/html/wsmthsendkeys.asp SendKeys('a') 送a SendKeys("a{1}4{2}5"); 送a,等1/10s,送4,等2/10s,送5 timeOut: <0:loop, 0 or not set:1 time, >0:be the time(ms) */ var SendKeysU; //SendKeys[generateCode.dLK]='Sleep'; function SendKeys(keys,appTitle,timeOut,timeInterval){ if(typeof WshShell!=='object'||typeof WshShell!=='object'&&typeof(WshShell=new_COM("WScript.Shell"))!=='object')return 1; if(isNaN(timeInterval)||timeInterval<1)timeInterval=100; // 時間間隔 timeOut=timeOut?timeOut<0?-1:Math.floor(timeOut/timeInterval)+1:0; if(appTitle) while(!WshShell.AppActivate(appTitle)) if(timeOut--)Sleep(timeInterval);else return 2; if(!SendKeysU)SendKeysU=100; // 時間間隔單位 while(keys.match(/\{([.\d]+)\}/)){ WshShell.SendKeys(keys.substr(0,RegExp.index)); Sleep(SendKeysU*RegExp.$1); keys=keys.substr(RegExp.lastIndex); } return WshShell.SendKeys(keys); } // Create an object reference: hack?! //var windows=new WScript(); // Run the calculator program //windows.explorer.run('calc.exe'); // Writing the local computer name to the screen //document.write(windows.network.computerName); // Copy files from one folder to another //windows.fileSystem.copyFile('c:\\mydocuments\\*.txt', 'c:\\tempfolder\\'); return ( _// JSDT:_module_ ); } });