PowerShell converts Quser string results to Objects

Source: Internet
Author: User

Today beans need to remotely log off a Terminal Server user, the operation is very simple, first quser get the remote user's SessionID, and then logoff command logoff can

Quser/server:xxxxlogoff sessionid/server:xxx


As with the early CMD and Linux bash commands, the quser result is a string. The processing of strings is cumbersome for scripts, and if you can convert them into standard object objects.


Look closely at the results of the quser, you can see his output is very neat, if I can put his middle space with a comma, that is the standard CSV format, it is easy to convert to object. The only problem is that some columns, like sessionname, I need to count how many spaces need to be replaced

ps c:\users\yli\documents>  $a =quser /server:sydbcc02ps c:\users\yli\documents>  $a  USERNAME               Sessionname        id  state   idle time   LOGON TIME smiths                 rdp-tcp#5          16   Active  24692+13:29  3/14/2017 9:06 AM llederbauer            rdp-tcp#4           22  active       1:18  3/14/2017 9:18  AM jedwards                                  23  disc          1:39  3/14/2017 7:54 AM tpicken                rdp-tcp#2           24  Active           .  3/14/2017 8:22 AM okaragiaouris          rdp-tcp#9          27  active          18  3/14/2017 8:55 am bhue                    rdp-tcp#7          28  active          12  3/14/2017 9:20 am mcherginets                                29  Disc         1:37   3/14/2017 8:58 AM abishop                                    30  Disc          2:48  3/14/2017 9:22 am fburns                                     31  Disc          2:28  3/14/2017 9:37 am ygordony                                   32  disc         2:07  3/14 /2017 10:04 am tmoloney               rdp-tcp#0          33  Active        1:07  3/14/2017 9:30 AM smaroo                 rdp-tcp#1           34  active       1:45   3/14/2017 10:19 AM jwoodford                                  35  disc           22  3/14/ 2017 10:42 am mozard                 rdp-tcp#8          36   active         56  3/14/2017 11:08 am  gmarsh                rdp-tcp# 6          37  active          41  3/14/2017 10:56 AM mking                                      38  disc            20  3/14/2017 11:11 AM mcolgan                rdp-tcp#11          39  Active         59   3/14/2017 11:16 AM jbelgiovane            rdp-tcp#12         41  Active          39  3/14/2017 11:33 AM ndrabsch                                   42  Disc              3  3/14/2017 11:57 am 


Directly look not very easy to differentiate, convert to ASCII look, you can see 32 (for space) a total of 16

$a =quser/server:sydbcc02[int[]][char[]] $a [2]3210810810110010111498971171011143232323232323232323232114100112


Okay, I'm going to have to replace the space between 2 and 17 with a comma, and the last code is as follows

$quser  =  (QUSER /SERVER:SYDBCC02)  -replace  ' \s{2,17} ',  ', '  |  Convertfrom-csv$quser | ftusername      sessionname id state   IDLE TIME LOGON TIME        --------       ----------- -- -----  --------- ----------         smiths                     16 Disc   3          3/14/2017 9:06 AM llederbauer   rdp-tcp#4    22 Active 1:26      3/14/2017 9:18 AM  jedwards                   23 disc   1:47      3/14/2017 7:54 am tpicken        rdp-tcp#2   24 Active 7          3/14/2017 8:22 am okaragiaouris rdp-tcp#9   27 active  .         3/14/2017 8:55 AM bhue           rdp-tcp#7   28 Active 20         3/14/2017 9:20 AM mcherginets                29 Disc   1:44       3/14/2017 8:58 AM abishop                    30 disc   2:56       3/14/2017 9:22 am fburns                     31 Disc   2:36       3/14/2017 9:37 AM ygordony                   32 Disc   2:15       3/14/2017 10:04 amtmoloney      rdp-tcp#0    33 active 1:15      3/14/2017 9:30 am  smaroo        rdp-tcp#1   34 Active  1:52      3/14/2017 10:19 amjwoodford                  35 Disc   30         3/14/2017 10:42 ammozard        rdp-tcp#8    36 Active .         3/14/2017 11:08  amgmarsh        rdp-tcp#6   37 active 48         3/14/2017 10:56 AMmking                          38     Disc      27                 mcolgan                    39 Disc    .         3/14/2017 11:16 AMjbelgiovane    rdp-tcp#12  41 active 47        3/14/2017 11:33 amndrabsch       rdp-tcp#3   42 Active .          3/14/2017 11:57 AMalawton                    43 Disc   11         3/14/2017 12:02 pm


This way can be easily further processed, the same ideas can be used in other CMD commands.

This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1906162

PowerShell converts Quser string results to Objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.