Windows Perl Scripting Forums » Perl

How to access Win32_LogonSession?

(3 posts)
  • Started 11 years ago by csburris
  • Latest reply from csburris


  1. csburris

    I need to access the Win32_LogonSession object from Win32:OLE to see whether a session is running interactively. I cannot figure how the load the object. Has anyone done this? The examples I've seen show how to access OLE programs, not the session itself.

    Posted 11 years ago #
  2. keydet89

    Hope this helps...

    #! c:\perl\bin\perl.exe

    use Win32::OLE qw(in);

    my $locatorObj = Win32::OLE->new('WbemScripting.SWbemLocator') || die
    "Error creating locator object: ".Win32::OLE->LastError()."\n";
    $locatorObj->{Security_}->{impersonationlevel} = 3;
    my $serverObj = $locatorObj->ConnectServer(Win32::NodeName(),'root\cimv2',"","")
    || die "Error connecting to $config{server}: ".Win32::OLE->LastError()."\n";

    foreach my $sess (in $serverObj->InstancesOf("Win32_LogonSession")) {
    print "Auth : ".$sess->{AuthenticationPackage}."\n";
    print "ID : ".$sess->{LogonId}."\n";
    print "Type : ".$sess->{LogonType}."\n";
    print "\n";
    Posted 11 years ago #
  3. csburris

    Well, your script works, but I wanted one that only showed my session. I've fiddled around and this one works for the current session only (the intent is to see whether the program is being run by the scheduler or interactively, and so to decide whether to use the gui or a log file for messages.

    Here's the program:

    #! c:\perl\bin\perl.exe

    # Print out values of the WMI Class "Win32_LogonSession"

    use Win32::OLE qw(in);
    $Win32::OLE::Warn = 3;
    use constant vbTab => "\x09";

    $strComputer = '.';

    # ------ Properties of Win32_LoginSession ----------------
    # string AuthenticationPackage;
    # string Caption;
    # string Description;
    # datetime InstallDate;
    # string LogonId;
    # uint32 LogonType;
    # string Name;
    # datetime StartTime;
    # string Status;

    my $datetime = Win32::OLE->new("WbemScripting.SWbemDateTime") or die;

    $objWMI = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2');
    $colOS = $objWMI->InstancesOf('Win32_LogonSession');
    foreach my $objOS (in $colOS) {
    print 'AuthenticationPackage: ' . $objOS->AuthenticationPackage, "\n";
    print 'Caption: ' . $objOS->Caption, "\n";
    print 'Description: ' . $objOS->Description, "\n";
    print 'InstallDate: ' . $objOS->InstallDate, "\n";
    print 'LogonId: ' . $objOS->LogonId, "\n";
    print 'LogonType: ' . $objOS->LogonType, "\n";
    print 'Name: ' . $objOS->Name, "\n";
    $datetime->{Value} = $objOS->StartTime;
    printf( "StartTime: %02d-%02d-%04d at %02d:%02d:%02d\n", $datetime->{Month},
    $datetime->{Day}, $datetime->{Year}, $datetime->{Hours}, $datetime->{Minutes},
    $datetime->{Seconds} );
    print 'Status: ' . $objOS->Status, "\n";
    Posted 11 years ago #

RSS feed for this topic


You must log in to post.