Solved

PowerShell - Tell if a Server is Online or Disconnected

Posted on 2015-01-26
10
96 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
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 39

Accepted Solution

by:
footech earned 500 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 39

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 39

Assisted Solution

by:footech
footech earned 500 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 39

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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 is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

822 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