如何修复foreach循环

时间:2016-10-14 08:00:07

标签: powershell

我的foreach循环将所有内容日志添加到单行。如何修复它以向索引添加内容?

$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"
}

1 个答案:

答案 0 :(得分:1)

看看那里:http://ss64.com/ps/foreach.html

MyEntity是列表中的下一个元素,请参考$id而不是$id

试试这个

$test