Saturday, November 06, 2004

Into the scripting world.

Recently i was learning scripting technologies like WSH to automate certain tasks and make my work little easier.Being a Networking Professional i knew this will surely help me in my work on daily basis. The initial experience was not so interesting as i was facing lot of difficulties in understanding language specifications. I had to read on vbscript and jscript a lot. During the same time i found very nice document on microsoft technet center. The Portable Script Center found at http://www.microsoft.com/downloads/details.aspx?FamilyID=b4cb2678-dafb-4e30-b2da-b8814fe2da5a&displaylang=en was really exciting for me. After a little reading i started to build small scripts and started playing with them.

As a first assignment for myself i decided to build a script which will give me the HardDisk Size and Free Space available on given computer.After struggling for sometime i could build very handy script. I realize the importance of such script only after building it.

The JScript is as follows,

// Script Starts Here.

function main()
{
var args = WScript.Arguments;
if( args.Length == 0 )
{
GetSpace( "localhost" );
}
else if( args.Length == 1 )
{
if( args.Item( 0 ) == "/?" )
Usage();
else
GetSpace( args.Item( 0 ) );
}
else
{
Usage();
}
}
main();

// Get size and free space for all harddisks on a computer
function GetSpace( computer )
{
print( "Drive Space on " + computer );
try
{
var wmi = GetObject( "winmgmts:{impersonationLevel=impersonate}!\\\\" + computer + "\\root\\cimv2" );
// Query for logical disk info DriveType 3 = HardDisk
var disks = wmi.ExecQuery( "SELECT * FROM Win32_LogicalDisk WHERE DriveType=3" );
var edisks = new Enumerator( disks );

for( ; !edisks.atEnd(); edisks.moveNext() )
{
var disk = edisks.item();
print( "Drive : " + disk.DeviceID );

var pfree = String( ( disk.FreeSpace / disk.Size ) * 100 );
pfree = pfree.substring( 0, pfree.lastIndexOf( "." ) + 3 ) + "%";
print( "Free : " + FormatSize( disk.FreeSpace ) + " (" + pfree + ")" );
print( "Size : " + FormatSize( disk.Size ) );

print( "-------------------------" );
}
}
catch( e )
{
print( "Error: " + e.description );
}
}

// Convert Bytes into MB
function FormatSize( size )
{
var mb = 1024 * 1024;
var rst = Math.round( size / mb );
rst = padright( rst, 6 );
return( rst + " MB" );
}

// Pad with leading spaces.Helps to format the output.
function padright( msg, size )
{
var showmsg = String( msg );
while( showmsg.length < showmsg = " " app =" WScript.ScriptName;">Usage
1.Copy the code and paste it into Notepad, save the file as "checkspace.js".
2.On command prompt go to the path of the file.
3.Type cscript checkspace.js /? which will show the help of the file
4. cscript checkspace.js will show the information about the local computer on which you are running the script.
5. cscript checkspace.js [computer] will show the information about the computer name specified as a parameter to the script.

This is just a start, i am planning to explore more about such scripts and start using them.

0 Comments:

Post a Comment

<< Home