?
Solved

Powershell Script help

Posted on 2015-02-09
9
Medium Priority
?
123 Views
Last Modified: 2015-02-10
Hi

I need to check who is logged onto various Blade Workstation's using a Powershell Script and output to a text file so that we can then format the text file to HTML using Powershell

We use this to help Users see what Blades are available who is logged in if they require a certain Blade.

I have used a .bat file in the past and cleaned the output file in Powershell, But would like to clean up the process with just one script.

One issue I have found when looking online for a solution is the ability to have the UserName field show as "No Users" when No user is logged on.

We use the "No User" to assign a Blade is Free icon in the HTML page.

I have tried to use the eq $Null but have failed so far.

Requirements

Hostname
UserName + "No User"
Time logged in

Any help on finding a solution would be very much appreciated.

Thank you
0
Comment
Question by:Si Walton
[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
  • 3
9 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 40598682
Can you provide more details how you do it now (e.g. the batch), and what you tried exactly?
0
 
LVL 1

Author Comment

by:Si Walton
ID: 40598689
Hi

We use the following batch file

winrs -r:pcbw177 hostname >> C:\Blades\UK\UK_Blades.txt  2>&1
Winrs -r:pcbw177 quser >> C:\Blades\UK\UK_Blades.txt  2>&1

It will provide the following to a txt file

PCBW00012
No User exists for *
PCBW00013
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 nimar                 console             3  Active      none   2/4/2015 2:52 PM

We then clean the txt file via Powershell
0
 
LVL 1

Author Comment

by:Si Walton
ID: 40598712
I have looked at many scripts but the below is close

Get-Content c:\myscripts\computers.txt | 
ForEach-Object {gwmi -computer $_ -class win32_computerSystem} | fl Name, UserName | 
out-file c:\myscripts\output8.txt

Open in new window


It gives the following output

Name     : PCBW180
UserName :

Name     : PCBW00013
UserName : DE-PROD\nimar
0
Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 40598752
Testing against $Null works for me. But we can also do it the other way round:
gwmi -computer (gc c:\temp\ee\computers.txt) Win32_ComputerSystem |
  select @{n='Blade'; e={$_.Name}},
         @{n='User'; e={ if ($_.UserName) { $_.Username } else { "No user" }} }

Open in new window

0
 
LVL 1

Author Comment

by:Si Walton
ID: 40598777
That works :)

Is there away to get the date/time stamp?

Thank you
0
 
LVL 1

Author Closing Comment

by:Si Walton
ID: 40598780
Quick and helpful thank you Qlemo
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40598805
Getting login time is more complex. You need to query Win32_LogonSession with the logon ID, which is associated to the name by another WMI object I cannot remember ...
0
 
LVL 1

Author Comment

by:Si Walton
ID: 40600427
Hi Qlemo

Login time isn't that important so it can be removed.

Is there away to have the USERNAME in Uppercase

DE-PROD\bendb = DE-PROD\BENDB

Thank you for your help with this, your time and effort is very much appreciated
0
 
LVL 1

Author Comment

by:Si Walton
ID: 40600725
any ideas how we can deal with the following error

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

we have an issue with the Blade and it would be good to assign an "issue" symbol on the HTML page

Thank you
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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...
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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 you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

752 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