PowerShell | 要對象的 TXT 內容

摺紙

感謝您到目前為止為我提供的所有幫助。我試圖用奇怪的想法來挑戰我正在學習的東西。我正在嘗試通讀 TXT 文件並嘗試將它們轉換為內存中的自定義對象,以便稍後進行操作。

TXT 文件的內容 - 可能有更多的“塊”

  Object GUID:  86dabdb1-cdc7-421c-a58e-7c2cb55b1dba
  Name:         ScannerSchduler
  Location:     \\?\C:\Windows\System32\Tasks\Microsoft\Windows Defender\
  Type:         file
  Status:       stored
  Store time:   Wed Nov 10 11:47:55 2021 (1636525075)
  Threat GUID:  b9d9575c-4723-4df3-b9ee-5d97a1d5b8bf
  Threat name:  Troj/MineJob-A

  Object GUID:  3eeeb91c-731e-4e03-a55e-4b200df17805
  Name:         WindowsParentalControlsSettings
  Location:     \\?\C:\Windows\System32\Tasks\Microsoft\Windows\Shell\
  Type:         file
  Status:       stored
  Store time:   Wed Nov 10 11:47:58 2021 (1636525078)
  Threat GUID:  b9d9575c-4723-4df3-b9ee-5d97a1d5b8bf
  Threat name:  Troj/MineJob-A

開頭有 2 個空格,但我們可以正確地 Trim()。

我的目標自定義對像是一個對像數組,

$Objects = @() 

ForEach ($a in $Array)
{
    $object = New-Object -TypeName PSObject
    $object | Add-Member -MemberType NoteProperty -Name "Object_GUID" -Value $var[1]
    $object | Add-Member -MemberType NoteProperty -Name "Name" -Value ??
    $object | Add-Member -MemberType NoteProperty -Name "Location" -Value ??
    $object | Add-Member -MemberType NoteProperty -Name "Type" -Value ??
    $object | Add-Member -MemberType NoteProperty -Name "Status" -Value ??
    $object | Add-Member -MemberType NoteProperty -Name "Store_time" -Value ??
    $object | Add-Member -MemberType NoteProperty -Name "Threat_GUID" -Value ??
    $object | Add-Member -MemberType NoteProperty -Name "Threat_Name" -Value ??
    $schtasks += $object
}

我感到不好意思尋求幫助,但這不是“家庭作業”。我不知道如何灌輸腳本思維和思考方法。我讀過一本書,上過幾門課,但最好的方法是尋求挑戰,而我總是在像這樣的小挑戰中失敗。

$Array 的內容將是包含信息的行。我如何 Foreach 將每行分成兩個 Name:Type 組合。

Where-Object {$_.length -gt 0} | Foreach-Object {
    $var = $_.split(':',2).trim()
    New-Variable -Name $var[0] -Value $var[1]
    Write-Host ($var[0] + " = " + $var[1])

以上可以為我找到的每一行提供 Name:Value 對。但是我無法將它嵌套在可以為我創建對象的東西中。我自己會繼續努力的。

馬蒂亞斯·R·傑森

假設屬性列表始終按順序排列,您可以使用該Object GUID:行作為新對象的錨點:

$data = @'
  Object GUID:  86dabdb1-cdc7-421c-a58e-7c2cb55b1dba
  Name:         ScannerSchduler
  Location:     \\?\C:\Windows\System32\Tasks\Microsoft\Windows Defender\
  Type:         file
  Status:       stored
  Store time:   Wed Nov 10 11:47:55 2021 (1636525075)
  Threat GUID:  b9d9575c-4723-4df3-b9ee-5d97a1d5b8bf
  Threat name:  Troj/MineJob-A

  Object GUID:  3eeeb91c-731e-4e03-a55e-4b200df17805
  Name:         WindowsParentalControlsSettings
  Location:     \\?\C:\Windows\System32\Tasks\Microsoft\Windows\Shell\
  Type:         file
  Status:       stored
  Store time:   Wed Nov 10 11:47:58 2021 (1636525078)
  Threat GUID:  b9d9575c-4723-4df3-b9ee-5d97a1d5b8bf
  Threat name:  Troj/MineJob-A
'@ -split '\r?\n' 

$objects = $data |ForEach-Object -Process {
  # always trim leading and trailing whitespace from line
  $_ = $_.Trim()

  if($_ -like 'Object GUID:*'){
    # first line in new block, output current object if any
    if($properties.Count){
      [pscustomobject]$properties
    }
    # ... then start processing new object
    $properties = [ordered]@{}
  }

  if($_ -like '*:*'){
    # extract name/value pair, trim again
    $name,$value = $_.Split(':') |ForEach-Object Trim
    # add value to property table
    $properties[$name] = $value
  }
} -End {
  # do we still have data collected for the last block in the sequence? output the last object too then
  if($properties.Count){
    [pscustomobject]$properties
    $properties = $null
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

對 Powershell 腳本獲取內容感到困惑

將對象放在最後而不推送內容

如何使用netcat接收txt文件的內容

如何獲取 .txt 文件內容 JS

將數組的內容打印到 .txt 文件中

cloudwatch lambda 日誌未顯示嵌套對象內容

打字稿未驗證正在傳播以完成類型的對象的內容

使用迭代顯示對象 c# 中字段的內容

如何在 ruby on rails 視圖中呈現嵌套 json 對象的內容

如何在頁面powershell開頭添加內容?

VStack 內容居中而不是左對齊

Reactstrap Modal 的內容對於 Jest mount 是不可見的

我的 CSS 代碼有問題。<div> 內的內容不居中對齊

我的 ms-auto 沒有在 div 內對齊內容?

如何遍歷對象內部的對象

Python 項目 - 將 .txt 文件的內容寫入 Pandas 數據框

在使用 php 發送 .txt 文件的內容之前進行格式化

如何將txt文件內容添加到文本框

如何在 Blazor WebAssembly 中顯示 .txt 文件的內容

C如何將.txt文件的內容放入二維數組

Powershell - 遍歷文件夾,獲取內容並發佈到 SOAP

PowerShell:獲取 json 對象的值

VBA 運行時錯誤“1004”:應用程序定義或對象定義錯誤”設置單元格內容

如果對象存在於 JSON 中,則打印另一個值,如果不存在,則不打印任何內容

如何在Python3中使用beautifulsoup查找配對內容

Linux shell:根據變量的內容對文件進行排序

修復覆蓋內容的位置,但相對位置沒有

在 Solr 中對內容進行排序和存儲

如何根據鍵 starts_with 對哈希內容進行分組?