Wednesday, November 10, 2004

Find out who is logged on?

This is in continuation to my last article about the displaying the drive details on local or remote computer using jscript.
In this part of article i have added a part of code which show the current user logged on to the given sytem. The script and its usage remains the same but output provides some extra details about logged on user.
To get the disk drive information i had used Win32_LogicalDisk class, in order to find out logged on user information i am using Win32_ComputerSystem class.

Newly added code is shown in BLUE color BOLD type

/**
* Script: CheckSpace.js
* Purpose: Prints the size and free space of all harddisk on local or remote computer and also displays the name of the user logged on.
* Author: Mukesh N. Shende
* Date: 6 Nov 2004
* Web: http://windowsexplorer.blogspot.com
* Note: Must be run under an account with sufficient
* permissions.
**/


//Code 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 compute and also the user logged on details
function GetSpace( 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 );

//Query for computer operating system in a Windows environment.
var loggedin = wmi.ExecQuery ("Select UserName FROM Win32_ComputerSystem");
var colComputer = new Enumerator( loggedin );

var cc = colComputer.item();
//If no one is logged on to the computer.
if (cc.UserName == null)
{
print("\nNo User is Currently Logged on " + computer);
print("\nDrive Space On " + computer );
print( "-------------------------" );
}
else
{
var TheUser = cc.UserName.split("\\");

print("\nUser Logged On: " + TheUser[1]);
print("\nDrive Space On " + computer );
print( "-------------------------" );
}

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 < size )
showmsg = " " + showmsg;

return( showmsg );
}

// Print
function print( msg )
{
WScript.Echo( msg );
}

// Print Usage
function Usage()
{
var app = WScript.ScriptName;
print( app + " [computer]" );
WScript.Quit( 0 );
}


// Code ends Here

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.

1 Comments:

At February 11, 2009 at 1:36 PM, Anonymous Anonymous said...

welcome to the wow power leveling cheap Wow gold service site, buy cheap wow gold,wow gold,world of warcraft power leveling buy wow gold

 

Post a Comment

<< Home