Freitag, 20. Juli 2012

Sending email via Javascript

If you want to automaticly be notified by a script sending emails is a good way.
This is a sample script where you can see how this is accomplished.
It is assumed that not authentification is required.

 var msg = new ActiveXObject("CDO.Message");   
 var config = new ActiveXObject("CDO.Configuration");  
 config.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2;   
 config.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "wwsmtp01.ww-intern.de";   
 config.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25;   
 config.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 0;   
 config.Fields.Update();  
 msg.Configuration=config;   
 msg.From="andreas.knees@ww-informatik.de";   
 msg.To="martin.niedenzu@ww-informatik.de";   
 msg.Subject="Enter Subject Here";   
 msg.TextBody = meinMailInhalt;   
 msg.Send();;  
 msg=null;   
 config=null;  

Donnerstag, 28. Juni 2012

convert ANSI to UTF-8 using JavaScript

 var adTypeBinary = 1;  
 var adTypeText  = 2;  
 var bOverwrite = true;  
 var bAsASCII = false;  
 var oFS = new ActiveXObject("Scripting.FileSystemObject");  
 var sFiNa = "OPM_Mitarbeiter.csv";  
 var sFrom = "Windows-1252";  
 var sFFSpec = oFS.GetAbsolutePathName( sFiNa);  
 var oTo = new ActiveXObject( "ADODB.Stream" );  
 var sTo = "utf-8";  
 var sTFSpec = oFS.GetAbsolutePathName( sFiNa + "-utf8.txt" );  
 WScript.Echo("File name:"+sTFSpec);  
 if (oFS.FileExists( sTFSpec )) {oFS.DeleteFile(sTFSpec);};  
 var oFrom = new ActiveXObject( "ADODB.Stream" );  
 oFrom.Type = adTypeText;  
 oFrom.Charset = sFrom;  
 oFrom.Open();  
 oFrom.LoadFromFile(sFFSpec);  
 WScript.Echo(oFrom.Size + " Bytes in " + sFFSpec);  
 var oTo = new ActiveXObject( "ADODB.Stream" );  
 oTo.Type  = adTypeText;  
 oTo.Charset = sTo;  
 oTo.Open();  
 oTo.WriteText(oFrom.ReadText());  
 WScript.Echo(oTo.Size + " Bytes in " + sTFSpec);  
 var adSaveCreateNotExist = 1;  
 WScript.Echo(sTFSpec);  
 oTo.SaveToFile(sTFSpec , adSaveCreateNotExist);  
 oFrom.Close();  
 oTo.Close();  
 WScript.Echo("Das Programm ist nicht abgestürzt");  

Remove BOM in Javascript

Sometimes you need to remove the BOM from a UTF-8 File. E.g. because Java can't deal with it. So if you use Windows as your OS you can use the follwing JavaScript.
Simple execute it in a console.
 // Removes the Byte Order Mark - BOM from a text file with UTF-8 encoding  
 // The BOM defines that the file was stored with an UTF-8 encoding.  
 function RemoveBOM(filePath) {  
     // Create a reader and a writer  
         var writer,reader, fileSize;  
         var writer = new ActiveXObject("Adodb.Stream");  
         var reader = new ActiveXObject("Adodb.Stream");  
     // Load from the text file we just wrote  
         reader.Open();  
         reader.LoadFromFile( filePath);  
     // Copy all data from reader to writer, except the BOM  
         writer.Mode=3;  
         writer.Type=1;  
         writer.Open();  
         reader.position=5;  
         reader.copyto(writer,-1);  
     // Overwrite file  
         writer.SaveToFile(filePath,2);  
     // Return file name  
         RemoveBOM = filePath;  
     // Kill objects  
         var writer = null;  
         var reader = null;  
 }  
 RemoveBOM("OPM_Mitarbeiter.csv-utf8.txt");  
cscript bomremover.js

Freitag, 30. März 2012

Backup using Robocopy, Dropbox and Javascript


  • Stoppen eine Prozeses

  • Kopieren der Daten

  • Den Prozess erneut starten



Robocopy, or "Robust File Copy", is a command-line directory replication command. It has been available as part of the Windows Resource Kit starting with Windows NT 4.0, and was introduced as a standard feature of Windows Vista, Windows 7 and Windows Server 2008. Robocopy is notable for capabilities above and beyond the built-in Windows copy and xcopy commands, including the following: • Ability to tolerate network interruptions and resume copying. • Ability to copy file data and


 var objWMIService = GetObject("winmgmts:\\\\.\\root\\CIMV2");  
