Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PowerShell - Tell if a Server is Online or Disconnected

Posted on 2015-01-26
10
Medium Priority
?
102 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 15

Author Comment

by:dbbishop
ID: 40571306
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 41

Accepted Solution

by:
footech earned 2000 total points
ID: 40571457
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
ID: 40571915
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
LVL 15

Author Comment

by:dbbishop
ID: 40572613
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
ID: 40572626
or all records.
0
 
LVL 41

Expert Comment

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

Author Comment

by:dbbishop
ID: 40573175
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 41

Assisted Solution

by:footech
footech earned 2000 total points
ID: 40573222
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
ID: 40573783
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 41

Expert Comment

by:footech
ID: 40574186
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

618 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