Script help needed.

DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
Not sure where to post this so please move it if its in the wrong place

I need to get a small script that will run localy on a PC and return a True or False result if any one is logged on.

We have a cluster based computing system (A bit Like Boinc or Folding at home) whcih uses the spare CPU cycles of peoples PC's to carry out complex mathmatical modeling.

At the momen it is running on a of network cluster of about 12 PC's, but that is really just a test and what we want to do is run it on actuly desktops (around 300+ ) but only while there is no one logged on.

And althouh the software does not have a built in method for detecting if any one is logged in to the system, it can accept a TRUE/FALSE return statmets from a script that is set to run periodiolicy.

Now I looked at PSlogged on from sysinternal which give me the following out put
H:\>psloggedon  -x
loggedon v1.33 - See who's logged on
Copyright ® 2000-2006 Mark Russinovich
Sysinternals - [URL="http://www.sysinternals.com"]www.sysinternals.com[/URL]
Users logged on locally:
     NT AUTHORITY\LOCAL SERVICE
     NT AUTHORITY\NETWORK SERVICE
     workdomain\devilwah
     NT AUTHORITY\SYSTEM
No one is logged on via resource shares.
 

so from that I am sure there is a way to run it through a script to check only the Local service, Networkservice and system are logged on. But I am not sure how to do this.

Even better would be a way to have a single script do the whole job.

I have looked around but I can't find anything, so I was wondering if any one can help?

Cheers

DevilWAH
  • If you can't explain it simply, you don't understand it well enough. Albert Einstein
  • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.