var colitems = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE name = 'notepad.exe'");
var e = new Enumerator(colitems);
for (; !e.atEnd(); e.moveNext())
{
e.item().Terminate();
}
try {
var objShell = new ActiveXObject("wscript.shell");
var oExec = objShell.Exec("robocopy c:\Daten g:\Daten /MIR /LOG:robocopy.log");
while (oExec.Status == 0)
{
WScript.Sleep(100);
}
WScript.Echo("finished robocopy");
try {
objShell.Run("C:\\WINDOWS\\system32\\notepad.exe");
//Warte-Schleife ist an dieser Stelle nicht erforderlich
WScript.Echo("launched Notepad");
}
catch (e){
WScript.Echo('Can not find '+"C:\\WINDOWS\\system32\\notepad.exe")
}
objShell = null;
}
catch (e){
WScript.Echo('Can not find '+"robocopy c:\Daten g:\Daten /MIR /LOG:robocopy.log")
}

Donnerstag, 16. Februar 2012

Change Password For Current User - VbScript

ChangePasswordForCurrentUser.vbs

 Const ADS_UF_PASSWD_CANT_CHANGE = 64  

Set WshShell = WScript.CreateObject("WScript.Shell")
Set oUserEnv = WshShell.Environment("PROCESS")


userID = oUserEnv("USERNAME")

If Not IsEmpty(userID) Then
Set oADsUser = GetObject("WinNT://ww-intern/" & userID)
WScript.Echo "UserFullName = " & oAdsUser.Get("FullName")
lUserFlags = oAdsUser.Get("userFlags")
If lUserFlags AND ADS_UF_PASSWD_CANT_CHANGE THEN
WScript.Echo "User darf Kennwort nicht ändern, das passende Recht wird jetzt gesetzt"
lUserFlags = lUserFlags Or ADS_UF_PASSWD_CANT_CHANGE
ELSE
Wscript.Echo "User darf Kennwort ändern"
lUserFlags = lUserFlags And Not ADS_UF_PASSWD_CANT_CHANGE
End If
oAdsUser.Put "userFlags", lUserFlags

strOldPwd = InputBox("Bitte geben Sie das aktuelle Passwort ein:","Altes Passwort eingeben")
strNewPwd = InputBox("Bitte geben Sie das neue Passwort ein:","Neues Passwort eingeben")
If strNewPwd <> "" Then
oADsUser.ChangePassword strOldPwd, strNewPwd
WScript.Echo "Das Passwort wurde geändert"
Else
WScript.Echo "Das Passwort darf nicht leer sein!"
End if
Else
WScript.Echo "UserID konnte nicht ermittelt werden!"
End If

jscript terminate process

 var objProcess;  
var colProcess;
var strComputer = ".";
var strProcessKill = "'calc.exe'"

var objWMIService = GetObject("winmgmts:\\\\.\\root\\CIMV2");
var colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " + strProcessKill )

var enumItems = new Enumerator(colProcess);

for (; !enumItems.atEnd(); enumItems.moveNext()) {
var objItem = enumItems.item();
objItem.Terminate()
WScript.Echo("fund" );
}

WScript.Echo("Just killed process " + strProcessKill + " on " + strComputer );

automation - jscript http client

Need do download a file via HTTP on a Windows-Server?

Here is a how-to.


    
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET", 'http://www.nikem.de/Willkommen_files/chart1.jpg', false);
xmlhttp.send(null);

var adTypeBinary = 1
var adSaveCreateOverWrite = 2
var BinaryStream = new ActiveXObject("ADODB.Stream");

//Specify stream type - we want To save binary data.
BinaryStream.Type = adTypeBinary

//Open the stream And write binary data To the object
BinaryStream.Open();
BinaryStream.Write(xmlhttp.responseBody);

//Save binary data To disk
BinaryStream.SaveToFile ("C:\chart1.jpg", adSaveCreateOverWrite);

WScript.Echo("Ready");






I used the binary format in the sample. So that data are saved as they come.
A text-based version can also be usefull.
It is also possible to support defirent encodings e.g. UTF-8.

Detailed Informations can be found under:
http://msdn.microsoft.com/en-us/library/ms535874(v=vs.85).aspx