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

查看:28
本文介绍了在 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. 虽然我还没有到那里,但我感觉我在第二步中也会遇到问题,即遍历不可注销用户的数组(将从本地用户组获取)

现在我将专注于问题 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

我想要的输出是:

user.name1
user.name2
user.name3

(目的是创建一个循环,简而言之,foreach user in list, if not in localgroup, logoff user".)

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

到目前为止,我已经使用 'rdpwd' 选择了文本,但是使用了split"的各种变体,我还没有比这更进一步.

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!

任何帮助将不胜感激.:)

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天全站免登陆