How to convert string to table or objects in power

2019-02-24 11:31发布

How can I convert the below exported text to csv so that i can use it as objects in powershell. Eg:

where{$_.qlimit -eq 27}

Text:

  samid            qlimit    qused  
  Administrator    -1        0      
  Guest            -1        0      
  admin            27        8      
  krbtgt           -1        0      
  r                -1        0      
  admin2           44        0  

3条回答
Emotional °昔
2楼-- · 2019-02-24 12:12

Another way with ConvertFrom-String (no headers in the file):

(Get-Content 'your_file').trim() | ConvertFrom-String -PropertyNames samid,qlimit,qused
查看更多
相关推荐>>
3楼-- · 2019-02-24 12:13

You can easily convert your table to PowerShell objects using the ConvertFrom-SourceTable cmdlet from the PowerShell Gallery:

ConvertFrom-SourceTable '
  samid            qlimit    qused  
  Administrator    -1        0      
  Guest            -1        0      
  admin            27        8      
  krbtgt           -1        0      
  r                -1        0      
  admin2           44        0  
' | Where-Object {$_.qlimit -eq 27}

Result:

samid qlimit qused
----- ------ -----
admin 27     8
查看更多
ゆ 、 Hurt°
4楼-- · 2019-02-24 12:24

Use the Get-Content cmdlet to load the file, replace two or more whitespaces with a comma and convert it to CSV using the ConvertFrom-Csv cmdlet:

$object = (Get-Content 'your_file').Trim() -replace '\s{2,}', ',' | ConvertFrom-Csv

If you now query your object:

$object | where qlimit -eq 27

You get the desired output:

samid qlimit qused
----- ------ -----
admin 27     8    
查看更多
登录 后发表回答