当前位置: 动力学知识库 > 问答 > 编程问答 >

powershell - How fix foreach loop

问题描述:

My foreach loop add all content logs to single line. How fix it to add content to index?

$FilterHashTable = @{logname='Application'; providername='Microsoft-Windows- SoftwareRestrictionPolicies'; StartTime=$StartTime};

$test = Invoke-Command -ScriptBlock {param ($FilterHashTable) Get-WinEvent -FilterHashtable $FilterHashTable} -ArgumentList $FilterHashTable -ComputerName 491-517 |

Select-Object PSComputerName, TimeCreated, Message

foreach ($id in $test)

{

$PSComputerName = $test.PSComputerName

$TimeCreated = $test.TimeCreated

$Message = $test.Message

Add-Content $logFile "$PSComputerName, $TimeCreated, $Message"

}

网友答案:

Take a look there: http://ss64.com/ps/foreach.html

$id is the the next element in your list, refer to $id instead of $test

Try this

foreach ($id in $test)
{

$PSComputerName = $id.PSComputerName
$TimeCreated = $id.TimeCreated
$Message = $id.Message

Add-Content $logFile  "$PSComputerName, $TimeCreated, $Message"
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: