Solved

PowerShell - Tell if a Server is Online or Disconnected

Posted on 2015-01-26
10
94 Views
Last Modified: 2015-01-27
I am creating a PowerShell script that can be run interactively on a server, but it is also possible that the user can disconnect from the machine while the script is executing. Before displaying a messagebox, I'd like to know whether the user is logged in (and display the messagebox) or if the account is running disconnected, and write a log message in lieu of a messagebox. Can this be done?
0
Comment
Question by:dbbishop
  • 6
  • 4
10 Comments
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
FYI, I am wanting to test if the user who started the script is disconnected, not just get a list of disconnected users, etc.
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
Comment Utility
This question isn't too dissimilar to those about getting a list of connected/logged-on users, as many of the methods of getting a list of those users also retrieves their status.

My preferred method of getting a list of those users (at least for now) is to parse the results of a "query user" command (only tested on Win7/2008R2).
$scriptblock = {
        Invoke-Expression "query user" 2> $null |
         Select -Skip 1 |
         Where-Object {$_ -match '^\s?>?(?<user>\S+)\s+((?<session>\S+)\s+)?(?<id>\S+)\s+(?<state>\S+)\s+(?<idle>\S+)\s+(?<logontime>[\d/]+ [\d:]+ [apm]+)$'} |
         Select @{N="User";e={$matches.user}},@{n="Session";e={$matches.session}},@{n="ID";e={$matches.id}},@{n="State";e={$matches.state}},@{n="Idle";e={$matches.idle}},@{n="LogonTime";e={$matches.logontime}},@{n="loggedon";e={$true}}
    }

Open in new window

You can run the script block with & or Invoke-Command.
& $scriptblock
Then you just have to filter the results by user and check the state.
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
Not exactly sure what to do here. I tried running this code but didn't seem to get any output.
I've got a userid stored in a variable $activeUser. I just want to determine if that user is logged on or disconnected. It would be a domain account on the server that I am testing.
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
never mind. for some reason I was not seeing the output.
Can you tell me how I would filter this to get just the State for a specific user? I've been playing around with it and either get an error or no output.
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
or all records.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 39

Expert Comment

by:footech
Comment Utility
Are you familiar with filtering by using Where-Object?
0
 
LVL 15

Author Comment

by:dbbishop
Comment Utility
To some extent, but only in the context that I've seen it used in some examples. I tried using it but apparently don't know the exact syntax because I am still getting all users, or a syntax error.

For my purpose, all I want to do is return the state for a specific user. Possibly a function where I pass a user and return the state.
0
 
LVL 39

Assisted Solution

by:footech
footech earned 500 total points
Comment Utility
The following will show the object the username you specify that has a state of disconnected.
& $scriptblock | Where {$_.User -eq "username" -and $_.State -eq "Disc" }

Open in new window

If there's no match there won't be any output.

If you just want the state no matter what for a user.
& $scriptblock | Where {$_.User -eq "username"} | Select State

Open in new window

0
 
LVL 15

Author Closing Comment

by:dbbishop
Comment Utility
Got it to work. I've done some phenominal stuff with PowerShell, but still pretty much a newbie. I find what I need to get by on the web and get it to work, and usually can figure it out without asking, but once in a while come across a roadblock. Wish I had time to just deep-dive into it because I have a feeling I'd be even more amazed at what it can do.
Thanks again. Don't spend all the points in one place :-D
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
There's definitely a lot of capability.  Once you get good at the basics (using the pipeline, filtering objects, selecting properties, checking conditions), you can go a long way.  Keep it up!  :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now