拆分文本在PowerShell中列 [英] Split text by columns in PowerShell

查看:361
本文介绍了拆分文本在PowerShell中列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个PowerShell的新手(Bash是我的事常)目前是谁在试图获得qwinsta输出到显示谁是登录为rdpwd'(的rdesktop)的用户,这样我可以对用户名列表来检查每个用户名,如果它们不匹配,将其注销。

I'm a PowerShell novice (Bash is my thing normally) who's currently trying to obtain qwinsta output to show who is logged in as an 'rdpwd' (rdesktop) user so that I can check each username against a list of usernames, and if they do not match, log them off.

我目前通过两个问题的工作:

I am currently working through two problems:


  1. 我无法拆分将只剩下的用户名qwinsta输出 - 我已经尝试了分裂的功能,但到目前为止,我得到任何语法问题,或怪异的结果;一个抱怨似乎是,'\\ S +'匹配的字母S,而不是空白;我已经设法分割到第二列其他时间,但是从线只输出1出现

  2. 虽然我现在还没有,我感觉到我将与第二阶段的问题为好,即通过非登录用户offable(这是从本地用户群中获得)的阵列循环

我将重点放在问题1现在!

I'll focus on problem 1 for now!

我已经得到的文字是:

SESSIONNAME       USERNAME        ID     STATE   TYPE      DEVICE
services                          0      Disc
console                           1      Conn
rdp-tcp#0         user.name1      2      Active  rdpwd
rdp-tcp#1         user.name2      3      Active  rdpwd
rdp-tcp#1         user.name3      4      Active  rdpwd
rdp-tcp                           65536  Listen

我想要的输出是:

The output I want is:

user.name1
user.name2
user.name3

(随着然后创建一个循环,说,总之来说,列表中的用户的foreach,如果不是在本地组,注销用户的目标。)

(With the aim of then creating a loop that says, in brief terms, "foreach user in list, if not in localgroup, logoff user".)

到目前为止,我已经尽量了与rdpwd文本选择,但使用的所有变动的方式在分裂,我没有比进一步向前。

So far, I've got as far as selecting text with 'rdpwd', but using all manner of variations on "split", I have not got further forward than that.

我很乐意分享我已经得到了,但很可惜,我不认为它会帮助任何人!

I'm happy to share what I've got already, but alas I don't think it'll help anyone!

任何援助将AP最多preciated。 :)

Any assistance would be most appreciated. :)

推荐答案

老实说,我想看看一个更好的方式来做到这一点,但你可以用一些文本操作和 ConvertFrom-CSV做傻事它的cmdlet:

Honestly I'd look up a better way to do this, but you can fudge it with some text manipulation and the ConvertFrom-Csv cmdlet:

$(qwinsta.exe) -replace "^[\s>]" , "" -replace "\s+" , "," | ConvertFrom-Csv | select username

首先更换任何前导空格或> 字符没事,然后用逗号替换任何空格。然后你可以管 ConvertFrom-CSV 并与数据作为一个对象的工作。

Firstly replace any leading spaces or > characters with nothing, then replace any white spaces with a comma. Then you can pipe to ConvertFrom-Csv and work with the data as an object.

其实,上面有一些问题,大多与 \\ S + ,因为如果一栏为空白它不能正确识别为一个空白的领域,下一个文本被错误地促进了当前字段。

Actually, the above has some issues, mostly with the \s+ because if a column is blank it does not get correctly recognised as a blank field, and the next text is incorrectly promoted to the current field.

以下是此命令一个完全成熟的解析器,并可能为任何类型的表格输出从本机Windows EXE工作:

The below is a full blown parser for this command, and would probably work for any sort of tabulated output from a native windows exe:

$o = @()
$op = $(qwinsta.exe)

$ma = $op[0] | Select-String "(?:[\s](\w+))" -AllMatches
$ErrorActionPreference = "Stop"

for($j=1; $j -lt $op.length; $j++) {
    $i = 0
    $obj = new-object pscustomobject
    while ($i -lt $ma.matches.count) { 
      $prop = $ma.matches[$i].groups[1].value; 
      $substrStart = $ma.matches[$i].index 
      $substrLen = $ma.matches[$i+1].index - $substrStart
      try {
        $obj | Add-Member $prop -notepropertyvalue $op[$j].substring($substrStart,$substrLen).trim() 
      }
      catch [ArgumentOutOfRangeException] {
        $substrLen = $op[$j].length - $substrStart 
        if($substrLen -gt 0) {
          $obj | Add-Member $prop -notepropertyvalue $op[$j].substring($substrStart,$substrLen).trim()
        }
        else {
          $obj | Add-Member $prop -notepropertyvalue ""
        }
      }
      $i++
    }
    $o += ,$obj
}

$o | ? { $_.type -eq 'rdpwd'} | select username

USERNAME
--------
user.name1
user.name2
user.name3

这篇关于拆分文本在PowerShell中列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