PowerShell | TXT内容到对象



感谢您迄今为止对我的帮助。我试图用奇怪的想法来挑战我所学的东西。我正在尝试读取TXT文件,并尝试将它们转换为内存中的自定义对象,以便稍后进行操作。

TXT文件的内容-可能有更多的";块";

Object GUID:  86dabdb1-cdc7-421c-a58e-7c2cb55b1dba
Name:         ScannerSchduler
Location:     \?C:WindowsSystem32TasksMicrosoftWindows 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:WindowsSystem32TasksMicrosoftWindowsShell
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

开头有两个空格,但我们可以向右修剪((。

我的目标自定义对象是一组对象,

$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的内容将是包含信息的行。我如何预测将每行拆分为两个名称:类型组合。

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])

上面可以为我找到的每一行提供名称:值对。但我无法将它嵌套在可以为我创建对象的东西中。我会继续自己尝试。

假设属性列表总是有序的,则可以使用Object GUID:行作为新对象的锚点:

$data = @'
Object GUID:  86dabdb1-cdc7-421c-a58e-7c2cb55b1dba
Name:         ScannerSchduler
Location:     \?C:WindowsSystem32TasksMicrosoftWindows 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:WindowsSystem32TasksMicrosoftWindowsShell
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
}
}

相关内容

最新更新