Solved

PowerShell - Tell if a Server is Online or Disconnected

Posted on 2015-01-26
10
97 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 40

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
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.

 
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 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 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

Does Powershell have you tied up in knots?

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

Question has a verified solution.

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

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…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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