?
Solved

PowerShell - Tell if a Server is Online or Disconnected

Posted on 2015-01-26
10
Medium Priority
?
100 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 40

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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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 40

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 40

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 40

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

800 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