感謝您到目前為止為我提供的所有幫助。我試圖用奇怪的想法來挑戰我正在學習的東西。我正在嘗試通讀 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 對。但是我無法將它嵌套在可以為我創建對象的東西中。我自己會繼續努力的。
假設屬性列表始終按順序排列,您可以使用該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] 删除。
我来说两句