Comments

  • dynamikdynamik Banned Posts: 12,312 ■■■■■■■■■□
    What type of script does it have to be? You probably just need to do some simple pattern matching. The -l option will only show local users and you could just search for "workdomain."
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    Either a batch script or VBscript, its got to run on XP machines that are standard build.

    Thats what I was thinking, but I didn't know if it was possible to get away from using psloggedon completly and have a singe script that does the lot.
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    Use WMI. I don't know how you would do it in VB Script but here it is in PoSh. You should be able to translate it with a little research.
    (Get-WmiObject -class win32_computerSystem).username 
    

    I leave it to you to determine how to use the result to set a boolean variable.
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    I found this
    strComputer = "servername" ' " use "." for local computer 
    Set objWMI = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" _ 
    & strComputer & "\root\cimv2")
    Set colSessions = objWMI.ExecQuery _ 
    ("Select * from Win32_LogonSession Where LogonType = 2") 
     
    If colSessions.Count = 0 Then 
    Wscript.Echo "FALSE" 
    Else
    WScript.Echo "RDP Sessions:"
    For Each objSession in colSessions
     
    Set colList = objWMI.ExecQuery("Associators of " _ 
    & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _
    & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" ) 
     
    For Each objItem in colList
    WScript.Echo "Username: " & objItem.Name & " FullName: " & objItem.FullName
     
    Next
    Next
    End If
    

    But just gives an error about server doesno exist "getobject" line 3 ??

    Other wise this seems he perfect script
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    ahh got it :)
    strComputer = "."   ' " use "." for local computer 
    Set objWMI = GetObject("winmgmts:" _ 
               & "{impersonationLevel=impersonate}!\\" _ 
               & strComputer & "\root\cimv2")
    Set colSessions = objWMI.ExecQuery _ 
      ("Select * from Win32_LogonSession Where LogonType = 2") 
         
    If colSessions.Count = 0 Then     
      Wscript.Echo "FALSE" 
    Else
      WScript.Echo "True"
      
    End If  
    
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    am i right in thinking that addin the line

    exitCode = TRUE

    would return this to the script/application calling this script ?
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    if i want this script to run under cscript rather than wscript. what can i put in it to achive this? i know i can run it as Cscript by putting that in front on the cli. but i want it to run as Cscript even if i was to just click it in windows. Cheers
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • ClaymooreClaymoore Member Posts: 1,637
    cscript //h:cscript //s

    This will set the default scripting engine to cscript

    To set it back to wscript, use this:

    wscript //h:wscript //s
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    would that do it for all scripts or just that one?
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • ClaymooreClaymoore Member Posts: 1,637
    That will do all of them. If you only want to change this script, you could create a batch file that would call cscript and the script and just double-click that.
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    I have ended up with this :)
    Public objShell : Set objShell = CreateObject("WScript.Shell")
    If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
        objShell.Run "cscript " & chr(34) & WScript.ScriptFullName & chr(34) & " //Nologo" & chr(34), 0, False
     
        WScript.Quit
    End If
    strComputer = "."   ' " use "." for local computer 
    Set objWMI = GetObject("winmgmts:" _ 
               & "{impersonationLevel=impersonate}!\\" _ 
               & strComputer & "\root\cimv2")
    set colSessions = objWMI.ExecQuery _ 
      ("Select * from Win32_LogonSession Where LogonType = 2") 
         
    If colSessions.Count = 0 Then     
      Set objStdOut = WScript.StdOut
      objStdOut.Write "UserLoggedIn = False"
       
      
    Else
      Set objStdOut = WScript.StdOut
      objStdOut.Write "UserLoggedIn = True"  
    End If  
    

    This catches if the script is run as Wscript and resubmits it as under cscript.

    the different log in codes are listed here if any one wants to change this script to show remote users, or service logons.

    Logon Type Codes Revealed
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    And that reminds me why I never learned VB scripting.... I imagine a similar thing could be done in PoSh in just a coule lines.
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    And that reminds me why I never learned VB scripting.... I imagine a similar thing could be done in PoSh in just a coule lines.

    Ah yes, but that would require installing PoSH on all 1500+ XP machines ;)

    I never lernt scripting either, just hash togather other peoples code ;)
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • darkerosxxdarkerosxx Banned Posts: 1,343
    DevilWAH wrote: »
    Ah yes, but that would require installing PoSH on all 1500+ XP machines ;)

    Which is probably a good idea, anyways, if you're going to be running scripts on 1500+ XP machines. icon_wink.gif
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    DevilWAH wrote: »
    Ah yes, but that would require installing PoSH on all 1500+ XP machines ;)

    I never lernt scripting either, just hash togather other peoples code ;)

    I know. I was just making a general observation about VB vs PoSh.

    I'm a total PoSh fan boy. If someone asks me how to toast bread, I think of a way to do it in PoSh. But In all seriousness. If you find yourself needing to do remote management on multiple systems, installing PoSh might be a very good idea. Not related to your current project, though.
  • ClaymooreClaymoore Member Posts: 1,637
    You can deploy PoSh as an optional update through WSUS. Or you could, I haven't used WSUS in a while.

    Once it is installed on all your workstations, you can use group policy to control the execution policy:
    Download details: Administrative Templates for Windows PowerShell
  • ZaitsZaits Member Posts: 142
    Keep in mind VBscripts don't all work the same with Windows 7 so you may have to eventually re-program this.
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    darkerosxx wrote: »
    Which is probably a good idea, anyways, if you're going to be running scripts on 1500+ XP machines. icon_wink.gif

    Yes but this assumes you have access to all the PC's that need to run the script. And its a bit of an over kill to run one script locally, and even when you are running admin scripts many scripts can be run from a remote PC (your admin PC) and don't even require VBscript or PoSH to run on the remote machine.

    for example this script is calling in to WMI as I understand, so if you change the first line to

    strComputer = "pc_name_here"

    it will retrieve remote info with out need for remote running of script. And I believe that PoSH also makes use of WMI so 90% of the time there is no need to install it on the remote PC.

    Currently I know 100% of the PC's have VB script that can do all the scripting needed. Why would you want to complicate it by installing PoSH, that's then another step in the chain to go wrong.

    Of course if I have windows 7 machine I would be doing this in PoSH, but as they say "don't try to fix what ain't broken" ;)

    I have played with PoSH on servers but I think it's a bit of an over kill for clients PC's, like I say most of the time you don't need to run the script on the client and if you do VB or even batch scripts will do the job just fine. I like to keep things simple.
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
Sign In or Register to comment.